【问题标题】:When are each of these iOS backgrounding delegate methods called?什么时候调用这些 iOS 后台委托方法?
【发布时间】:2012-07-13 13:59:18
【问题描述】:

我很困惑在哪些情况下会调用这些 iOS 委托方法中的哪一个:

- (void)applicationWillResignActive:(UIApplication *)application {
}


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


- (void)applicationWillEnterForeground:(UIApplication *)application {
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
}


- (void)applicationWillTerminate:(UIApplication *)application {
    }

我尝试过使用它们,因为如果按下主页按钮,我希望我的应用在退出之前立即执行一些操作,并且在应用重新打开时执行一次。谁能快速总结一下何时使用这些/何时在我的应用程序中调用它们?

【问题讨论】:

    标签: iphone background terminate


    【解决方案1】:

    Apple 自己的描述是最好的,你会在 Xcode 的每个模板应用程序中找到它。

    - (void)applicationWillResignActive:(UIApplication *)application
    {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }
    
    - (void)applicationDidEnterBackground:(UIApplication *)application
    {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }
    
    - (void)applicationWillEnterForeground:(UIApplication *)application
    {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }
    
    - (void)applicationDidBecomeActive:(UIApplication *)application
    {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }
    
    - (void)applicationWillTerminate:(UIApplication *)application
    {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }
    

    【讨论】:

    • 那些是我的模板,但我对这些东西很陌生,我发现它们有点复杂。我不明白什么时候“终止”和“进入后台”等等。
    【解决方案2】:
    - (void)applicationWillResignActive:(UIApplication *)application {
    // Home button was pressed! Do some actions here!
    }
    
    
    - (void)applicationDidEnterBackground:(UIApplication *)application {
    // Your app is now running in the background. It is no longer visible.
    }
    
    
    - (void)applicationWillEnterForeground:(UIApplication *)application {
    // Your application was running in the background, but is now being re-opened
    }
    
    
    - (void)applicationDidBecomeActive:(UIApplication *)application {
    // You application is now once again active
    }
    
    
    - (void)applicationWillTerminate:(UIApplication *)application {
    // Your application is about to QUIT.
        }
    

    【讨论】:

    • 谢谢!我知道这些可能看起来很基本,但很难理解它们什么时候真正发生。
    • 我相信 applicationWillResignActive(及其对应的 applicationDidBecomeActive)也会在来电或其他系统中断时被调用。
    猜你喜欢
    • 2012-07-17
    • 1970-01-01
    • 1970-01-01
    • 2016-05-12
    • 2016-07-16
    • 2013-02-25
    • 1970-01-01
    • 1970-01-01
    • 2011-09-19
    相关资源
    最近更新 更多