IOS开发 applicationDidBecomeActive 应用程序挂起、复原与终止

一、挂起

当有电话进来或者锁屏,这时你的应用程会挂起,在这时,UIApplicationDelegate委托会收到通知,调用 applicationWillResignActive方法,你可以重写这个方法,做挂起前的工作,比如关闭网络,保存数据。

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

当你的程序被挂起后他不会在后台运行。

二、复原
当程序复原时,另一个名为 applicationDidBecomeActive 委托方法会被调用,在此你可以通过之前挂起前保存的数据来恢复你的应用程序:

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

注意:应用程序在启动时,在调用了 applicationDidFinishLaunching 方法之后也会调用 applicationDidBecomeActive 方法,所以你要确保你的代码能够分清复原与启动,避免出现逻辑上的bug。

三、终止
当用户按下按钮,或者关机,程序都会被终止。当一个程序将要正常终止时会调用 applicationWillTerminate 方法。但是如果长主按钮强制退出,则不会调用该方法。这个方法该执行剩下的清理工作,比如所有的连接都能正常关闭,并在程序退出前执行任何其他的必要的工作:

- (void)applicationWillTerminate:(UIApplication*)application{    
    
} 
https://blog.csdn.net/iukey/article/details/7311115

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
{  
    // Override point for customization after application launch.  
    NSLog(@"\n ===> 程序开始 !");   
      
    return YES;  
}  
                              
- (void)applicationWillResignActive:(UIApplication *)application  
{  
      
    NSLog(@"\n ===> 程序暂行 !");   
}  
  
- (void)applicationDidEnterBackground:(UIApplication *)application  
{  
      
     NSLog(@"\n ===> 程序进入后台 !");   
}  
  
- (void)applicationWillEnterForeground:(UIApplication *)application  
{  
      
     NSLog(@"\n ===> 程序进入前台 !");   
}  
  
- (void)applicationDidBecomeActive:(UIApplication *)application  
{  
    NSLog(@"\n ===> 程序重新激活 !");   
      
}  
  
- (void)applicationWillTerminate:(UIApplication *)application  
{  
    NSLog(@"\n ===> 程序意外暂行 !");   
  
    UIDevice *device = [UIDevice currentDevice];  
      
}  

首次运行:

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  • (void)applicationDidBecomeActive:(UIApplication *)application

首次关闭(home):

  • (void)applicationWillResignActive:(UIApplication *)application
  • (void)applicationDidEnterBackground:(UIApplication *)application

再次运行:

  • (void)applicationWillEnterForeground:(UIApplication *)application
  • (void)applicationDidBecomeActive:(UIApplication *)application

再次关闭:

  • (void)applicationWillResignActive:(UIApplication *)application
  • (void)applicationDidEnterBackground:(UIApplication *)application

https://blog.csdn.net/m372897500/article/details/32710321

你可能感兴趣的:(IOS开发 applicationDidBecomeActive 应用程序挂起、复原与终止)