[英]how to determine in applicationDidBecomeActive whether it is the initial iPhone app launch?
how to determine in how to determine in UIApplicationDidBecomeActiveNotification whether it is the initial app launch?whether it is the initial app launch? 如何确定如何在UIApplicationDidBecomeActiveNotification中确定它是否是初始应用程序启动?是否是初始应用程序启动?
that is the initial start up of the application, as opposed to subsequent DidBecomeActive's due to the application being put in background and then to foreground (eg user goes to calendar then back to your app) 这是应用程序的初始启动,而不是后续的DidBecomeActive,因为应用程序被放在后台然后到前台(例如用户转到日历然后回到你的应用程序)
FWIW, the accepted answer tells you if the app has ever been launched before, not if the app is resuming from the background vs launching. FWIW,接受的答案会告诉您之前是否有应用程序启动,而不是应用程序是从后台恢复还是启动。 Once the
alreadyLaunched
key has been set in preferences it will return YES
when the app is launched in the future (vs resumed from background). 一旦
alreadyLaunched
在首选项中设置了已启动的密钥,它将在将来启动应用程序时返回YES
(vs从后台恢复)。
To detect if the app has resumed from the background you don't need to add anything to preferences. 要检测应用程序是否已从后台恢复,您无需向首选项添加任何内容。 Rather, do the following in your app delegate implementation.
而是在您的应用程序委托实现中执行以下操作。
// myAppDelegate.m
//
@interface MyAppDelegate()
@property (nonatomic) BOOL activatedFromBackground;
@end
@implementation MyAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.activatedFromBackground = NO;
// your code
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
self.activatedFromBackground = YES;
// your code
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
if (self.activatedFromBackground) {
// whatever you want here
}
}
@end
In your applicationDidFinishLaunching:withOptions:
put this: 在你的
applicationDidFinishLaunching:withOptions:
把这个:
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"alreadyLaunched"];
[[NSUserDefaults standardUserDefaults] synchronize];
Then, in didBecomeActive
: 然后,在
didBecomeActive
:
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"alreadyLaunched"]) {
// is NOT initial launch...
} else {
// is initial launch...
}
I used to use the method mentioned by @XJones. 我曾经使用过@XJones提到的方法。 Then I realized it has a potential issue: if "the initial app launch" means to check in applicationDidBecomeActive whether it was called the first time since the app was launched!
然后我意识到它有一个潜在的问题:如果“初始应用程序启动”意味着检查applicationDidBecomeActive是否自应用程序启动以来第一次调用它! Because when app was relaunching the app (either through springboard, app switching or URL) all the above 3 delegate method will be called!
因为当app重新启动应用程序时(通过跳板,应用程序切换或URL),所有上述3个委托方法都将被调用! So the safest way is to reset self.activatedFromBackground in applicationDidBecomeActive.
所以最安全的方法是在applicationDidBecomeActive中重置self.activatedFromBackground。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.