美文网首页
iOS 权限

iOS 权限

作者: CAICAI0 | 来源:发表于2018-03-19 14:41 被阅读142次

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

    1. Push Notifications 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 权限

          本文链接:https://www.haomeiwen.com/subject/qotpqftx.html