標籤:
UIApplication簡單從字面上瞭解就是應用程式,開發的時候有的時候會根據需要調用其中的方法,看起來不起眼,實際在iOS開發UIApplication提供了iOS程式運行期間的控制和協作工作。每一個應用程式在運行期必須有且僅有一個UIApplication(或則其子類)的一個執行個體,就是設計模式中常說的單例模式,通過sharedApplication擷取單例對象,不需要另外的init一個UIApplication。
UIApllication概念
經常程式可能編譯的時候沒錯,啟動並執行時候報錯,通常都是停留在main.m中,這是程式的進入點,也從這裡建立了UIApplication單例執行個體。
int main(int argc, char * argv[]) { @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); }}
UIIApplication一般主要用於處理使用者事件,處理的時候會建立一個隊列,將所有使用者事件都放入隊列,逐一處理,在處理的時候,它會發送當前事件 到一個合適的處理事件的目標控制項。同時UIApplication執行個體還維護一個在本應用中開啟的window列表(UIWindow執行個體),這樣UIApplication可以擷取應用中的任何一個UIView對象。UIApplication執行個體會被賦予一個代理對象,以處理應用程式的生命週期事件(比如程式啟動和關閉),系統事件(比如電話、簡訊)等等。具體看下具體應用,
故事板中建立一個按鈕,之後的畫擷取點擊事件,加入以下兩行代碼:
UIApplication *application=[UIApplication sharedApplication]; //設定表徵圖上的更新數字 application.applicationIconBadgeNumber=25;
在iOS8.0之前會看到App上面有紅色的數字顯示,前幾個月頭像整人事件,不少人被坑了,強迫自己幾天沒看,看到小紅點就想點,控制台會提示以下資訊:Attempting to badge the application icon but haven‘t received permission from the user to badge the application
具體解決辦法很簡單,就是擷取以下機器的版本號碼,然後根據需要通知使用者選擇設定(個人覺得更人性化,手機上要是滿屏的小紅點,想想就。。);
float versionNumber=[[[UIDevice currentDevice] systemVersion] floatValue]; if (versionNumber >= 8.0) { NSLog(@"%@",[NSString stringWithFormat:@"當前的系統為%f",versionNumber]); //通知設定 UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil]; //接收使用者的選擇 [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; }
UIApplication可以做一些其他的事情,比如其中的openURL就比較實用:
//電話 [application openURL:[NSURL URLWithString:@"tel://10010"]]; //簡訊 [application openURL:[NSURL URLWithString:@"sms://10010"]]; //郵件 [application openURL:[NSURL URLWithString:@"mailto://[email protected]"]]; //URL [application openURL:[NSURL URLWithString:@"http://www.cnblogs.com/xiaofeixiang/"]];
UIApplicationDelegate
UIApllicationDelegate協議中定義的方法有有一部分是和應用程式的狀態是相關的,建立的項目預設的都有AppDelegate.h和AppDelegate.m檔案,可以先參考以下預設產生的AppDelegate.m代碼:
//// AppDelegate.m// UIApplicationDemo//// Created by keso on 15/2/5.// Copyright (c) 2015年 keso. All rights reserved.//#import "AppDelegate.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. NSLog(@"application-程式完成載入"); 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(@"applicationWillResignActive-登出啟用");}- (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(@"applicationDidEnterBackground-進入後台");}- (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(@"applicationWillEnterForeground-進入前台");}- (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(@"applicationDidBecomeActive-成為啟用狀態");}- (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. NSLog(@"applicationWillTerminate-程式終止狀態");}@end
最開始main函數中其實是將AppDelegate執行個體化交給UIApplication去代理執行其中的方法,如果你喜歡這個AppDelegate檔案,完成有必須要根據需要建立一個自己的類檔案,只要聲明UIApplicationDelegate即可。關於上面的狀態我們可以先看一張蘋果官網的圖:
其中方法蘋果預設有解釋,不過大概寫下個人的理解:
application:didFinishLaunchingWithOptions:—應用展示給使用者之前完成最終的初始化工作
applicationWillResignActive:-從啟用狀態進入休眠狀態,突然的電話,簡訊可以使當前程式進入後台,不可互動
applicationDidEnterBackground-應用程式進入後台,可以儲存和傳輸資料
applicationWillEnterForeground-可以在這裡恢複資料,即將進入前台,還不是啟用狀態
applicationDidBecomeActive-進入前台擷取擷取焦點,可以互動
applicationWillTerminate-應用程式銷毀的時候調用,如果掛起不調用
寫到這裡可以具體看一下App的啟動過程:
1.main函數
2.UIApplicationMain 建立UIApplication對象,建立UIApplication的delegate對象(監聽應用程式狀態)
3.根據Info.plist獲得最主要storyboard的檔案名稱,載入最主要的storyboard,建立UIWindow(如果沒有故事板,程式啟動完畢時調用代理的application:didFinishLaunchingWithOptions:方法,在application:didFinishLaunchingWithOptions:中建立UIWindow ),建立和設定UIWindow的rootViewController, 顯示視窗;
iOS開發-UIApplication和App啟動狀態