简体   繁体   English

如何检查用户是否首次访问我的iOS应用?

[英]How to check if user visit my iOS app for the first time?

If user is opening application for the first time after downloading it, then I want to show a view controller with tutorial and then go to main application's View Controller. 如果用户下载后第一次打开应用程序,那么我想显示带有教程的视图控制器,然后转到主应用程序的视图控制器。 But if user is already visited app before, then go to Main view controller. 但是,如果用户之前已经访问过应用程序,则转到主视图控制器。

How to implement that check in AppDelegate? 如何在AppDelegate中实施该检查?

add in your app delegate 添加您的应用程序委托

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    if ([[NSUserDefaults standardUserDefaults]objectForKey:@"firsttime"]) {
        [[NSUserDefaults standardUserDefaults]setObject:@"YES" forKey:@"firsttime"];
        [[NSUserDefaults standardUserDefaults]synchronize];
    }

}

If condition will check for the key is available in app or not. 如果条件将检查密钥是否在应用程序中可用。

If it is not available then it will save it to YES. 如果不可用,则将其保存为“是”。

You can do as you want. 您可以随心所欲。

I would do that in your main view controller in method viewWillAppear . 我会在方法viewWillAppear主视图控制器中执行此操作。 You can use NSUserDefaults . 您可以使用NSUserDefaults Here's an example: 这是一个例子:

Swift: 迅速:

func isAppAlreadyLaunchedOnce() {
    let defaults = NSUserDefaults.standardUserDefaults()

    if let isAppAlreadyLaunchedOnce = defaults.stringForKey("isAppAlreadyLaunchedOnce"){
        println("App already launched")
        return true
    } else {
        defaults.setBool(true, forKey: "isAppAlreadyLaunchedOnce")
        println("App launched first time")
        //Show your tutorial.
        return false
    }
}

Objective-C: 目标C:

- (void)isAppAlreadyLaunchedOnce {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *isAppAlreadyLaunchedOnce = [defaults stringForKey:@"isAppAlreadyLaunchedOnce"];

    if (isAppAlreadyLaunchedOnce != nil) {
        NSLog(@"App already launched");
        return true;
    } else {
        NSLog(@"App launched first time");
        [defaults setBool:true forKey:@"isAppAlreadyLaunchedOnce"];
        //Show your tutorial.
        return false;
    }
}

And then run this method in your viewWillAppear : 然后在您的viewWillAppear运行此方法:

Swift: 迅速:

func viewWillAppear(animated: Bool) {
    isAppAlreadyLaunchedOnce()
}

Objective-C: 目标C:

- (void)viewWillAppear:(BOOL)animated {
    [self isAppAlreadyLaunchedOnce];
}

NSUserDefaults will be cleared when user will uninstall app. 用户卸载应用程序时,将清除NSUserDefaults

Add a flag in NSUserDefaults . NSUserDefaults添加一个标志。 When the user launches the app for the first time, the flag is not set. 用户首次启动该应用程序时,未设置标志。 After checking the status of the flag, if the flag is not set the flag should be set. 检查标志的状态后,如果未设置标志,则应设置标志。

you need to use boolForKey and setBool 您需要使用boolForKeysetBool

if (![[NSUserDefaults standardUserDefaults]boolForKey:@"firsttime"]) {
                [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"firsttime"];
                [[NSUserDefaults standardUserDefaults]synchronize];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查用户是否从iOS上的另一个应用程序调用我的应用程序? - How to check if user call my app from another app on iOS? 如何检查用户是否从另一个应用程序安装了我的应用程序? iOS(统一) - How to check if a user installed my app from my another app? iOS (Unity) iOS:检查首次使用Facebook登录的用户-目标C - iOS: Check first time user logs in using Facebook - Objective C 如何在iOS 6和iOS 7中检查用户是否允许我的应用使用后置摄像头 - How to check user has permitted back camera usage for my app in both iOS 6 and iOS 7 如何在应用程序第一次运行时在我的iOS应用程序中打开页面视图控制器? - How to open a page view controller in my iOS app only the first time the app runs? 如何设置首次启动iOS App - How to setup first time launch of an iOS App iOS首次启动使链接用户从以下位置下载应用程序 - IOS first time launch get the link user downloaded the app from 如何确定用户第一次运行应用程序? - How to determine that user runs the app for the first time? 每当用户从App Store购买我的iOS应用程序时,如何获得通知? - How can I get notified every time a user purchases my iOS app from the App Store? 新安装的iOS Google应用如何在我第一次运行帐户时知道我的帐户? - How does a newly installed iOS Google app know about my account the first time I run it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM