iOS學習之UIApplication及其代理,iosuiapplication

來源:互聯網
上載者:User

iOS學習之UIApplication及其代理,iosuiapplication
1. 什麼是UIApplication

  • UIApplication 對象是應用程式的象徵,不能手動建立,不能 alloc init,一個應用程式只允許 一個 。

  • 每個應用都有自己的 UIApplication 對象,而且是單例。

  通過 [UIApplication shareApplication] 可以擷取這個單例對象。  

  弄成單例的原因:

   UIApplication 對象是用來設定應用全域資訊的,一個應用程式如果有很多 UIApplication 對象,都不知道聽誰的。

  • 一個iOS程式啟動後建立第一個對象就是 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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.