Attempting to badge the application icon but haven't received permission from the user to badge the application
iOS 8 起,應用若要在圖標右上角顯示紅色數字,必須先向用戶請求通知權限(包含 UIUserNotificationTypeBadge)。未獲授權直接設定 applicationIconBadgeNumber 會拋出上述錯誤。
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)
categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
AppDelegate 實作 application:didRegisterUserNotificationSettings: 回調,確認用戶允許後再設定 [[UIApplication sharedApplication] setApplicationIconBadgeNumber:數字]。