iOS 探讨之 玩转设备权限

原创 2016年08月30日 14:04:44

阐述

从前几年的棱镜门到最近的Apple与FBI关于解锁iPhone的争论,人们开始重视个人隐私地保护。Apple在用户保护隐私这方面做了很多工作,因此受到了广大用户的好评,但变相得增加对iOS开发者的要求(熟练地掌握设备权限)。下面我们开始探讨一下iOS设备中某些权限。(注:iPad、iTouch不含有某些功能)


总括

本文探讨方面包括以下权限:

- 联网权限
- 相册权限
- 相机、麦克风权限
- 定位权限
- 推送权限
- 通讯录权限
- 日历、备忘录权限


具体

1. 联网权限

依赖库

@import CoreTelephony; //联网

获取状态

// 获取当前网络状态
- (void)getTheCurrentNetEnvironment {
    CTCellularData *cellularData = [[CTCellularData alloc]init];
    cellularData.cellularDataRestrictionDidUpdateNotifier =  ^(CTCellularDataRestrictedState state){
        //获取联网状态
        switch (state) {
            case kCTCellularDataRestricted:
                NSLog(@"Update Restricrted");
                break;
            case kCTCellularDataNotRestricted:
                NSLog(@"Update Not Restricted");
                break;
            case kCTCellularDataRestrictedStateUnknown:
                NSLog(@"Update Unknown");
                break;
            default:
                break;
        };
    };


//    CTCellularData *cellularData = [[CTCellularData alloc]init];
    CTCellularDataRestrictedState state = cellularData.restrictedState;
    switch (state) {
        case kCTCellularDataRestricted:
            NSLog(@"Current Restricrted");
            break;
        case kCTCellularDataNotRestricted:
            NSLog(@"Current Not Restricted");
            break;
        case kCTCellularDataRestrictedStateUnknown:
            NSLog(@"Current Unknown");
            break;
        default:
            break;
    }
}

2. 相册权限

依赖库

@import AssetsLibrary; // 相册 iOS 6-9
@import Photos; // 相册 iOS 8+

获取状态

// 获取当前相册权限 iOS 6 - 9
- (void)getTheCurrentPhoteAlbumEnvironmentOld {
    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+
- (void)getTheCurrentPhoteAlbumEnvironment {
    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;
    }
}

设置权限

// 设置相册权限 iOS 8+
- (void)setTheCurrentPhoteAlbumEnvironment {
    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
        if (status == PHAuthorizationStatusAuthorized) {
            NSLog(@"Authorized");
        }else{
            NSLog(@"Denied or Restricted");
        }
    }];
}

3. 相机、麦克风权限

依赖库

@import AVFoundation; // 相机和麦克风权限

获取状态

// 获取当前相机权限
- (void)getTheCurrentCameraEnvironment {
    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;
    }
}
设置权限

// 设置当前相机权限
- (void)setTheCurrentCameraEnvironment {
    [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");
//        }
//    }];
}

4. 定位权限

依赖库

@import CoreLocation; // 定位权限
获取状态

// 获取当前地理位置权限
- (void)getTheCurrentLocationEnvironment {
    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;
    }
}

// 当前地理位置权限改变回调
- (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;
    }

}
设置权限

// 设置当前地理位置权限
- (void)setTheCurrentLocationEnvironment {
    CLLocationManager *manager = [[CLLocationManager alloc] init];
    [manager requestAlwaysAuthorization];//一直获取定位信息
    [manager requestWhenInUseAuthorization];//使用的时候获取定位信息
}

5. 推送权限

获取状态

// 获取当前推送权限
- (void)getTheCurrentNotificationEnvironment {
    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;
    }
}
设置权限
// 设置当前推送权限
- (void)setTheCurrentNotificationEnvironment {
    UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:setting];
}

6. 通讯录权限

依赖库

@import AddressBook; // 通讯录权限 iOS 9-
@import Contacts; // 通讯录权限 iOS 9+
获取状态

// 获取当前地址薄权限 iOS 9-
- (void)getTheCurrentAddressBookEnvironmentOld {

    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;
    }
}

// 获取当前地址薄权限 iOS 9+
- (void)getTheCurrentAddressBookEnvironment {

    CNAuthorizationStatus CNstatus = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
    switch (CNstatus) {
        case CNAuthorizationStatusAuthorized:
            NSLog(@"Authorized");
            break;
        case CNAuthorizationStatusDenied:
            NSLog(@"Denied'");
            break;
        case CNAuthorizationStatusNotDetermined:
            NSLog(@"not Determined");
            break;
        case CNAuthorizationStatusRestricted:
            NSLog(@"Restricted");
            break;
        default:
            break;
    }
}
设置权限

// 设置当前地址薄权限 iOS 9-
- (void)setTheCurrentAddressBookEnvironmentOld {

    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
    ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
        if (granted) {
            NSLog(@"Authorized");
            CFRelease(addressBook);
        }else{
            NSLog(@"Denied or Restricted");
        }
    });
}

// 设置当前地址薄权限 iOS 9+
- (void)setTheCurrentAddressBookEnvironment {
    CNContactStore *store = [[CNContactStore alloc] init];
    [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (granted) {
            NSLog(@"Authorized");
        }else{
            NSLog(@"Denied or Restricted");
        }
    }];
}

7. 日历、备忘录权限

依赖库

@import EventKit; // 日历、备忘录权限
获取状态

// 获取当前日历权限
- (void)getTheCurrentDayEventEnvironment {
    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;
    }
}
设置权限

// 设置当前日历权限
- (void)setTheCurrentDayEventEnvironment {
    EKEventStore *store = [[EKEventStore alloc]init];
    [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError * _Nullable error) {
        if (granted) {
            NSLog(@"Authorized");
        }else{
            NSLog(@"Denied or Restricted");
        }
    }];
}

8. 彩蛋: 跳转App权限界面

// 跳转至系统设置当前App权限界面
- (void)setTheCurrentAllEnvironment {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}

总结

希望大家能够玩转设备权限




如何获取iOS应用网络权限?

问题描述 在iOS 10下 ,首次进入应用时,会有询问是否允许网络连接权限的的弹窗,为更好进行用户交互,需要在打开应用时获取应用禁用网络权限状态(状态分为:未知、限制网络、未限制网络),客户端根据不同...

App开发权限

一、请求权限的方式 1.打开App时请求权限 2.告知用户授权益处后请求权限 3.在必要的情况下请求权限 4.先展示自定义对话框,同意后再请求权限 二、权限分类 1.联网权限 2...

CoreTelephony(简介)

使用CoreTelephony可以获取用户电话服务供应商的信息,以及当前蜂窝电话的状态。...

iOS开发中的这些权限

写在前面 APP开发避免不开系统权限的问题,如何在APP以更加友好的方式向用户展示系统权限,似乎也是开发过程中值得深思的一件事; 那如何提高APP获取iOS系统权限...

iOS,利用URL Schemes跳转到设置wifi、蜂窝移动网络页面

一. 跳转到“设置” //>=iOS8.0可用 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicatio...

linux学习之旅(六)&玩转权限

玩转权限,权限精详讲。

获取Root权限,让你玩转安卓

疑问一:Root权限是什么?  疑问二:获取Root权限有什么用?   疑问三:如何获得Root权限?   答疑一: Root是Linux等类UNIX系统中的超级管理员用户帐户,该帐户拥有整个系统...
  • cometwo
  • cometwo
  • 2012年08月30日 19:34
  • 1238

玩转并理解linux中的文件/目录的rwx权限

linux是一个相对安全的系统, 其中的权限更是无处不在。 在本文中, 我们来谈谈linux中的文件/目录的rwx权限。 为了简便起见, 我们仅仅以文件owner的rwx为例。         一...
  • stpeace
  • stpeace
  • 2015年05月06日 22:16
  • 3242

玩转设备远程,一张结构图看清楚如何实现PLC数据远程传输

玩转设备远程,一张结构图看清楚如何实现PLC数据远程传输
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:iOS 探讨之 玩转设备权限
举报原因:
原因补充:

(最多只允许输入30个字)