ios 判斷用戶是否開啟權限---并跳轉“系統設置”
1.判斷 訪問相冊 或 相機 權限是否開啟
2.檢測是否開啟定位
后面將持續更新
只有在應用請求過位置權限 或者 通知權限的時候,才會跳進自己app里面的設置呢。不然直接跳到系統設置界面
//打開app定位設置
NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if([[UIApplication sharedApplication] canOpenURL:settingsURL]) {
[[UIApplication sharedApplication] openURL:settingsURL];
}
工具/原料 Mac / Mac miniXcode方法/步驟? 1.? =====判斷 訪問相冊 或 相機 權限是否開啟====//在info.plist 里面設置? NSCameraUsageDescription Privacy - Camera Usage Description? ? ? App需要您的同意,才能訪問相機 NSPhotoLibraryUsageDescriptionPrivacy - Photo Library Usage Description? App需要您的同意,才能訪問相冊 科普://=================相冊=======================//相冊權限判斷 需要引入框架#import//相冊
===PHAuthorizationStatus===相冊權限狀態判斷
在8.0系統以后,新加入了Photos.framework框架,我們可以利用框架中的PHAuthorizationStatus進行相冊權限狀態判斷。
==判斷是否開啟相冊權限 的4中狀態
typedef NS_ENUM(NSInteger,PHAuthorizationStatus) {
//==1. 用戶還沒有關于這個應用程序做出了選擇
PHAuthorizationStatusNotDetermined = 0,
//==2. 這個應用程序未被授權訪問圖片數據。用戶不能更改該應用程序的狀態,可能是由于活動的限制,如家長控制到位。
PHAuthorizationStatusRestricted,
//==3. 用戶已經明確否認了這個應用程序訪問圖片數據
PHAuthorizationStatusDenied,
//==4. 用戶授權此應用程序訪問圖片數據
PHAuthorizationStatusAuthorized
}PHOTOS_AVAILABLE_IOS_TVOS(8_0, 10_0);
//========================相機===================== //相冊權限判斷 需要引入框架#import#import//=======AVAuthorizationStatus======判斷是否開啟相機權限 的4中狀態typedef NS_ENUM(NSInteger, AVAuthorizationStatus) {? ? ? ? ? //1. 表明用戶尚未選擇關于客戶端是否可以訪問硬件? ? ? ? AVAuthorizationStatusNotDetermined = 0,? ? ? ? //2. 客戶端未被授權訪問硬件的媒體類型。用戶不能改變客戶機的狀態,可能由于活躍的限制,如家長控制? ? ? ? AVAuthorizationStatusRestricted,? ? ? ? //3. 明確拒絕用戶訪問硬件支持的媒體類型的客戶? ? ? AVAuthorizationStatusDenied,? ? ? //4. 客戶端授權訪問硬件支持的媒體類型? ? ? AVAuthorizationStatusAuthorized? } NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED; =================使用==================== //選擇從相冊獲取圖片//判斷狀態? 如果已經授權 則從相冊選取相片? ? ? ? ? ? ? ? ? 如果沒有授權? 則跳轉到授權設置界面? //選擇從相機獲取圖片//判斷狀態? 如果已經授權 則打開攝像頭? ? ? ? ? ? ? ? ? 如果沒有授權? 則跳轉到授權設置界面? //引入下面的框架? //相冊權限判斷 需要引入框架#import//相冊 //相冊權限判斷 需要引入框架#import#import【注意】? 控制器要遵循的協議相冊//自定義的枚舉
typedef NS_ENUM(NSInteger, ChosePhontType) {
ChosePhontTypeAlbum,? //相冊
ChosePhontTypeCamera? //相機
};
//下面部分可以直接粘貼復制使用
-(void)clickHeaderImageView{? //點擊頭像
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"選擇相片" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *album = [UIAlertAction actionWithTitle:@"相冊" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self chosePhoto:ChosePhontTypeAlbum]; //從系統相冊選擇照片
}];
UIAlertAction *camera = [UIAlertAction actionWithTitle:@"相機" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self chosePhoto:ChosePhontTypeCamera]; //相機
}];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
//
}];
[alert addAction:album];
[alert addAction:camera];
[alert addAction:cancel];
[self presentViewController:alert animated:YES completion:^{
}];
}
//==========訪問系統? 相冊 / 相機? ===============
- (void)chosePhoto:(ChosePhontType)type{
UIImagePickerController *piker = [[UIImagePickerController alloc] init];
piker.delegate = self;
piker.allowsEditing = YES;
if (type == ChosePhontTypeAlbum) {? // 相冊
//======判斷 訪問相冊 權限是否開啟=======
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
//有被授權訪問的照片數據? 用戶已經明確否認了這一照片數據的應用程序訪問
if (status == PHAuthorizationStatusRestricted ||
status == PHAuthorizationStatusDenied) {
//====沒有權限====
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"去開啟訪問相冊權限?" message:nil preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}];
UIAlertAction *ok = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
//===無權限 引導去開啟===
[self openJurisdiction];
}];
// 將UIAlertAction添加到UIAlertController中
[alertController addAction:cancel];
[alertController addAction:ok];
// present顯示
[self presentViewController:alertController animated:YES completion:nil];
}else{? ? //====有訪問相冊的權限=======
piker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
}else if (type == ChosePhontTypeCamera) {? // 相機
//======判斷 訪問相機 權限是否開啟=======
AVAuthorizationStatus authStatus =? [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
//===無權限====
if (authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied){
//====沒有權限====
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"去開啟訪問相機權限?" message:nil preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}];
UIAlertAction *ok = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
//===無權限 引導去開啟===
[self openJurisdiction];
}];
// 將UIAlertAction添加到UIAlertController中
[alertController addAction:cancel];
[alertController addAction:ok];
// present顯示
[self presentViewController:alertController animated:YES completion:nil];
}else{? //===有權限======
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {? //相機可用
piker.sourceType = UIImagePickerControllerSourceTypeCamera;
}else{? // 相機不可用
[SVProgressHUD showErrorWithStatus:@"相機不可用"];
return;
}
}
}
[self presentViewController:piker animated:YES completion:^{
}];
}
#pragma mark-------去設置界面開啟權限----------
-(void)openJurisdiction{
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
}
#pragma mark UIImagePickerController回調方法================
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { //選取的照片
//選取的照片
UIImage *image = info[UIImagePickerControllerEditedImage];
_tableViewHeaderView.headerV.image = image;
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { //取消選擇
[self dismissViewControllerAnimated:YES completion:nil];
}
//2.=======檢測是否開啟定位======? //在info.plist 里面配置NSLocationWhenInUseUsageDescriptionPrivacy - Location When In Use Usage Description? ? App需要使用定位功能 NSLocationAlwaysUsageDescriptionPrivacy - Location Always Usage Description? ? App需要使用定位功能 引入框架? #import//定位遵循協議//=========CLAuthorizationStatus=========typedef NS_ENUM(int, CLAuthorizationStatus) {? ? ? ? //定位服務授權狀態是用戶沒有決定是否使用定位服務? ? ? ? kCLAuthorizationStatusNotDetermined = 0,? ? ? ? //定位服務授權狀態是受限制的。可能是由于活動限制定位服務,用戶不能改變。這個狀態可能不是用戶拒絕的定位服務? ? ? ? kCLAuthorizationStatusRestricted,? ? ? ? //定位服務授權狀態已經被用戶明確禁止,或者在設置里的定位服務中關閉? ? ? ? kCLAuthorizationStatusDenied,? ? ? ? ? //定位服務授權狀態已經被用戶允許在任何狀態下獲取位置信息。包括監測區域、訪問區域、或者在有顯著的位置變化的時候? ? ? ? kCLAuthorizationStatusAuthorizedAlways NS_ENUM_AVAILABLE(10_12, 8_0),? ? ? ? //定位服務授權狀態僅被允許在使用應用程序的時候? ? ? ? kCLAuthorizationStatusAuthorizedWhenInUse NS_ENUM_AVAILABLE(NA, 8_0),? ? ? ? //已被廢棄? ? ? ? kCLAuthorizationStatusAuthorized NS_ENUM_DEPRECATED(10_6, NA, 2_0, 8_0, "Use kCLAuthorizationStatusAuthorizedAlways") __TVOS_PROHIBITED __WATCHOS_PROHIBITED = kCLAuthorizationStatusAuthorizedAlways? ? }; 【注意】//1.//判斷定位是否開啟是判斷的整個手機系統的定位是否打開,并不是針對這一應用//[CLLocationManager locationServicesEnabled] //跳轉到? 整個手機系統的“定位”設置界面 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"]]; //2.//跳轉至 系統的權限設置界面 NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];? ? ? ? [[UIApplication sharedApplication] openURL:settingsURL];? //================使用=================引入框架? #import//定位遵循協議//當前狀態CLAuthorizationStatus status = [CLLocationManager authorizationStatus];if ([CLLocationManager locationServicesEnabled] && [CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied) {? ? ? ? ? //定位開啟? ? }? //全局變量CLLocationManager * locationManager; NSString * currentCity; //當前城市 NSString *prv; //當前省? -(void)addLocation{? //開始定位? ? ? ? //判斷定位是否開啟是判斷的整個手機系統的定位是否打開,并不是針對這一應用? ? //判斷定位功能是否打開? ? if ([CLLocationManager locationServicesEnabled]) {? ? ? ? ? ? ? ? locationManager = [[CLLocationManager alloc] init];? ? ? ? locationManager.delegate = self;? //遵循協議? ? ? ? //精確定位? ? ? ? locationManager.desiredAccuracy = kCLLocationAccuracyBest;? ? ? ? [locationManager requestWhenInUseAuthorization];? //使用時定位? ? ? ? currentCity = [[NSString alloc] init];? ? ? ? [locationManager startUpdatingLocation];? //開始定位? ? }}#pragma mark CoreLocation delegate----- 定位---- //定位失敗則執行此代理方法//定位失敗彈出提示框,點擊"打開定位"按鈕,會打開系統的設置,提示打開定位服務- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {? ? ? ? UIAlertController * alertVC = [UIAlertController alertControllerWithTitle:@"允許\"定位\"提示" message:@"請在設置中打開定位" preferredStyle:UIAlertControllerStyleAlert];? ? UIAlertAction * ok = [UIAlertAction actionWithTitle:@"打開定位" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {? ? ? ? ? ? ? ? //打開app定位設置? ? ? ? NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];? ? ? ? [[UIApplication sharedApplication] openURL:settingsURL];? ? }];? ? UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {? ? ? ? ? ? }];? ? [alertVC addAction:cancel];? ? [alertVC addAction:ok];? ? [self presentViewController:alertVC animated:YES completion:nil];? ? }//定位成功- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray*)locations {? ? //? ? [locationManager stopUpdatingLocation];? ? CLLocation *currentLocation = [locations lastObject];? ? CLGeocoder * geoCoder = [[CLGeocoder alloc] init];? ? ? ? //反編碼? ? [geoCoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray* _Nullable placemarks, NSError * _Nullable error) {? ? ? ? if (placemarks.count > 0) {? ? ? ? ? ? CLPlacemark *placeMark = placemarks[0];? ? ? ? ? ? currentCity = placeMark.locality;? ? ? ? ? ? if (!currentCity) {? ? ? ? ? ? ? ? currentCity = @"無法定位當前城市";? ? ? ? ? ? }? ? ? ? ? ? NSLog(@"%@",currentCity); //這就是當前的城市? ? ? ? ? ? NSLog(@"%@",placeMark.name);//具體地址:? xx市xx區xx街道? ? ? ? ? ? //administrativeArea? 省? ? ? ? ? ? NSLog(@"%@",placeMark.administrativeArea);? ? ? ? }? ? ? ? else if (error == nil && placemarks.count == 0) {? ? ? ? ? ? NSLog(@"No location and error return");? ? ? ? }? ? ? ? else if (error) {? ? ? ? ? ? NSLog(@"location error: %@ ",error);? ? ? ? }? ? ? ? ? ? }];}? //3.=======檢測是否允許消息推送======#import//====方法一
+ (BOOL)isAllowedNotification {
//
if ([UIDevice isSystemVersioniOS8]) {? // >= ios8
// system is iOS8
UIUserNotificationSettings *setting = [[UIApplication sharedApplication? ] currentUserNotificationSettings];
if (UIUserNotificationTypeNone != setting.types) {
return YES;
}
} else {//iOS7
UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if(UIRemoteNotificationTypeNone != type) {
return YES;
}else
}
return NO;
}
+ (BOOL)isSystemVersioniOS8 {
//check systemVerson of device
UIDevice *device = [UIDevice currentDevice];
float sysVersion = [device.systemVersion floatValue];
if (sysVersion >= 8.0f) {
return YES;
}
return NO;
}
//====方法二
if ([[UIDevice currentDevice].systemVersion floatValue]>=8.0f) {
UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
if (UIUserNotificationTypeNone == setting.types) {
NSLog(@"推送關閉");
}else{
NSLog(@"推送打開");
}
}else{
UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if(UIRemoteNotificationTypeNone == type){
NSLog(@"推送關閉");
}else{
NSLog(@"推送打開");
}
}
// 去設置
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
//===方法三
+ (void)isOpenMessageNotificationServiceWithBlock:(ReturnBlock)returnBlock
{
BOOL isOpen = NO;
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0
UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
if (setting.types != UIUserNotificationTypeNone) {
isOpen = YES;
}
#else
UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (type != UIRemoteNotificationTypeNone) {
isOpen = YES;
}
#endif
if (returnBlock) {
returnBlock(isOpen);
}
}
//====方法四
+ (void)isOpenMessageNotificationServiceWithBlock:(ReturnBlock)returnBlock
{
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
[[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings *settings) {
if (returnBlock) {
returnBlock(settings.authorizationStatus == UNAuthorizationStatusAuthorized);
}
}];
#elif __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0
returnBlock([[UIApplication sharedApplication] isRegisteredForRemoteNotifications]);
#else
UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (returnBlock) {
returnBlock(type != UIRemoteNotificationTypeNone);
}
#endif
}
4
//4.
NSContactsUsageDescription -> 通訊錄
NSMicrophoneUsageDescription -> 麥克風