iOS8之后用一下方法跳轉到設置界面
//手機系統(tǒng)版本號
#define SYSTEMVERSION [[[UIDevice currentDevice]systemVersion]floatValue]
//跳轉到設置界面颈抚,讓用戶開啟權限
NSURL *url;
if(SYSTEMVERSION >= 8.0)
{
//iOS8 以后
url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; if ([[UIApplication sharedApplication] canOpenURL:url])
[[UIApplication sharedApplication] openURL:url];
}
補充資料:
在iOS應用程序中打開設備設置界面及其中某指定的選項界面
NSURL *url = [NSURL URLWithString:@“prefs:xxx"];
if ([[UIApplication sharedApplication] canOpenURL:url])
{
[[UIApplication sharedApplication] openURL:url];
}
一些其他可用的參數:
隱私下常用字段
@"prefs:root=Privacy&path=CAMERA"
,
//設置相機使用權限
@"prefs:root=Privacy&path=PHOTOS"
//設置照片使用權限
打開一級界面可將上面的字符串修改為以下對應的字段:
@"prefs:root=WIFI"
,//打開WiFi
@"prefs:root=Bluetooth"
,//打開藍牙設置頁
@"prefs:root=NOTIFICATIONS_ID"
,//通知設置
@"prefs:root=General"
,//通用
@"prefs:root=DISPLAY&BRIGHTNESS",//顯示與亮度
@"prefs:root=Wallpaper",//墻紙
@"prefs:root=Sounds",//聲音
@"prefs:root=Privacy",//隱私
@"prefs:root=STORE",//存儲
@"prefs:root=NOTES",//備忘錄
@"prefs:root=SAFARI",//Safari
@"prefs:root=MUSIC",//音樂
@"prefs:root=Photos",//照片與相機
@"prefs:root=CASTLE"http://iCloud
@"prefs:root=FACETIME",//FaceTime
@"prefs:root=LOCATION_SERVICES",//定位服務
@"prefs:root=Phone",//電話
@"prefs:root=AIRPLANE_MODE"http://飛行模式
@"prefs:root=General&path=Network"http://網絡
@"prefs:root=VIDEO"http://視頻
prefs:root=CASTLE&path=STORAGE_AND_BACKUP//
prefs:root=General&path=INTERNATIONAL//
prefs:root=ACCOUNT_SETTINGS//
prefs:root=MUSIC&path=EQ//
prefs:root=MUSIC&path=VolumeLimit//
prefs:root=NIKE_PLUS_IPOD//
prefs:root=General&path=ManagedConfigurationList//
prefs:root=General&path=Reset//
prefs:root=Sounds&path=Ringtone//
prefs:root=General&path=Assistant//
prefs:root=General&path=SOFTWARE_UPDATE_LINK//
prefs:root=TWITTER// prefs:root=General&path=USAGE//
prefs:root=General&path=Network/VPN//
prefs:root=INTERNET_TETHERING//
通用下常用字段
@"prefs:root=General&path=About",//關于本機
@"prefs:root=General&path=SOFTWARE_UPDATE_LINK",//軟件更新
@"prefs:root=General&path=DATE_AND_TIME",//日期和時間
@"prefs:root=General&path=ACCESSIBILITY",//輔助功能
@"prefs:root=General&path=Keyboard",//鍵盤
@"prefs:root=General&path=VPN",//VPN設置
@"prefs:root=General&path=AUTOLOCK",//自動鎖屏
@"prefs:root=General&path=INTERNATIONAL",//語言與地區(qū)
@"prefs:root=General&path=ManagedConfigurationList",//描述文件
prefs:root=General&path=ACCESSIBILITY//重力感應
prefs:root=General&path=USAGE/CELLULAR_USAGE//用量
.m 方發(fā)聲明
#import <Foundation/Foundation.h>
typedef enum : NSUInteger {
systemAuthorityCamera,
systemAuthorityPhotoLibrary,
systemAuthorityNotifacation,
systemAuthorityNetwork,
systemAuthorityAudio,
systemAuthorityLocation,
systemAuthorityAddressBook,
systemAuthorityCalendar,
systemAuthorityReminder,} systemAuthorityType;
@interface SystemAuthority : NSObject
/**
相機權限開關
@return YES/NO
*/
- (BOOL)CameraAuthority;
/**
相冊權限開關
@return YES/NO
*/
- (BOOL)PhotoLibraryAuthority;
/**
推送權限開關
@return YES/NO
*/
- (BOOL)notificationAuthority;
/**
連網權限開關
@return YES/NO
*/
- (BOOL)netWorkAuthority;
/**
麥克風權限開關
@return YES/NO
*/
- (BOOL)audioAuthority;
/**
定位權限開關
@return YES/NO
*/
- (BOOL)locationAuthority;
/**
通訊錄權限開關
@return YES/NO
*/
- (BOOL)addressBookAuthority;
/**
日歷權限開關
@return YES/NO
*/
- (BOOL)calendarAuthority;
/**
備忘錄權限開關
@return YES/NO
*/
- (BOOL)reminderAuthority;
@end
.h 方法實現(xiàn)
#import "SystemAuthority.h"
#import <AVFoundation/AVFoundation.h>
#import <Photos/Photos.h>
#import <AssetsLibrary/AssetsLibrary.h>
@import CoreTelephony;
#import <UserNotifications/UserNotifications.h>
#import <CoreLocation/CoreLocation.h>
#import <AddressBook/AddressBook.h>
#import <Contacts/Contacts.h>
#import <EventKit/EventKit.h>
//手機系統(tǒng)版本號
#define SYSTEMVERSION [[[UIDevice currentDevice]systemVersion]floatValue]
@implementation SystemAuthority
- (BOOL)CameraAuthority
{
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (authStatus == AVAuthorizationStatusDenied || authStatus == AVAuthorizationStatusRestricted) {
[self showAlertWithType:systemAuthorityCamera];
return NO;
}
return YES;
}
- (BOOL)PhotoLibraryAuthority
{
if (SYSTEMVERSION >= 8.0) {
PHAuthorizationStatus authStatus = [PHPhotoLibrary authorizationStatus];
if(authStatus == PHAuthorizationStatusDenied || authStatus == PHAuthorizationStatusRestricted) {
// 未授權
[self showAlertWithType:systemAuthorityPhotoLibrary];
return NO;
}
}
else if (SYSTEMVERSION >= 6.0 && SYSTEMVERSION < 8.0)
{
ALAuthorizationStatus authStatus = [ALAssetsLibrary authorizationStatus];
if(authStatus == ALAuthorizationStatusDenied || authStatus == ALAuthorizationStatusRestricted) {
// 未授權
[self showAlertWithType:systemAuthorityPhotoLibrary];
return NO;
}
}
return YES;
}
- (BOOL)notificationAuthority
{
if (SYSTEMVERSION>=8.0f)
{
UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
if (UIUserNotificationTypeNone == setting.types)
{
[self showAlertWithType:systemAuthorityNotifacation];
return NO;
}
}
else
{
UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if(UIRemoteNotificationTypeNone == type){
[self showAlertWithType:systemAuthorityNotifacation];
return NO;
}
}
return YES;
}
- (BOOL)netWorkAuthority
{
CTCellularData *cellularData = [[CTCellularData alloc]init];
CTCellularDataRestrictedState state = cellularData.restrictedState;
if (state == kCTCellularDataRestricted) {
[self showAlertWithType:systemAuthorityNetwork];
return NO;
}
return YES;
}
- (BOOL)audioAuthority
{
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
if (authStatus == AVAuthorizationStatusDenied || authStatus == AVAuthorizationStatusRestricted) {
[self showAlertWithType:systemAuthorityAudio];
return NO;
}
return YES;
}
- (BOOL)locationAuthority
{
BOOL isLocation = [CLLocationManager locationServicesEnabled];
if (!isLocation) {
CLAuthorizationStatus CLstatus = [CLLocationManager authorizationStatus];
if (CLstatus == kCLAuthorizationStatusDenied || CLstatus == kCLAuthorizationStatusDenied) {
[self showAlertWithType:systemAuthorityLocation];
return NO;
}
}
return YES;
}
- (BOOL)addressBookAuthority
{
if (SYSTEMVERSION >= 9.0) {
CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
if (status == CNAuthorizationStatusDenied || status == CNAuthorizationStatusRestricted)
{
[self showAlertWithType:systemAuthorityAddressBook];
return NO;
}
}
else
{
ABAuthorizationStatus ABstatus = ABAddressBookGetAuthorizationStatus();
if (ABstatus == kABAuthorizationStatusDenied || ABstatus == kABAuthorizationStatusRestricted)
{
[self showAlertWithType:systemAuthorityAddressBook];
return NO;
}
}
return YES;
}
- (BOOL)calendarAuthority
{
EKAuthorizationStatus EKstatus = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent];
if (EKstatus == EKAuthorizationStatusDenied || EKstatus == EKAuthorizationStatusRestricted)
{
[self showAlertWithType:systemAuthorityCalendar];
return NO;
}
return YES;
}
- (BOOL)reminderAuthority
{
EKAuthorizationStatus EKstatus = [EKEventStore authorizationStatusForEntityType:EKEntityTypeReminder];
if (EKstatus == EKAuthorizationStatusDenied || EKstatus == EKAuthorizationStatusRestricted)
{
[self showAlertWithType:systemAuthorityReminder];
return NO;
}
return YES;
}
- (void)showAlertWithType:(systemAuthorityType)type
{
NSString *title;
NSString *msg;
switch (type) {
case systemAuthorityCamera:
title = @"未獲得授權使用相機";
msg = @"請在設備的 設置-隱私-相機 中打開垄开。";
break;
case systemAuthorityPhotoLibrary:
title = @"未獲得授權使用相冊";
msg = @"請在設備的 設置-隱私-照片 中打開欣喧。";
break;
case systemAuthorityNotifacation:
title = @"未獲得授權使用推送";
msg = @"請在設備的 設置-隱私-推送 中打開橘忱。";
break;
case systemAuthorityNetwork:
title = @"未獲得授權使用網絡";
msg = @"請在設備的 設置-隱私-網絡 中打開叫榕。";
break;
case systemAuthorityAudio:
title = @"未獲得授權使用麥克風";
msg = @"請在設備的 設置-隱私-麥克風 中打開晃危。";
break;
case systemAuthorityLocation:
title = @"未獲得授權使用定位";
msg = @"請在設備的 設置-隱私-定位 中打開糕殉。";
break;
case systemAuthorityAddressBook:
title = @"未獲得授權使用通訊錄";
msg = @"請在設備的 設置-隱私-通訊錄 中打開颈将。";
break;
case systemAuthorityCalendar:
title = @"未獲得授權使用日歷";
msg = @"請在設備的 設置-隱私-日歷 中打開昌阿。";
break;
case systemAuthorityReminder:
title = @"未獲得授權使用備忘錄";
msg = @"請在設備的 設置-隱私-備忘錄 中打開饥脑。";
break;
default:
break;
}
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:title message:msg delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"立即前往", nil];
alertView.delegate = self;
[alertView show];
alertView.tag = type;
}
#pragma mark -- AlertView Delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1)
{
//跳轉到設置界面,讓用戶開啟權限
NSURL *url;
if(SYSTEMVERSION >= 8.0)
{
//iOS8 以后
url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication] canOpenURL:url])
[[UIApplication sharedApplication] openURL:url];
}
else
{
//iOS8 之前
//以下方法暫未測試
switch (alertView.tag) {
case systemAuthorityCamera:
url = [NSURL URLWithString:@"prefs:root=Privacy&path=CAMERA"];
break;
case systemAuthorityPhotoLibrary:
url = [NSURL URLWithString:@"prefs:root=Privacy&path=PHOTOS"];
break;
case systemAuthorityNetwork:
url = [NSURL URLWithString:@"prefs:root=General&path=Network"];
break;
case systemAuthorityAudio:
url = [NSURL URLWithString:@"prefs:root=Privacy&path=Audio"];
break;
case systemAuthorityLocation:
url = [NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"];
break;
case systemAuthorityAddressBook:
url = [NSURL URLWithString:@"prefs:root=Privacy&path=AddressBook"];
break;
case systemAuthorityCalendar:
url = [NSURL URLWithString:@"prefs:root=Privacy&path=Calendar"];
break;
case systemAuthorityReminder:
url = [NSURL URLWithString:@"prefs:root=Privacy&path=NOTES"];
break;
default:
break;
}
if ([[UIApplication sharedApplication] canOpenURL:url])
{
[[UIApplication sharedApplication] openURL:url];
}
}
}
}
@end