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:
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的官方文档。
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
- 3 回答
- 0 关注
- 1717 浏览
添加回答
举报