简体   繁体   中英

in iOS 4.3, how i can differentiate between the background mode, when you press the home button or when you press the on/off button?

I already use the:

(void)applicationDidEnterBackground:(UIApplication *)application {}

method, but I can't differentiate if is because press the home button or the on/off button.

Thanks in advance,

For the on/off button(or an incoming call or SMS):

- (void)applicationWillResignActive:(UIApplication *)application  

For the Home button:

- (void)applicationDidEnterBackground:(UIApplication *)application

With the notification of applicationWillResignActive , applicationDidBecomeActive will still enter while you are entering in background. But there is a way to differentiate by getting the state of the app, so try this in applicationDidEnterBackground .

- (void)appHasGoneInBackground {       
    bool inBackground = [UIApplication sharedApplication].applicationState == UIApplicationStateBackground;

    // lockScreen state
    if (!inBackground) {
        // do something
    }
}

Apple's UIApplication-class reference

Use - (void)applicationDidEnterBackground:(UIApplication *)application {} when your app is entering the background (home button) and - (void)applicationWillTerminate:(UIApplication *)application when it's about to be closed (on/off button or iOS call to close after a random time in background).

My understanding is that when you lock or unlock your iOS device your application delegate will call - (void)applicationWillResignActive:(UIApplication *)application and - (void)applicationDidBecomeActive:(UIApplication *)application , respectively. Locking and unlocking are similar to receiving an interruption like a phone call. Sending your application to the background by hitting the home button calls different methods, namely - (void)applicationDidEnterBackground:(UIApplication *)application and - (void)applicationWillEnterForeground:(UIApplication *)application .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM