现在的位置: 首页 > 综合 > 正文

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

2016年08月29日 ⁄ 综合 ⁄ 共 3628字 ⁄ 字号 评论关闭

一、挂起

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

C代码  

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

/*添加你自己的挂起前准备代码*/    

}    

 

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

 

二、复原

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

C代码  

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

/*添加你的恢复代码*/    

}    

 

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

 

三、终止

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

C代码  

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

/*在这里添加退出前的清理代码以及其他工作代码*/    

}    

 

来源: http://blog.csdn.net/iukey/article/details/7311115

 

 

Java代码  

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

{  

    // Override point for customization after application launch.  

    NSLog(@"\n ===> 程序开始 !");   

      

    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. 

     */  

    NSLog(@"\n ===> 程序暂行 !");   

}  

  

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

     */  

     NSLog(@"\n ===> 程序进入后台 !");   

}  

  

- (void)applicationWillEnterForeground:(UIApplication *)application  

{  

    /* 

     Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 

     */  

     NSLog(@"\n ===> 程序进入前台 !");   

}  

  

- (void)applicationDidBecomeActive:(UIApplication *)application  

{  

    NSLog(@"\n ===> 程序重新激活 !");   

    /* 

     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  

{  

    NSLog(@"\n ===> 程序意外暂行 !");   

  

    UIDevice *device = [UIDevice currentDevice];  

    /* 

     Called when the application is about to terminate. 

     Save data if appropriate. 

     See also applicationDidEnterBackground:. 

     */  

}  

 

首次运行

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

抱歉!评论已关闭.