iOS學習之UIApplication及其代理,iosuiapplication
1. 什麼是UIApplication
通過 [UIApplication shareApplication] 可以擷取這個單例對象。
弄成單例的原因:
UIApplication 對象是用來設定應用全域資訊的,一個應用程式如果有很多 UIApplication 對象,都不知道聽誰的。
2. UIApplication的作用(常用的屬性和方法)
UIApplication 一般用來做一些應用層級的操作(app提醒框,控制連網的狀態,打電話,開啟網頁)。
1> 設定appIcon提醒數字
表徵圖需要手動清除,應用程式關閉,不會自動清除。
iOS標頭檔中的屬性聲明:
@property(nonatomic) NSInteger applicationIconBadgeNumber __TVOS_PROHIBITED; // set to 0 to hide. default is 0. In iOS 8.0 and later, your application must register for user notifications using -[UIApplication registerUserNotificationSettings:] before being able to set the icon badge.
通過注釋我們可以得知:iOS8之後必須註冊使用者通知,註冊後才能顯示提醒數字
註冊使用者通知方法聲明:
// Registering UIUserNotificationSettings more than once results in previous settings being overwritten.- (void)registerUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED;
iOS8.0推出註冊使用者通知的目的是為了提高使用者的體驗,對於有強迫症的使用者可以通過這個通知將提醒數字關閉。
執行個體代碼:
// 通過單例方法擷取 UIApplication 對象UIApplication *app = [UIApplication sharedApplication];// 設定appIcon提醒數字,iOS8之後必須註冊使用者通知app.applicationIconBadgeNumber = 10;// 建立使用者通知UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];// 註冊使用者的通知[app registerUserNotificationSettings:settings];
使用者通知
點擊 " 好 " 後就可以看到類似於QQ訊息數量的提醒,
點擊 " 不允許 "後就沒有,
2> 設定連網狀態
顯示連網狀態,告訴使用者此應用正在連網。
iOS標頭檔中的屬性聲明:
@property(nonatomic,getter=isNetworkActivityIndicatorVisible) BOOL networkActivityIndicatorVisible __TVOS_PROHIBITED; // showing network spinning gear in status bar. default is NO
執行個體代碼:
// 設定連網狀態 app.networkActivityIndicatorVisible = YES;
:
3> 開啟網頁
iOS標頭檔中的方法聲明:
- (BOOL)openURL:(NSURL*)url NS_EXTENSION_UNAVAILABLE_IOS("");
執行個體代碼:
#pragma mark - 開啟網頁- (IBAction)btnClick:(id)sender { // URL:資源路徑 // URL:協議頭://網域名稱+路徑 http,https,file,tel // 協議頭: // 開啟網頁 @"http://www.baidu.com" NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"]; [[UIApplication sharedApplication] openURL:url];}
UIApplication 開啟資源的好處:不用判斷用什麼軟體開啟,系統會自動根據協議頭判斷。
3. UIApplication 的代理方法
我們可以進入到 UIApplicationDelegate 的標頭檔看到它的代理方法有很多,常用的也就是你建立一個 Single View Application 工程中在 AppDelegate 中產生的,下面代碼就是,這些代理具體如何使用詳見代碼注釋。
執行個體代碼:
#import "AppDelegate.h"@interface AppDelegate ()@end@implementation AppDelegate// 學習代理方法,只需要知道這個方法什麼時候調用,這個方法可以用來幹嘛// 程式啟動完成後調用(常用)- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. NSLog(@"%s__%d", __FUNCTION__, __LINE__); return YES;}// 當app失去焦點的時候調用- (void)applicationWillResignActive:(UIApplication *)application { NSLog(@"%s__%d", __FUNCTION__, __LINE__); // 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.}// app進入背景時候調用(常用)// app忽然被打斷的時候,在這裡儲存一些需要用到的資料- (void)applicationDidEnterBackground:(UIApplication *)application { NSLog(@"%s__%d", __FUNCTION__, __LINE__); // 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.}// app即將進入前台的時候調用- (void)applicationWillEnterForeground:(UIApplication *)application { NSLog(@"%s__%d", __FUNCTION__, __LINE__); // 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.}// 當app擷取到焦點的時候調用,意味著app可以和使用者互動- (void)applicationDidBecomeActive:(UIApplication *)application { NSLog(@"%s__%d", __FUNCTION__, __LINE__); // 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.}// app被關閉的時候調用- (void)applicationWillTerminate:(UIApplication *)application { NSLog(@"%s__%d", __FUNCTION__, __LINE__); // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.}// app接受記憶體警告的時候被調用(常用)// 清空圖片緩衝 - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application{ NSLog(@"%s__%d", __FUNCTION__, __LINE__);}@end