简体   繁体   English

如何在applicationDidBecomeActive中确定它是否是最初的iPhone应用程序启动?

[英]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.

相关问题 如何知道在调用applicationWillEnterForeground或applicationDidBecomeActive时是否已显示启动映像? - How to know whether launch image has been shown when applicationWillEnterForeground or applicationDidBecomeActive is called? iPhone +应用内购买+确定是否购买了产品 - iPhone + In App purchase + determine whether product is purchased or not 如何确定我们的设备中是否存在 iphone 应用程序(上一个/下一个版本)? - How to determine, whether an iphone app (previous/next version) is existed in our device or not? 如何从iPhone应用程序启动另一个应用程序 - How to launch another app from an iPhone app 在iPhone中,如何从应用程序检查互联网是否存在? - In iphone, how to check whether internet is there from the app? 如何确定当前设备是iPhone还是iPad? - How do I determine whether the current device is an iPhone or iPad? 如何确定用户是在iPhone上的Edge还是3G - How to determine whether user is on Edge or 3G on iPhone 如何非公开地启动iPhone应用程序? - How to launch an iPhone app non-publicly? 如何检测首次在 iPhone 上启动的应用程序 - How to detect first time app launch on an iPhone iphone上的applicationDidBecomeActive时如何从活动实例中设置NSTimer的目标 - How to set a target of NSTimer from active instance when applicationDidBecomeActive on iphone
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM