权限分类

  • 联网权限

  • 相册权限

  • 相机、麦克风权限

  • 定位权限

  • 推送权限

  • 通讯录权限

  • 日历、备忘录权限

联网权限

引入头文件 @import CoreTelephony;

应用启动后,检测应用中是否有联网权限

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
CTCellularData *cellularData = [[CTCellularData alloc]init];
cellularData.cellularDataRestrictionDidUpdateNotifier =  ^(CTCellularDataRestrictedState state){
  //获取联网状态
  switch (state) {
      case kCTCellularDataRestricted:
          NSLog(@"Restricrted");
          break;
      case kCTCellularDataNotRestricted:
          NSLog(@"Not Restricted");
          break;
      case kCTCellularDataRestrictedStateUnknown:
          NSLog(@"Unknown");
          break;
      default:
          break;
  };
};

查询应用是否有联网功能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
CTCellularData *cellularData = [[CTCellularData alloc]init];
CTCellularDataRestrictedState state = cellularData.restrictedState;
 switch (state) {
  case kCTCellularDataRestricted:
      NSLog(@"Restricrted");
      break;
  case kCTCellularDataNotRestricted:
      NSLog(@"Not Restricted");
      break;
  case kCTCellularDataRestrictedStateUnknown:
      NSLog(@"Unknown");
      break;
  default:
      break;
}

相册权限

iOS 9.0之前

导入头文件@import AssetsLibrary;

检查是否有相册权限

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
switch (status) {
  case ALAuthorizationStatusAuthorized:
      NSLog(@"Authorized");
      break;
  case ALAuthorizationStatusDenied:
      NSLog(@"Denied");
      break;
  case ALAuthorizationStatusNotDetermined:
      NSLog(@"not Determined");
      break;
  case ALAuthorizationStatusRestricted:
      NSLog(@"Restricted");
      break;
       
  default:
      break;
}

相册权限--iOS 8.0之后

导入头文件@import Photos;

检查是否有相册权限

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
PHAuthorizationStatus photoAuthorStatus = [PHPhotoLibrary authorizationStatus];
switch (photoAuthorStatus) {
  case PHAuthorizationStatusAuthorized:
      NSLog(@"Authorized");
      break;
  case PHAuthorizationStatusDenied:
      NSLog(@"Denied");
      break;
  case PHAuthorizationStatusNotDetermined:
      NSLog(@"not Determined");
      break;
  case PHAuthorizationStatusRestricted:
      NSLog(@"Restricted");
      break;
  default:
      break;
}

![Uploading 144446-b8aca7ba38c5f8c0_695906.png . . .]获取相册权限

1
2
3
4
5
6
7
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
  if (status == PHAuthorizationStatusAuthorized) {
      NSLog(@"Authorized");
  }else{
      NSLog(@"Denied or Restricted");
  }
  }];

相机和麦克风权限

导入头文件@import AVFoundation;

检查是否有相机或麦克风权限

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
AVAuthorizationStatus AVstatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];//相机权限
AVAuthorizationStatus AVstatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];//麦克风权限
 
switch (AVstatus) {
  case AVAuthorizationStatusAuthorized:
      NSLog(@"Authorized");
      break;
  case AVAuthorizationStatusDenied:
      NSLog(@"Denied");
      break;
  case AVAuthorizationStatusNotDetermined:
      NSLog(@"not Determined");
      break;
  case AVAuthorizationStatusRestricted:
      NSLog(@"Restricted");
      break;
  default:
      break;
}

获取相机或麦克风权限

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {//相机权限
  if (granted) {
      NSLog(@"Authorized");
  }else{
      NSLog(@"Denied or Restricted");
  }
}];
 
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {//麦克风权限
  if (granted) {
      NSLog(@"Authorized");
  }else{
      NSLog(@"Denied or Restricted");
  }
}];

定位权限

导入头文件@import CoreLocation;

由于iOS8.0之后定位方法的改变,需要在info.plist中进行配置;

配置文件

检查是否有定位权限

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 BOOL isLocation = [CLLocationManager locationServicesEnabled];
if (!isLocation) {
  NSLog(@"not turn on the location");
}
CLAuthorizationStatus CLstatus = [CLLocationManager authorizationStatus];
switch (CLstatus) {
  case kCLAuthorizationStatusAuthorizedAlways:
      NSLog(@"Always Authorized");
      break;
  case kCLAuthorizationStatusAuthorizedWhenInUse:
      NSLog(@"AuthorizedWhenInUse");
      break;
  case kCLAuthorizationStatusDenied:
      NSLog(@"Denied");
      break;
  case kCLAuthorizationStatusNotDetermined:
      NSLog(@"not Determined");
      break;
  case kCLAuthorizationStatusRestricted:
      NSLog(@"Restricted");
      break;
  default:
      break;
}

获取定位权限

1
2
3
CLLocationManager *manager = [[CLLocationManager alloc] init];
[manager requestAlwaysAuthorization];//一直获取定位信息
[manager requestWhenInUseAuthorization];//使用的时候获取定位信息

在代理方法中查看权限是否改变

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
 switch (status) {
  case kCLAuthorizationStatusAuthorizedAlways:
      NSLog(@"Always Authorized");
      break;
  case kCLAuthorizationStatusAuthorizedWhenInUse:
      NSLog(@"AuthorizedWhenInUse");
      break;
  case kCLAuthorizationStatusDenied:
      NSLog(@"Denied");
      break;
  case kCLAuthorizationStatusNotDetermined:
      NSLog(@"not Determined");
      break;
  case kCLAuthorizationStatusRestricted:
      NSLog(@"Restricted");
      break;
  default:
      break;
  }
   
}

推送权限

检查是否有通讯权限

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings];
switch (settings.types) {
  case UIUserNotificationTypeNone:
      NSLog(@"None");
      break;
  case UIUserNotificationTypeAlert:
      NSLog(@"Alert Notification");
      break;
  case UIUserNotificationTypeBadge:
      NSLog(@"Badge Notification");
      break;
  case UIUserNotificationTypeSound:
      NSLog(@"sound Notification'");
      break;
       
  default:
      break;
}

获取推送权限

1
2
UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:setting];

通讯录权限

iOS 9.0之前

导入头文件 @import AddressBook;

检查是否有通讯录权限

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
ABAuthorizationStatus ABstatus = ABAddressBookGetAuthorizationStatus();
switch (ABstatus) {
  case kABAuthorizationStatusAuthorized:
      NSLog(@"Authorized");
      break;
  case kABAuthorizationStatusDenied:
      NSLog(@"Denied'");
      break;
  case kABAuthorizationStatusNotDetermined:
      NSLog(@"not Determined");
      break;
  case kABAuthorizationStatusRestricted:
      NSLog(@"Restricted");
      break;
  default:
      break;
}

获取通讯录权限

1
2
3
4
5
6
7
8
9
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
  if (granted) {
      NSLog(@"Authorized");
      CFRelease(addressBook);
  }else{
      NSLog(@"Denied or Restricted");
  }
});

iOS 9.0及以后

导入头文件 @import Contacts;

检查是否有通讯录权限

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
  switch (status) {
        case CNAuthorizationStatusAuthorized:
        {
            NSLog(@"Authorized:");
        }
            break;
        case CNAuthorizationStatusDenied:{
            NSLog(@"Denied");
        }
            break;
        case CNAuthorizationStatusRestricted:{
            NSLog(@"Restricted");
        }
            break;
        case CNAuthorizationStatusNotDetermined:{
             NSLog(@"NotDetermined");
        }
            break;
             
       }

获取通讯录权限

1
2
3
4
5
6
7
8
9
10
CNContactStore *contactStore = [[CNContactStore alloc] init];
    [contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (granted) {
 
           NSLog(@"Authorized");
        }else{
 
           NSLog(@"Denied or Restricted");
        }
    }];

日历、备忘录权限

导入头文件

检查是否有日历或者备忘录权限

1
2
3
4
 typedef NS_ENUM(NSUInteger, EKEntityType) {
  EKEntityTypeEvent,//日历
  EKEntityTypeReminder //备忘
 };
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
EKAuthorizationStatus EKstatus = [EKEventStore  authorizationStatusForEntityType:EKEntityTypeEvent];
switch (EKstatus) {
  case EKAuthorizationStatusAuthorized:
      NSLog(@"Authorized");
      break;
  case EKAuthorizationStatusDenied:
      NSLog(@"Denied'");
      break;
  case EKAuthorizationStatusNotDetermined:
      NSLog(@"not Determined");
      break;
  case EKAuthorizationStatusRestricted:
      NSLog(@"Restricted");
      break;
  default:
      break;
}

获取日历或备忘录权限

1
2
3
4
5
6
7
8
EKEventStore *store = [[EKEventStore alloc]init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError * _Nullable error) {
  if (granted) {
      NSLog(@"Authorized");
  }else{
      NSLog(@"Denied or Restricted");
  }
}];

最后一点

素有获取权限的方法,多用于用户第一次操作应用,iOS 8.0之后,将这些设置都整合在一起,并且可以开启或关闭相应的权限。所有的权限都可以通过下面的方法打开:

1
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString

iOS开发中的权限的更多相关文章

  1. 再续iOS开发中的这些权限

    前言 上篇文章iOS开发中的这些权限,你搞懂了吗?介绍了一些常用权限的获取和请求方法,知道这些方法的使用基本上可以搞定大部分应用的权限访问的需求.但是,这些方法并不全面,不能涵盖住所有权限访问的方法. ...

  2. iOS开发中权限再度梳理

    前言 上篇文章iOS开发中的这些权限,你搞懂了吗?介绍了一些常用权限的获取和请求方法,知道这些方法的使用基本上可以搞定大部分应用的权限访问的需求.但是,这些方法并不全面,不能涵盖住所有权限访问的方法. ...

  3. ios开发中的小技巧

    在这里总结一些iOS开发中的小技巧,能大大方便我们的开发,持续更新. UITableView的Group样式下顶部空白处理 //分组列表头部空白处理 UIView *view = [[UIViewal ...

  4. fir.im Weekly - iOS开发中的Git流程

    本期 fir.im Weekly 收集了微博上的热转资源,包含 Android.iOS 开发工具.源码等好用的轮子,还有一些 APP 设计的 Tips,希望对你有用. 精仿知乎日报 iOS 端 @我偏 ...

  5. 在iOS开发中使用FMDB

    在iOS开发中使用FMDB 前言 SQLite (http://www.sqlite.org/docs.html) 是一个轻量级的关系数据库.iOS SDK 很早就支持了 SQLite,在使用时,只需 ...

  6. 【转】在iOS开发中使用FMDB

    本文转载自:唐巧的博客 在iOS开发中使用FMDB APR 22ND, 2012 前言 SQLite (http://www.sqlite.org/docs.html) 是一个轻量级的关系数据库.iO ...

  7. 总结iOS开发中的断点续传那些事儿

    前言 断点续传概述 断点续传就是从文件赏赐中断的地方重新开始下载或者上传数据,而不是从头文件开始.当下载大文件的时候,如果没有实现断点续传功能,那么每次出现异常或者用户主动的暂停,都会从头下载,这样很 ...

  8. iOS开发中静态库之".framework静态库"的制作及使用篇

    iOS开发中静态库之".framework静态库"的制作及使用篇 .framework静态库支持OC和swift .a静态库如何制作可参照上一篇: iOS开发中静态库之" ...

  9. iOS开发中静态库制作 之.a静态库制作及使用篇

    iOS开发中静态库之".a静态库"的制作及使用篇 一.库的简介 1.什么是库? 库是程序代码的集合,是共享程序代码的一种方式 2.库的类型? 根据源代码的公开情况,库可以分为2种类 ...

随机推荐

  1. BOM and Event Source

    EventSource: var test = function(){ var btn = event.srcElement; var str += btn.type + ":" ...

  2. PHP基础之POST与GET

    post 与 get区别 *.Post传输数据时,不需要在URL中显示出来,而Get方法要在URL中显示.*.Post传输的数据量大,可以达到2M,而Get方法由于受到URL长度的限制,只能传递大约1 ...

  3. Epub基础知识介绍

    转载自:http://www.cnblogs.com/linlf03/archive/2011/12/13/2286218.html 一.什么是epub epub是一个完全开放和免费的电子书标准.它可 ...

  4. Asp.net通过模板(.dot/Html)导出Word,同时导出图片

    一.Office组件导出Word(服务器配置麻烦) 需要引用Office的DLL,在下文的附件中,不同的Offic版本用的不一样,虽然高级版本可以兼容低级的,不过,还是统一版本最好 贴上核心代码(转载 ...

  5. Java(Helloworld.java)

    public class A{ public static void main(String args[]){ System.out.println("Hello world!") ...

  6. SQLite 创建自增长标识列

    SQLite Autoincrement(自动递增) SQLite 的 AUTOINCREMENT 是一个关键字,用于表中的字段值自动递增.我们可以在创建表时在特定的列名称上使用 AUTOINCREM ...

  7. 【JAVA】 UIMnager

    Java'中的几种Look and Feel 1.Metal风格 (默认) String lookAndFeel = "javax.swing.plaf.metal.MetalLookAnd ...

  8. Linux crontab 命令格式与详细例子

    基本格式 :* * * * * command分 时 日 月 周 命令 第1列表示分钟1-59 每分钟用*或者 */1表示第2列表示小时1-23(0表示0点)第3列表示日期1-31第4列表示月份1-1 ...

  9. [Android] 升级了新的android studio之后 发生如下的报错,The following classes could not be instantiated:

    The following classes could not be instantiated:- android.support.v4.widget.DrawerLayout (Open Class ...

  10. *HDU1053 哈夫曼编码

    Entropy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...