如何检测iPhone上的第一次应用程序启动
我怎样才能检测到第一次启动
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // if very first launch than perform actionA // else perform actionB }
方法?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { if (![[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"]) { [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"]; [[NSUserDefaults standardUserDefaults] synchronize]; } return YES; }
我为这个目的写了一个小图书馆。 它让我知道这是否是有史以来第一次启动,或只是为了这个版本,以及用户安装的任何以前的版本。 它在github上作为一个cocoapod在Apache 2许可下可用: GBVersionTracking
你只要在application:didFinishLaunching:withOptions:
调用它application:didFinishLaunching:withOptions:
[GBVersionTracking track];
然后检查这是否是第一次发射只要在任何地方打电话:
[GBVersionTracking isFirstLaunchEver];
与此类似:
[GBVersionTracking isFirstLaunchForVersion]; [GBVersionTracking currentVersion]; [GBVersionTracking previousVersion]; [GBVersionTracking versionHistory];
在Swift 3中试试这个:
func isAppAlreadyLaunchedOnce()->Bool{ let defaults = UserDefaults.standard if let isAppAlreadyLaunchedOnce = defaults.string(forKey: "isAppAlreadyLaunchedOnce"){ print("App already launched : \(isAppAlreadyLaunchedOnce)") return true }else{ defaults.set(true, forKey: "isAppAlreadyLaunchedOnce") print("App launched first time") return false } }
在Swift 2中试试这个,
func isAppAlreadyLaunchedOnce()->Bool{ let defaults = NSUserDefaults.standardUserDefaults() if let isAppAlreadyLaunchedOnce = defaults.stringForKey("isAppAlreadyLaunchedOnce"){ print("App already launched : \(isAppAlreadyLaunchedOnce)") return true }else{ defaults.setBool(true, forKey: "isAppAlreadyLaunchedOnce") print("App launched first time") return false } }
更新: –对于OBJ-C我使用这个,
+ (BOOL)isAppAlreadyLaunchedOnce { if ([[NSUserDefaults standardUserDefaults] boolForKey:@"isAppAlreadyLaunchedOnce"]) { return true; } else { [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isAppAlreadyLaunchedOnce"]; [[NSUserDefaults standardUserDefaults] synchronize]; return false; } }
参考OBJ-C: https : //stackoverflow.com/a/9964400/3411787
Xcode 7和Swift 2.0的另一个想法是使用扩展
extension NSUserDefaults { func isFirstLaunch() -> Bool { if !NSUserDefaults.standardUserDefaults().boolForKey("HasAtLeastLaunchedOnce") { NSUserDefaults.standardUserDefaults().setBool(true, forKey: "HasAtLeastLaunchedOnce") NSUserDefaults.standardUserDefaults().synchronize() return true } return false } }
现在你可以在你的应用程序的任何地方写
if NSUserDefaults.standardUserDefaults().isFirstLaunch() { // do something on first launch }
我个人比较喜欢UIApplication的扩展:
extension UIApplication { class func isFirstLaunch() -> Bool { if !NSUserDefaults.standardUserDefaults().boolForKey("HasAtLeastLaunchedOnce") { NSUserDefaults.standardUserDefaults().setBool(true, forKey: "HasAtLeastLaunchedOnce") NSUserDefaults.standardUserDefaults().synchronize() return true } return false } }
因为函数调用更具描述性:
if UIApplication.isFirstLaunch() { // do something on first launch }
你可以用下面的静态方法实现它:
+ (BOOL)isFirstTime{ static BOOL flag=NO; static BOOL result; if(!flag){ if ([[NSUserDefaults standardUserDefaults] boolForKey:@"hasLaunchedOnce"]){ result=NO; }else{ [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"hasLaunchedOnce"]; [[NSUserDefaults standardUserDefaults] synchronize]; result=YES; } flag=YES; } return result; }
启动时需要保存一些内容,然后检查是否存在。 如果没有,这是第一次。 “东西”可以是文件,数据库条目,用户默认设置….
为迅速3.0
添加扩展
extension UIApplication { class func isFirstLaunch() -> Bool { if UserDefaults.standard.bool(forKey: "hasBeenLaunchedBeforeFlag") { UserDefaults.standard.set(true, forKey: "hasBeenLaunchedBeforeFlag") UserDefaults.standard.synchronize() return true } return false } }
然后在你的代码
UIApplication.isFirstLaunch()
这很简单,只需要六行代码。
将这些代码添加到应用程序启动首选项或其他任何您可能需要testing是否第一次运行应用程序的位置将会非常有用。
//These next six lines of code are the only ones required! The rest is just running code when it's the first time. //Declare an integer and a default. NSUserDefaults *theDefaults; int launchCount; //Set up the properties for the integer and default. theDefaults = [NSUserDefaults standardUserDefaults]; launchCount = [theDefaults integerForKey:@"hasRun"] + 1; [theDefaults setInteger:launchCount forKey:@"hasRun"]; [theDefaults synchronize]; //Log the amount of times the application has been run NSLog(@"This application has been run %d amount of times", launchCount); //Test if application is the first time running if(launchCount == 1) { //Run your first launch code (Bring user to info/setup screen, etc.) NSLog(@"This is the first time this application has been run"; } //Test if it has been run before if(launchCount >= 2) { //Run new code if they have opened the app before (Bring user to home screen etc. NSLog(@"This application has been run before); }
PS 不要在首选项中使用布尔只要坚持整数。 未定义时,它们默认为零。
另外, [theDefaults synchronize];
行并不是必需的,但我发现,当一个应用程序在数百个设备上运行数百次时,结果并不总是可靠的,此外,这是更好的做法。
存储布尔键在NSUserDefaults第一次它会是你会改变它是,并保持它,直到应用程序删除或重新安装将再次是第一次。
对于Xcode 7中的Swift 2.0。在AppDelegate.swift文件中:
import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(application: UIApplication, willFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool { return true } func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { didFinishLaunchingOnce() return true } func didFinishLaunchingOnce() -> Bool { let defaults = NSUserDefaults.standardUserDefaults() if let hasBeenLauncherBefore = defaults.stringForKey("hasAppBeenLaunchedBefore") { //print(" N-th time app launched ") return true } else { //print(" First time app launched ") defaults.setBool(true, forKey: "hasAppBeenLaunchedBefore") return false } } }
快速和容易的function
- (BOOL) isFirstTimeOpening { NSUserDefaults *theDefaults = [NSUserDefaults standardUserDefaults]; if([theDefaults integerForKey:@"hasRun"] == 0) { [theDefaults setInteger:1 forKey:@"hasRun"]; [theDefaults synchronize]; return true; } return false; }
NSUserDefaults +macros
最好的方法是使用NSUserDefaults
并保存一个BOOL
variables。 如上所述,下面的代码将会很好:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; [userDefaults setObject:[NSNumber numberWithBool:true] forKey:@"~applicationHasLaunchedBefore"]; [userDefaults synchronize];
您还可以创build一个macros如下,以便轻松检查是否是第一次启动
#define kApplicationHasLaunchedBefore [[NSUserDefaults standardUserDefaults] objectForKey:@"~applicationHasLaunchedBefore"]
那么就这样使用它,
if (kApplicationHasLaunchedBefore) { //App has previously launched } else { //App has not previously launched }