iOS 10 plist配置
描述字符串一定要填寫仗岖,不然會引發(fā)包無效的問題沈自,導(dǎo)致構(gòu)建版本不顯示
<!-- 相冊 -->
<key>NSPhotoLibraryUsageDescription</key>
<string>App需要您的同意,才能訪問相冊</string>
<!-- 相機(jī) -->
<key>NSCameraUsageDescription</key>
<string>App需要您的同意,才能訪問相機(jī)</string>
<!-- 麥克風(fēng) -->
<key>NSMicrophoneUsageDescription</key>
<string>App需要您的同意,才能訪問麥克風(fēng)</string>
<!-- 位置 -->
<key>NSLocationUsageDescription</key>
<string>App需要您的同意,才能訪問位置</string>
<!-- 在使用期間訪問位置 -->
<key>NSLocationWhenInUseUsageDescription</key>
<string>App需要您的同意,才能在使用期間訪問位置</string>
<!-- 始終訪問位置 -->
<key>NSLocationAlwaysUsageDescription</key>
<string>App需要您的同意,才能始終訪問位置</string>
<!-- 日歷 -->
<key>NSCalendarsUsageDescription</key>
<string>App需要您的同意,才能訪問日歷</string>
<!-- 提醒事項 -->
<key>NSRemindersUsageDescription</key>
<string>App需要您的同意,才能訪問提醒事項</string>
<!-- 運動與健身 -->
<key>NSMotionUsageDescription</key>
<string>App需要您的同意,才能訪問運動與健身</string>
<!-- 健康更新 -->
<key>NSHealthUpdateUsageDescription</key>
<string>App需要您的同意,才能訪問健康更新</string>
<!-- 健康分享 -->
<key>NSHealthShareUsageDescription</key>
<string>App需要您的同意,才能訪問健康分享</string>
<!-- 藍(lán)牙 -->
<key>NSBluetoothPeripheralUsageDescription</key>
<string>App需要您的同意,才能訪問藍(lán)牙</string>
<!-- 媒體資料庫 -->
<key>NSAppleMusicUsageDescription</key>
<string>App需要您的同意,才能訪問媒體資料庫</string>
如果不起作用,可以請求后臺權(quán)限,類似于這樣:
<key>UIBackgroundModes</key>
<array>
<!-- 在這里寫上你在后臺模式下要使用權(quán)限對應(yīng)的key -->
<string>location</string>
...
</array>
鑒定權(quán)限
鑒定權(quán)限是很有必要的,防止用戶關(guān)閉權(quán)限后出現(xiàn)crash
DDYAuthorityMaster.h
// Use : [DDYAuthorityMaster checkCameraAuthority]
#import <Foundation/Foundation.h>
typedef void (^AuthorizedFinishBlock)();
@interface DDYAuthorityMaster : NSObject
#pragma mark - 攝像頭權(quán)限
+ (void)cameraAuthorizedSuccess:(AuthorizedFinishBlock)success fail:(AuthorizedFinishBlock)fail;
#pragma mark - 麥克風(fēng)權(quán)限
+ (void)audioAuthorizedSuccess:(AuthorizedFinishBlock)success fail:(AuthorizedFinishBlock)fail;
#pragma mark - 相冊權(quán)限
+ (void)albumAuthorizedSuccess:(AuthorizedFinishBlock)success fail:(AuthorizedFinishBlock)fail;
#pragma mark - 推送通知權(quán)限
+ (void)pushNotificationAuthorizedSuccess:(AuthorizedFinishBlock)success fail:(AuthorizedFinishBlock)fail;
#pragma mark - 推送通知權(quán)限
+ (void)locationAuthorizedSuccess:(AuthorizedFinishBlock)success fail:(AuthorizedFinishBlock)fail;
#pragma mark - 通訊錄權(quán)限
+ (void)AddressBookAuthorizedSuccess:(AuthorizedFinishBlock)success fail:(AuthorizedFinishBlock)fail;
/** 麥克風(fēng)權(quán)限 */
+ (void)audioAuthSuccess:(AuthorityBlock)success fail:(AuthorityBlock)fail alertShow:(BOOL)show;
/** 攝像頭權(quán)限 */
+ (void)cameraAuthSuccess:(AuthorityBlock)success fail:(AuthorityBlock)fail alertShow:(BOOL)show;
/** 相冊使用權(quán)限 */
+ (void)albumAuthSuccess:(AuthorityBlock)success fail:(AuthorityBlock)fail alertShow:(BOOL)show;
/** 推送通知權(quán)限 */
+ (void)pushNotificationAuthSuccess:(AuthorityBlock)success fail:(AuthorityBlock)fail alertShow:(BOOL)show;
/** 通訊錄權(quán)限 */
+ (void)contactsAuthSuccess:(AuthorityBlock)success fail:(AuthorityBlock)fail alertShow:(BOOL)show;
/** 定位權(quán)限 */
+ (void)locationAuthSuccess:(AuthorityBlock)success fail:(AuthorityBlock)fail alertShow:(BOOL)show;
@end
DDYAuthorityMaster.m
/*
* 不彈出提示或需要在提示框增加詳細(xì)描述的,需手動在info.plist加一些字段
* NSLocationWhenInUseUsageDescription 位置權(quán)限 使用期間 狀態(tài)
* NSLocationAlwaysUsageDescription 位置權(quán)限 始終 狀態(tài)
*
*/
#import "DDYAuthorityMaster.h"
#import "DDYAppDelegate.h"
#define kAPPName [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"]
#define CancelText NSLocalizedString(@"Cancel", nil)
#define OKText NSLocalizedString(@"OK", nil)
#define OpenText NSLocalizedString(@"Open Setting", nil)
#define CancelText NSLocalizedString(@"Cancel", nil)
#define CameraText NSLocalizedString(@"Camera", nil)
#define AudioText NSLocalizedString(@"Microphone", nil)
#define AlbumText NSLocalizedString(@"Album", nil)
#define LocationText NSLocalizedString(@"Location", nil)
#define ContactText NSLocalizedString(@"AddressBook", nil)
@import AVFoundation;
@import CoreLocation;
@import AddressBook; // 通訊錄 iOS 9-
@import Contacts; // 通訊錄 iOS 9+
@import AssetsLibrary; // 相冊 iOS 6-9
@import Photos; // 相冊 iOS 8+
@implementation DDYAuthorityMaster
#pragma mark 私有方法
+ (BOOL)checkAuthority:(AVAuthorizationStatus)status {
return (status == AVAuthorizationStatusAuthorized) || (status == AVAuthorizationStatusNotDetermined);
}
#pragma mark 彈窗提示無權(quán)限
+ (void)showAlertController:(AuthorizedFinishBlock)block device:(NSString *)device {
UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"沒有權(quán)限" message:[NSString stringWithFormat:@"請開啟‘%@’對 %@ 的使用權(quán)限",kAPPName,device] preferredStyle:UIAlertControllerStyleAlert];
[alertC addAction:[UIAlertAction actionWithTitle:CancelText style:UIAlertActionStyleCancel handler:nil]];
[alertC addAction:[UIAlertAction actionWithTitle:OKText style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}]];
[((DDYAppDelegate *)[UIApplication sharedApplication].delegate).window.rootViewController presentViewController:alertC animated:YES completion:block];
}
#pragma mark iOS10以下可以跳轉(zhuǎn)系統(tǒng)設(shè)置
+ (void)showNotificationAlertController:(AuthorizedFinishBlock)block {
UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"Push Notification Are Off" message:[NSString stringWithFormat:@"Don't miss out on messages from friends.Go\"Setting->Notifications\"to open"] preferredStyle:UIAlertControllerStyleAlert];
[alertC addAction:[UIAlertAction actionWithTitle:CancelText style:UIAlertActionStyleCancel handler:nil]];
[alertC addAction:[UIAlertAction actionWithTitle:OpenText style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}]];
[((DDYAppDelegate *)[UIApplication sharedApplication].delegate).window.rootViewController presentViewController:alertC animated:YES completion:block];
}
#pragma mark 攝像頭權(quán)限
+ (BOOL)checkCameraAuthority {
return [self checkAuthority:[AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]];
}
+ (void)cameraAuthorizedSuccess:(AuthorizedFinishBlock)success fail:(AuthorizedFinishBlock)fail;{
if ([self checkCameraAuthority]) {
if (success) {
success();
}
}else{
[self showAlertController:fail device:CameraText];
}
}
#pragma mark 麥克風(fēng)權(quán)限
+ (BOOL)checkAudioAuthority {
return [self checkAuthority:[AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio]];
}
+ (void)audioAuthorizedSuccess:(AuthorizedFinishBlock)success fail:(AuthorizedFinishBlock)fail{
if ([self checkAudioAuthority]) {
if (success) {
success();
}
}else{
[self showAlertController:fail device:AudioText];
}
}
#pragma mark 相冊權(quán)限
+ (BOOL)checkAlbumAuthority {
return [ALAssetsLibrary authorizationStatus] == ALAuthorizationStatusAuthorized;
}
+ (void)albumAuthorizedSuccess:(AuthorizedFinishBlock)success fail:(AuthorizedFinishBlock)fail {
if ([self checkAlbumAuthority]) {
if (success) {
success();
}
}else{
[self showAlertController:fail device:AlbumText];
}
}
#pragma mark 位置權(quán)限
+ (BOOL)checkLocationAuthority {
return [CLLocationManager locationServicesEnabled];
}
+ (void)locationAuthorizedSuccess:(AuthorizedFinishBlock)success fail:(AuthorizedFinishBlock)fail {
if ([self checkLocationAuthority]) {
if (success) {
success();
}
}else{
[self showAlertController:fail device:LocationText];
}
}
#pragma mark 推送通知權(quán)限
+ (BOOL)checkPushNotificationAuthority {
return [[UIApplication sharedApplication] currentUserNotificationSettings].types != UIUserNotificationTypeNone;
}
+ (void)pushNotificationAuthorizedSuccess:(AuthorizedFinishBlock)success fail:(AuthorizedFinishBlock)fail {
if ([self checkPushNotificationAuthority]) {
if (success) {
success();
}
}else{
[self showNotificationAlertController:fail];
}
}
#pragma mark 通訊錄權(quán)限
+ (BOOL)checkAddressBookAuthority {
return ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized || ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined;
}
+ (void)AddressBookAuthorizedSuccess:(AuthorizedFinishBlock)success fail:(AuthorizedFinishBlock)fail{
if ([self checkAddressBookAuthority]) {
if (success) {
success();
}
} else {
[self showAlertController:fail device:ContactText];
}
}
#pragma mark 默認(rèn)無權(quán)限提示
+ (void)showAlertWithAppName:(NSString *)appName device:(NSString *)device
{
UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"沒有權(quán)限" message:[NSString stringWithFormat:@"請開啟‘%@’對 %@ 的使用權(quán)限",appName,device] preferredStyle:UIAlertControllerStyleAlert];
[alertC addAction:[UIAlertAction actionWithTitle:CancelText style:UIAlertActionStyleCancel handler:nil]];
[alertC addAction:[UIAlertAction actionWithTitle:OKText style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}]];
[((DDYAppDelegate *)[UIApplication sharedApplication].delegate).window.rootViewController presentViewController:alertC animated:YES completion:nil];
}
#pragma mark 麥克風(fēng)權(quán)限
+ (void)audioAuthSuccess:(AuthorityBlock)success fail:(AuthorityBlock)fail alertShow:(BOOL)show
{
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
switch (authStatus)
{
case AVAuthorizationStatusNotDetermined: // 有沒有詢問過還否開啟麥克風(fēng)權(quán)限(用戶未確定過)
{
[[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
if (granted)
{
if (success)
{
success();
}
}
else
{
if (show)
{
[self showAlertWithAppName:@"測試APP" device:@"麥克風(fēng)"];
}
if (fail)
{
fail();
}
}
}];
}
break;
case AVAuthorizationStatusRestricted: // 未授權(quán),且用戶無法更新,如家長控制情況下
case AVAuthorizationStatusDenied: // 用戶拒絕App使用該權(quán)限
if (show)
{
[self showAlertWithAppName:@"測試APP" device:@"麥克風(fēng)"];
}
if (fail)
{
fail();
}
break;
case AVAuthorizationStatusAuthorized: // 已授權(quán)
if (success)
{
success();
}
break;
}
}
#pragma mark 攝像頭權(quán)限
+ (void)cameraAuthSuccess:(AuthorityBlock)success fail:(AuthorityBlock)fail alertShow:(BOOL)show
{
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
switch (authStatus)
{
case AVAuthorizationStatusNotDetermined:
{
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if (granted)
{
NSLog(@"我點擊了授權(quán)");
if (success)
{
success();
}
}
else
{
NSLog(@"我點擊了拒絕");
if (show)
{
[self showAlertWithAppName:@"測試APP" device:@"攝像頭"];
}
if (fail)
{
fail();
}
}
}];
}
break;
case AVAuthorizationStatusRestricted:
case AVAuthorizationStatusDenied:
if (show)
{
[self showAlertWithAppName:@"測試APP" device:@"攝像頭"];
}
if (fail)
{
fail();
}
break;
case AVAuthorizationStatusAuthorized:
if (success)
{
success();
}
break;
}
}
#pragma mark 相冊權(quán)限
+ (void)albumAuthSuccess:(AuthorityBlock)success fail:(AuthorityBlock)fail alertShow:(BOOL)show
{
// ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus]; // 6-9
PHAuthorizationStatus authStatus = [PHPhotoLibrary authorizationStatus]; // 8+
switch (authStatus)
{
case AVAuthorizationStatusNotDetermined:
{
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
if (status == PHAuthorizationStatusAuthorized)
{
if (success)
{
success();
}
}
else
{
if (show)
{
[self showAlertWithAppName:@"測試APP" device:@"相冊"];
}
if (fail)
{
fail();
}
}
}];
}
break;
case AVAuthorizationStatusRestricted:
case AVAuthorizationStatusDenied:
if (show)
{
[self showAlertWithAppName:@"測試APP" device:@"相冊"];
}
if (fail)
{
fail();
}
break;
case AVAuthorizationStatusAuthorized:
if (success)
{
success();
}
break;
}
}
#pragma mark 推送通知權(quán)限
+ (void)pushNotificationAuthSuccess:(AuthorityBlock)success fail:(AuthorityBlock)fail alertShow:(BOOL)show
{
UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings];
switch (settings.types)
{
case UIUserNotificationTypeNone:
{
UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:setting];
}
break;
case UIUserNotificationTypeBadge:
case UIUserNotificationTypeSound:
case UIUserNotificationTypeAlert:
if (success)
{
success();
}
break;
}
}
#pragma mark 通訊錄權(quán)限
+ (void)contactsAuthSuccess:(AuthorityBlock)success fail:(AuthorityBlock)fail alertShow:(BOOL)show
{
ABAuthorizationStatus authStatus = ABAddressBookGetAuthorizationStatus();
switch (authStatus)
{
case AVAuthorizationStatusNotDetermined:
{
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
if (granted)
{
if (success)
{
success();
}
}
else
{
if (show)
{
[self showAlertWithAppName:@"測試APP" device:@"通訊錄"];
}
if (fail)
{
fail();
}
}
});
}
break;
case AVAuthorizationStatusRestricted:
case AVAuthorizationStatusDenied:
if (show)
{
[self showAlertWithAppName:@"測試APP" device:@"通訊錄"];
}
if (fail)
{
fail();
}
break;
case AVAuthorizationStatusAuthorized:
if (success)
{
success();
}
break;
}
}
#pragma mark 定位權(quán)限
+ (void)locationAuthSuccess:(AuthorityBlock)success fail:(AuthorityBlock)fail alertShow:(BOOL)show
{
BOOL isLocation = [CLLocationManager locationServicesEnabled];
if (isLocation)
{
NSLog(@"not turn on the location");
if (success)
{
success();
}
}
else
{
CLLocationManager *manager = [[CLLocationManager alloc] init];
// [manager requestAlwaysAuthorization];//一直獲取定位信息
[manager requestWhenInUseAuthorization];//使用的時候獲取定位信息
// manager.delegate = self;
}
CLAuthorizationStatus CLstatus = [CLLocationManager authorizationStatus];
switch (CLstatus)
{
case kCLAuthorizationStatusAuthorizedAlways:
NSLog(@"Always Authorized");
break;
case kCLAuthorizationStatusAuthorizedWhenInUse:
NSLog(@"AuthorizedWhenInUse");
break;
case kCLAuthorizationStatusDenied:
{
NSLog(@"Denied");
if (show)
{
[self showAlertWithAppName:@"測試APP" device:@"定位"];
}
if (fail)
{
fail();
}
}
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;
}
}
@end
權(quán)限判定 + 部分在未詢問時主動申請 點個星星祝福你
碼農(nóng)不易點星星 scan code
附
iOS 定位服務(wù)寨昙、通訊錄、日歷掀亩、提醒事項舔哪、照片、藍(lán)牙共享槽棍、麥克風(fēng)捉蚤、相機(jī)等授權(quán)檢測
iOS10 隱私權(quán)限設(shè)置問題