首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

iOS 應用圖標角標數字權限處理

     編輯:程式設計綜合問答

錯誤訊息

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:數字]。
  • 若用戶拒絕,需引導至「設定」手動開啟權限。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved