0
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
此代碼是爲UILocalNotification
註冊,這也將彈出此幫助:需要更多關於UILocalNotification權限
問題1:在這種狀態下,當用戶還沒有選擇任何選項,怎麼辦當用戶選擇其中一個選項Don't Allow
或Ok
時,我會收到通知?所以我可以相應地執行應用。
-
UIUserNotificationSettings *current = [[UIApplication sharedApplication] currentUserNotificationSettings];
UIUserNotificationType required = UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
if(current.types & required) {
NSLog(@"Permission present: %lu", (unsigned long)current.types);
} else {
NSLog(@"Permission not present: %lu", (unsigned long)current.types);
}
當應用程序將啓動第一次後,使用此代碼我想取的權限用戶允許(也許他後藤設置和禁用所有類型的通知警報)。
問題2:我只是越來越號碼log
像7
爲我檢查的各類許可和0
如果用戶已經不允許了UILocalNotification
。如何正確檢查權限?
- (空)應用程序:(UIApplication的*)應用程序didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings。這將在用戶執行任何批准或拒絕操作時調用。 – Arjuna
對於檢查受支持的類型,您可以檢查是否((類型&UIRemoteNotificationTypeBadge)。也就是說,您可以使用UIUserNotificationSettings的types屬性對所需的類型進行按位AND操作。 – Arjuna