[英]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
您需要使用boolForKey
和setBool
if (![[NSUserDefaults standardUserDefaults]boolForKey:@"firsttime"]) {
[[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"firsttime"];
[[NSUserDefaults standardUserDefaults]synchronize];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.