iOS 關於 定位、相機、相簿許可權處理
阿新 • • 發佈:2018-11-08
蘋果AppStore對於私有API的檢測越來越嚴格,下邊給大家提供定位、相機、相簿的許可權處理方法,在這個封裝了一個工具類,感興趣的可以拿走。後續會繼續加入其他許可權,歡迎經常來逛逛。
LPDeviceManger.h
#import <Foundation/Foundation.h> @interface LPDeviceManager : NSObject /** 檢測使用者是否開啟位置許可權 */ +(BOOL)checkUserLocationAuth; /** 檢測使用者是否開啟相機許可權 */ +(BOOL)checkUserCameraAuth; /** 檢測使用者是否開啟相簿許可權 */ +(BOOL)checkUserPhotoAuth; @end
LPDeviceManger.m
#import <AVFoundation/AVFoundation.h> #import "LPDeviceManager.h" #import <AssetsLibrary/AssetsLibrary.h> @implementation LPDeviceManager /** 定位許可權 @return YES NO */ +(BOOL)checkUserLocationAuth { CLAuthorizationStatus author = [CLLocationManager authorizationStatus]; if (author == kCLAuthorizationStatusDenied || author == kCLAuthorizationStatusRestricted) { [self showAlertWithMessage:@"為了更好的體驗,請到設定->隱私->定位服務中開啟!"]; return NO; } return YES; } /** 相機許可權 @return YES NO */ +(BOOL)checkUserCameraAuth { AVAuthorizationStatus author = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; if (author == AVAuthorizationStatusRestricted || author == AVAuthorizationStatusDenied) { [self showAlertWithMessage:@"應用相機許可權受限,請在\"設定-隱私-相機\"中啟用"]; return NO; } return YES; } /** 相簿許可權 @return YES NO */ +(BOOL)checkUserPhotoAuth { ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus]; if (author == AVAuthorizationStatusRestricted || author == AVAuthorizationStatusDenied) { [self showAlertWithMessage:@"應用相簿許可權受限,請在\"設定-隱私-相簿\"中啟用"]; return NO; } return YES; } +(void)showAlertWithMessage:(NSString *)message { UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"溫馨提示" message:message preferredStyle:UIAlertControllerStyleAlert]; [alert addAction:[UIAlertAction actionWithTitle:@"去開啟" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSURL *settingURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; if ([[UIApplication sharedApplication] canOpenURL:settingURL]) { [[UIApplication sharedApplication] openURL:settingURL]; } }]]; [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]]; UIViewController *root = [UIApplication sharedApplication].delegate.window.rootViewController; [root presentViewController:alert animated:YES completion:nil]; } @end