你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS開發(103)之後台處理

IOS開發(103)之後台處理

編輯:IOS開發綜合

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:

 


 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved