简体   繁体   English

在 iOS 4.3 中,当您按下主页按钮或按下开/关按钮时,我如何区分背景模式?

[英]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.随着applicationDidBecomeActive applicationWillResignActive会在您进入后台时进入。 But there is a way to differentiate by getting the state of the app, so try this in applicationDidEnterBackground .但是有一种方法可以通过获取应用程序的 state 来区分,所以在applicationDidEnterBackground中试试这个。

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

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

Apple's UIApplication-class reference Apple 的 UIApplication 类参考

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).当您的应用程序进入后台(主页按钮)时使用- (void)applicationDidEnterBackground:(UIApplication *)application {}并在即将关闭时- (void)applicationWillTerminate:(UIApplication *)application (开/关按钮或 iOS在后台随机时间后调用关闭)。

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.我的理解是,当您锁定或解锁 iOS 设备时,您的应用程序委托将分别调用- (void)applicationWillResignActive:(UIApplication *)application- (void)applicationDidBecomeActive:(UIApplication *)application 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 .通过点击主页按钮将应用程序发送到后台调用不同的方法,即- (void)applicationDidEnterBackground:(UIApplication *)application- (void)applicationWillEnterForeground:(UIApplication *)application

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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