天天看點

【原】iOS學習之UIApplication及其代理

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> 設定聯網狀态

  顯示聯網狀态,告訴使用者此應用正在聯網。  

@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      

繼續閱讀