iOS四大對象之UIApplication對象

  • 每一個應用都有自己的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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,591評論 19 139
  • 當我修改了/etc/profile文件,我想讓它立刻生效,而不用重新登錄;這時就想到用source命令,如:sou...
    Ten_Minutes閱讀 806評論 0 1
  • 其一江友堂前偶爾見,漢春口中幾度聞。舊時坎中同校友,如今江淮大詩人。 其二昨日里名落孫山,只道是人杰地靈。今晚隨大...
    蔚海山莊三六子閱讀 568評論 0 2

友情鏈接更多精彩內(nèi)容