2015-11-12 37 views
0
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 

    if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){ 
     [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]]; 
    } 

此代碼是爲UILocalNotification註冊,這也將彈出此幫助:需要更多關於UILocalNotification權限

enter image description here

問題1:在這種狀態下,當用戶還沒有選擇任何選項,怎麼辦當用戶選擇其中一個選項Don't AllowOk時,我會收到通知?所以我可以相應地執行應用。

-

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:我只是越來越號碼log7爲我檢查的各類許可和0如果用戶已經不允許了UILocalNotification。如何正確檢查權限?

+0

- (空)應用程序:(UIApplication的*)應用程序didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings。這將在用戶執行任何批准或拒絕操作時調用。 – Arjuna

+0

對於檢查受支持的類型,您可以檢查是否((類型&UIRemoteNotificationTypeBadge)。也就是說,您可以使用UIUserNotificationSettings的types屬性對所需的類型進行按位AND操作。 – Arjuna

回答

1

我在我的一個項目中使用了下面的方法來確定用戶是否授予了權限,或者他/她是否真的在設置中關閉了通知。將這個方法的appdelegate和檢查

-(BOOL)notificationServicesEnabled { 
BOOL isEnabled = NO; 

if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)]){ 
    UIUserNotificationSettings *notificationSettings = [[UIApplication sharedApplication] currentUserNotificationSettings]; 

    if (!notificationSettings || (notificationSettings.types == UIUserNotificationTypeNone)) { 
     isEnabled = NO; 
    } else { 
     isEnabled = YES; 
    } 
} else { 
    UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; 
    if (types & UIRemoteNotificationTypeAlert) { 
     isEnabled = YES; 
    } else{ 
     isEnabled = NO; 
    } 
} 

    return isEnabled; 
} 

然後你可以簡單的檢查與條件

if([self notificationServicesEnabled])