- 每一個應用都有自己的UIApplication對象,而且是單例的
- iOS程序創(chuàng)建的第一個對象就是UIApplication
- 通過
[UIApplication sharedApplication]可以獲得這個單例對象 - 一個iOS程序啟動后創(chuàng)建的第一個對象就是UIApplication對象
- 一個應用程序里面只能有一個UIApplication對象,且只有一個(通過代碼獲取兩個UIApplication對象,打印地址可以看出地址是相同的)。
- 利用UIApplication對象,能進行一些應用級別的操作。
1. 獲取
不能直接創(chuàng)建。由系統(tǒng)自動創(chuàng)建。但是可以獲取。
//獲取這個單例對象
UIApplication *app = [UIApplication sharedApplication];
//NS_EXTENSION_UNAVAILABLE_IOS("Use view controller based solutions where appropriate instead.");
@property(class, nonatomic, readonly) UIApplication *sharedApplication
2. 創(chuàng)建聯(lián)網(wǎng)指示器

Paste_Image.png
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// 獲取應用程序?qū)ο? UIApplication *app = [UIApplication sharedApplication];
// 設置聯(lián)網(wǎng)指示器
app.networkActivityIndicatorVisible = YES;
}
3. 設置app右上角小圖標的數(shù)字

Paste_Image.png
3.1 授權(quán)的說明
- 在使用此類獲取授權(quán)的時候可以看到如圖的提示。劃線表示已經(jīng)過期,蘋果不再建議使用。
- 官方建議使用UNNotificationSettings 替代。
- 黃色報警,只是說明系統(tǒng)不建議使用這個方法。因為iOS10之后過期了,但是考慮到iOS 10.0剛推出沒有多久,很多用戶還依然是使用iOS9,所以這個地方最好還是使用兼容iOS9的方法。

Paste_Image.png
最終代碼:
// 獲取應用程序?qū)ο? UIApplication *app = [UIApplication sharedApplication];
// 創(chuàng)建用戶通知設置
UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
// 注冊授權(quán)通知請求
[app registerUserNotificationSettings:setting];
// 設置APP右上角的提示數(shù)字
app.applicationIconBadgeNumber = 998;
3.2 設置提示數(shù)字
- 使用屬性applicationIconBadgeNumber進行賦值。
app.applicationIconBadgeNumber = 998;
- 對于這個屬性,官方說明如下:
// 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.
@property(nonatomic) NSInteger applicationIconBadgeNumber;
4. 打電話、發(fā)短信、上網(wǎng)
- 使用openURL的方法
// 打開百度
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
[app openURL:url];
// 打電話
NSURL *phone = [NSURL URLWithString:@"tel:10086"];
[app openURL:phone];
// 發(fā)短信
NSURL *sms = [NSURL URLWithString:@"sms:10086"];
[app openURL:sms];
// 發(fā)郵件
NSURL *email = [NSURL URLWithString:@"mailto:hah@22.com"];
[app openURL:email];
5. 判斷系統(tǒng)版本
- 如果使用的方法中又不兼容系統(tǒng)的,就需要提前判斷系統(tǒng)版本,從而使用不同的方法。
//獲取當前系統(tǒng)版本
[UIDevice currentDevice].systemVersion.floatValue