iOS 权限

iOS中的权限在官方概念中叫能力(capabilities)。它是app要拥有某些功能。目前在iOS11SDK中一共有22种。我们下面按照最为常用开始罗列,争取坚持写完。

  1. Push Notifications
    iOS 权限_第1张图片
    64C70E4CF32BF6F610B2BACFDBBFDAD6.jpg

    1.1 申请。首先在XCode中capabilities中找到Push Notification(如上图的部分)打开后面的开关。证书配置不提,代码申请,触发系统弹框。极光推送已经封装了申请部分,如果使用了极光推送不用手动申请。

     if ([UIDevice currentDevice].systemVersion.doubleValue < 8.0) {
         [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert];
     }else if([UIDevice currentDevice].systemVersion.doubleValue < 10.0){
         UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge| UIUserNotificationTypeSound|UIUserNotificationTypeAlert  categories:nil];
         [application registerUserNotificationSettings:settings];
         [application registerForRemoteNotifications];
     }else{
         if (@available(iOS 10.0, *)) {
             UNAuthorizationOptions options = UNAuthorizationOptionBadge|UNAuthorizationOptionSound|UNAuthorizationOptionAlert|UNAuthorizationOptionCarPlay;
             [[UNUserNotificationCenter currentNotificationCenter]requestAuthorizationWithOptions:options completionHandler:^(BOOL granted, NSError * _Nullable error) {
                 
             }];
         }
         [application registerForRemoteNotifications];
     }
    

    判断当前状态

     if (@available(iOS 10.0, *)) {//iOS 10 以后
         [[UNUserNotificationCenter currentNotificationCenter]getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
             UNNotificationSetting notificationSetting = settings.notificationCenterSetting;
             if (notificationSetting == UNNotificationSettingEnabled) {
                 
             }else if(notificationSetting == UNNotificationSettingDisabled){
                 
             }else{//UNNotificationSettingNotSupported
                 
             }
         }];
     } else {
         if (@available(iOS 8.0, *)) {
             if([[UIApplication sharedApplication] currentUserNotificationSettings].types == UIUserNotificationTypeNone){
                 
             }else{
                 
             }
         }else{
             if ([[UIApplication sharedApplication] enabledRemoteNotificationTypes]  == UIRemoteNotificationTypeNone){
                 
             }else{
                 
             }
         }
     }
    

    跳转设置

     if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]){
         [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
     }
    

你可能感兴趣的:(iOS 权限)