iOS-解決iOS8及以上設置applicationIconBadgeNumber報錯的問題

 

在IOS7中設置applicationIconBadgeNumber不會有什麼問題,可是直接在IOS8中設置applicationIconBadgeNumber會報錯。由於在IOS8中要想設置applicationIconBadgeNumber,須要用戶的受權,在IOS8中,須要加上下面的代碼:app

UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];

可是這兩句若是是在IOS7的系統中運行的話就會報錯,因此須要先判斷IOS的版本,完整代碼以下:spa

float version = [[[UIDevice currentDevice] systemVersion] floatValue];
    
if (version >= 8.0) {
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}

若是是註冊消息推送的話,須要這樣寫:code

float version = [[[UIDevice currentDevice] systemVersion] floatValue];

if (version >= 8.0) {
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    [application registerForRemoteNotifications];
}
相關文章
相關標籤/搜索