为了账号安全,请及时绑定邮箱和手机立即绑定

applicationWillEnterForeground与applicationDid

applicationWillEnterForeground与applicationDid

子衿沉夜 2019-12-07 14:29:20
当应用程序从后台唤醒并且您希望它准备好处于活动状态时,哪个合适的委托实现?applicationWillEnterForeground与applicationDidBecomeActive-有什么区别?当应用程序要进入睡眠状态并且您想为其准备清理和保存数据时,哪个合适的委托实现?applicationWillResignActive与applicationDidEnterBackground-有什么区别?另外,我注意到当传入的SMS或呼叫进入时,会调用applicationWillResignActive,但是用户选择单击“确定”并继续。我不希望我的应用在这些情况下采取任何措施。我只希望它继续运行而无需任何中间清理,因为用户没有退出该应用程序。因此,我认为仅在applicationDidEnterBackground中进行清理工作更有意义。我希望您能就最佳做法提供宝贵的意见,然后再选择要实施的代表来唤醒和入睡,以及考虑诸如SMS /呼叫中断等事件。
查看完整描述

3 回答

?
缥缈止盈

TA贡献2041条经验 获得超4个赞

唤醒时即重新启动应用程序(通过跳板,应用程序切换或URL)applicationWillEnterForeground:。在应用程序准备就绪后,仅在后台运行后才执行一次,而applicationDidBecomeActive:在启动后可能会被多次调用。这applicationWillEnterForeground:对于重新启动后只需进行一次设置的理想选择。

applicationWillEnterForeground: 叫做:

  • 重新启动应用程序时

  • 之前 applicationDidBecomeActive:

applicationDidBecomeActive: 叫做:

  • 应用首次启动后的时间 application:didFinishLaunchingWithOptions:

  • applicationWillEnterForeground:如果没有URL处理。

  • 之后application:handleOpenURL:被称为。

  • 之后applicationWillResignActive:,如果用户忽略中断就像一个电话或短信。

applicationWillResignActive: 叫做:

  • 当有电话之类的干扰时。

    • 如果用户接听电话applicationDidEnterBackground:被呼叫。

    • 如果用户忽略呼叫applicationDidBecomeActive:被调用。

  • 当按下主屏幕按钮或用户切换应用程序时。

  • 文档说你应该

    • 暂停正在进行的任务

    • 禁用计时器

    • 暂停游戏

    • 降低OpenGL帧率

applicationDidEnterBackground: 叫做:

  • 后 applicationWillResignActive:

  • 文档说您应该:

    • 释放共享资源

    • 保存用户数据

    • 使计时器无效

    • 保存应用程序状态,以便在应用程序终止时可以将其恢复。

    • 禁用UI更新

  • 您有5秒的时间做您需要做的事情并返回方法

    • 如果您未在约5秒钟内返回,则该应用程序将终止。

    • 您可以要求更多时间 beginBackgroundTaskWithExpirationHandler:


查看完整回答
反对 回复 2019-12-07
?
鸿蒙传说

TA贡献1865条经验 获得超7个赞

管理应用程序的生命周期有助于解决您的问题。有关快速概念,您可以参阅该文档中的图。您还可以从XCode向导生成的代码中读取注释。列出如下:


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    // Override point for customization after application launch.

    return YES;

}


- (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:.

     */

}

有关更多详细说明,请参阅UIApplicationDelegate的官方文档。


查看完整回答
反对 回复 2019-12-07
?
绝地无双

TA贡献1946条经验 获得超4个赞

我仍然对Dano的回答有些困惑,因此我做了一些测试以获取某些情况下的事件流以供参考,但是它对您也可能有用。这适用UIApplicationExitsOnSuspend于不在info.plist中使用的应用。这是在iOS 8模拟器上进行的,并已通过iOS 7设备确认。请原谅Xamarin的事件处理程序名称。它们非常相似。


从非运行状态进行的初始启动和所有后续启动:

完成启动


激活


中断(电话,顶部向下滑动,底部向上滑动):

主页按钮双击列出不活动的应用程序,然后重新选择我们的应用程序:

OnResignActivation


激活


主页按钮连按两次列出不活动的应用程序,选择另一个应用程序,然后重新启动我们的应用程序:

按下主页按钮,然后重新启动:

锁定(开/关按钮),然后解锁:

OnResignActivation


DidEnterBackground


进入前景


激活


双击主屏幕按钮,然后终止我们的应用程序:(随后的重新启动是第一种情况)

OnResignActivation


DidEnterBackground


DidEnterBackground(仅限iOS 7?)


是的,DidEnterBackground在iOS7设备上被两次调用。两次UIApplication状态均为Background。但是,iOS 8模拟器没有。这需要在iO


查看完整回答
反对 回复 2019-12-07
  • 3 回答
  • 0 关注
  • 1567 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信