IOS開發(103)之幕後處理

來源:互聯網
上載者:User

1 前言
IOS4 之後提供了幕後處理,在後台運行應用程式,在一些情形下甚至可以在使用者按下Home按鈕之後在後台運行。

2 詳述
IOS可以在使用者按下Home按鈕後將應用程式添加到暫停狀態。這種暫停執行的狀態在概念上類似於將Mac設定為睡眠模式。應用程式的所有工作記憶體都在RAM中,在暫停時它完全不執行。因此,切換回這樣的應用程式的速度非常快。系統提供了多種方式,通過UIApplication類嚮應用程式通知其執行狀態的變化,該類針對此用途提供了許多委託方法和通知,我們將介紹如何使用他們。

2.1 應用程式的聲明周期
2.1.1 未運行
此狀態表明所有應用程式都位於一個剛剛重啟的裝置上,在裝置開啟狀態下,不論應用程式在何時啟動,只有遇到以下情況應用程式才返回未運行狀態:

(1)應用程式的Info.plist包含UIApplicationExitsOnSuspend鍵設定為YES;

(2)應用程式之前被暫停並且系統需要清楚一些記憶體;

(3)應用程式在運行過程中崩潰。

2.1.2 活動
這是應用程式在螢幕上顯示時的正常運行狀態。他可以接收使用者輸入並更新顯示。

2.1.3 後台
此狀態中,應用程式獲得了一定的時間來執行一些代碼,但是它無法直接存取螢幕或者獲得任何使用者輸入。在使用者按下Home按鈕後不久,所有應用程式都會進入狀態,他們中的大部分會迅速進入暫停狀態。希望在後台啟動並執行應用程式一直處於此狀態,直到被在此啟用。

2.1.4 暫停
停用的應用程式被凍結。普通的應用程式處於後台不久會轉變為此狀態。此應用程式在活動時使用的所有記憶體將原封不懂的得以保留。如果使用者將應用程式切換回活動狀態,它將回複到之前的狀態。另一方面,如果系統需要為當前活動的應用程式提供更多的記憶體,任何停用的應用程式都可能被凍結(並返回到未運行狀態),他們的記憶體將釋放用於其他用途。

2.1.5 不活動
應用程式僅在兩個其他狀態之間的臨界過度階段處於不活動狀態。應用程式可以在任意長的時間內處於不活動狀態的唯一前提是,使用者正在處理系統提示(比如顯示的撥入電話或者SMS提示)或使用者鎖定了螢幕。這基本時上是一種中間的過度狀態。

2.2 狀態更改通知
為了管理這些狀態之間的更改,UIApplication定義了它的委託可以實現的一些方法。除了委託方法,UIApplication還定義了一個匹配的通知名稱集合。這使得除了應用程式委託外的其他對象可以在應用程式狀態更改時註冊通知。

跟蹤應用程式的執行狀態和相應的通知名稱的委託方法:

委託方法通知名稱

//可以在application:didFinishLaunchingWithOptions:添加一些應用程式初始化代碼。


application:didFinishLaunchingWithOptions:UIApplicationDidFinishLaunchingNotification

//如果按下home按鈕,將調用applicationWillResignActive,不應該在applicationWillResignActive中假設應用程式將要進入後台狀態,它只是一種臨界狀態。最終將恢複到活動狀態。


applicationWillResignActive:UIApplicationWillResignActiveNotification

//如果稍後將應用程式切回到前台將調用applicationDidBecomeActive


applicationDidBecomeActive:UIApplicationDidBecomeActiveNotification

//釋放所有有可能在以後重新建立的資源,儲存所有使用者資料,關閉網路連接等。如果在這裡花了太長時間(超過5秒),系統將斷定應用程式的行為異常並終止它。其應該實現applicationWillEnterForeground:來重新建立在applicationDidEnterBackground:中銷毀的內容。


applicationDidEnterBackground:UIApplicationDidEnterBackgroundNotification

applicationWillEnterForeground:UIApplicationWillEnterForegroundNotification

//很少用這個方法,只有在應用程式進入後台,並且系統處於某種原因決定跳過暫停狀態並終止應用程式時,才會真正調用它。

applicationWillTerminate:UIApplicationWillTerminateNotification

2.3執行個體解析
接下來我們建立一個項目,來真正的觀察你一下這些方法的調用時間:


ZYAppDelegate.m:


[plain]
// 
//  ZYAppDelegate.m 
//  State Lab 
// 
//  Created by zhangyuc on 13-6-8. 
//  Copyright (c) 2013年 zhangyuc. All rights reserved. 
// 
 
#import "ZYAppDelegate.h" 
 
#import "ZYViewController.h" 
 
@implementation ZYAppDelegate 
 
- (void)dealloc 

    [_window release]; 
    [_viewController release]; 
    [super dealloc]; 

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

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 
    /** 
     *_cmd:Objective-C提供了一個方便的內建變數,名為_cmd,它始終包含當前方法的選取器。 
     *NSStringFromSelector() :函數返回給制定選取器的NSString表示 
     *二者結合可以提供輸出當前方法名稱的捷徑,無需重新鍵入或者複製黏貼它。 
     */ 
    NSLog(@"%@",NSStringFromSelector(_cmd)); 
    self.viewController = [[[ZYViewController alloc] initWithNibName:@"ZYViewController" bundle:nil] autorelease]; 
    self.window.rootViewController = self.viewController; 
    [self.window makeKeyAndVisible]; 
    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(@"%@",NSStringFromSelector(_cmd)); 

 
- (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(@"%@",NSStringFromSelector(_cmd)); 

 
- (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(@"%@",NSStringFromSelector(_cmd)); 

 
- (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. 
     NSLog(@"%@",NSStringFromSelector(_cmd)); 

 
- (void)applicationWillTerminate:(UIApplication *)application 

    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 
     NSLog(@"%@",NSStringFromSelector(_cmd)); 

 
@end 

//
//  ZYAppDelegate.m
//  State Lab
//
//  Created by zhangyuc on 13-6-8.
//  Copyright (c) 2013年 zhangyuc. All rights reserved.
//

#import "ZYAppDelegate.h"

#import "ZYViewController.h"

@implementation ZYAppDelegate

- (void)dealloc
{
    [_window release];
    [_viewController release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    /**
     *_cmd:Objective-C提供了一個方便的內建變數,名為_cmd,它始終包含當前方法的選取器。
     *NSStringFromSelector() :函數返回給制定選取器的NSString表示
     *二者結合可以提供輸出當前方法名稱的捷徑,無需重新鍵入或者複製黏貼它。
     */
    NSLog(@"%@",NSStringFromSelector(_cmd));
    self.viewController = [[[ZYViewController alloc] initWithNibName:@"ZYViewController" bundle:nil] autorelease];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    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(@"%@",NSStringFromSelector(_cmd));
}

- (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(@"%@",NSStringFromSelector(_cmd));
}

- (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(@"%@",NSStringFromSelector(_cmd));
}

- (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.
     NSLog(@"%@",NSStringFromSelector(_cmd));
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
     NSLog(@"%@",NSStringFromSelector(_cmd));
}

@end
運行結果(控制台):

剛進入程式:


2013-06-08 10:29:17.243 State Lab[1866:c07] application:didFinishLaunchingWithOptions:

2013-06-08 10:29:17.248 State Lab[1866:c07] applicationDidBecomeActive:

點擊Home鍵:
再重新回到項目:
最後一種情況很有趣,先將程式迅速地在此啟用,然後變為不活動,最後進入後台。
這些委託方法和通知都直接與某種“運行”狀態有關:活動,不活動和後台。每個委託方法僅在一種狀態中調用(每個通知也僅在一種狀態中出現)。最重要的狀態過度是在活動狀態與其他狀態之間,一些過度(比如從後台到暫停)不會出現任何通知。


2013-06-08 10:29:48.598 State Lab[1866:c07] applicationWillResignActive:

2013-06-08 10:29:48.599 State Lab[1866:c07] applicationDidEnterBackground:


2013-06-08 10:30:11.357 State Lab[1866:c07] applicationWillEnterForeground:

2013-06-08 10:30:11.358 State Lab[1866:c07] applicationDidBecomeActive:

如果手機收到一條SMS訊息:

2013-06-08 10:29:48.598 State Lab[1866:c07] applicationWillResignActive:


如果關閉該訊息:

2013-06-08 10:29:17.248 State Lab[1866:c07] applicationDidBecomeActive:


如果回複SMS訊息:

2013-06-08 10:29:17.248 State Lab[1866:c07] applicationDidBecomeActive:


2013-06-08 10:29:48.598 State Lab[1866:c07] applicationWillResignActive:

2013-06-08 10:29:48.599 State Lab[1866:c07] applicationDidEnterBackground:

 


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.