iOS 權(quán)限判斷 跳轉(zhuǎn)對應設置界面

相機權(quán)限

1.1 使用說明

  1. 在合適的地方導入#import <AVFoundation/AVFoundation.h>
  2. 使用AVAuthorizationStatus類獲取當前權(quán)限狀態(tài)
  3. 在沒有權(quán)限的情況下彈出alertView提示跳轉(zhuǎn)。

1.2 代碼示例

  • 權(quán)限判斷
#import <AVFoundation/AVFoundation.h>
...
// 相機權(quán)限判斷
- (void)getPhotoAuthorizationStatus
{
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (authStatus == AVAuthorizationStatusDenied || authStatus == AVAuthorizationStatusRestricted) {
      // 沒有權(quán)限。彈出alertView
     [self showAlert];
    }else{
    //獲取了權(quán)限,直接調(diào)用相機接口
    }
    
}
  • 彈出alertView

- (void)showAlert
{
    
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"相機權(quán)限未開啟"
                                                        message:@"相機權(quán)限未開啟,請進入系統(tǒng)【設置】>【隱私】>【相機】中打開開關(guān),開啟相機功能"
                                                       delegate:nil
                                              cancelButtonTitle:@"取消"
                                              otherButtonTitles:@"立即開啟", nil];
    
 @weakify(self);
[[alertView rac_buttonClickedSignal] subscribeNext:^(NSNumber *buttonIndex) {
 @strongify(self);
         if ([buttonIndex isEqualToNumber:@1]) {
#ifdef __IPHONE_8_0 
          //跳入當前App設置界面,
        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
#else 
        //適配iOS7 ,跳入系統(tǒng)設置界面
        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"prefs:General&path=Reset"]];
#endif 
    }
}];
    [alertView show];
}

1.3 參數(shù)說明

  • 當前權(quán)限狀態(tài)
typedef NS_ENUM(NSInteger, AVAuthorizationStatus) {
 AVAuthorizationStatusNotDetermined = 0, // 用戶尚未做出選擇這個應用程序的問候
 AVAuthorizationStatusRestricted,   // 此應用程序沒有被授權(quán)訪問的照片數(shù)據(jù)。可能是家長控制權(quán)限
 AVAuthorizationStatusDenied,      // 用戶已經(jīng)明確否認了應用程序訪問
 AVAuthorizationStatusAuthorized   // 用戶已經(jīng)授權(quán)應用訪問相機
}

  • 跳轉(zhuǎn)到系統(tǒng)設定界面的字段
About — prefs:root=General&path=About  
Accessibility — prefs:root=General&path=ACCESSIBILITY  
AirplaneModeOn— prefs:root=AIRPLANE_MODE  
Auto-Lock — prefs:root=General&path=AUTOLOCK  
Brightness — prefs:root=Brightness  
Bluetooth — prefs:root=General&path=Bluetooth
Date& Time — prefs:root=General&path=DATE_AND_TIME  
FaceTime — prefs:root=FACETIME
General— prefs:root=General
Keyboard — prefs:root=General&path=Keyboard  
iCloud — prefs:root=CASTLE  iCloud 
Storage & Backup — prefs:root=CASTLE&path=STORAGE_AND_BACKUP  
International — prefs:root=General&path=INTERNATIONAL  
Location Services — prefs:root=LOCATION_SERVICES  
Music — prefs:root=MUSIC  
Music Equalizer — prefs:root=MUSIC&path=EQ  
Music VolumeLimit— prefs:root=MUSIC&path=VolumeLimit  
Network — prefs:root=General&path=Network  
Nike + iPod — prefs:root=NIKE_PLUS_IPOD  
Notes — prefs:root=NOTES  
Notification — prefs:root=NOTIFICATIONS_ID  
Phone — prefs:root=Phone  
Photos — prefs:root=Photos  
Profile — prefs:root=General&path=ManagedConfigurationList  
Reset — prefs:root=General&path=Reset  
Safari — prefs:root=Safari  Siri — prefs:root=General&path=Assistant  
Sounds — prefs:root=Sounds  
SoftwareUpdate— prefs:root=General&path=SOFTWARE_UPDATE_LINK  
Store — prefs:root=STORE  
Twitter — prefs:root=TWITTER  
Usage — prefs:root=General&path=USAGE  
VPN — prefs:root=General&path=Network/VPN  
Wallpaper — prefs:root=Wallpaper  
Wi-Fi — prefs:root=WIFI
Setting—prefs:root=INTERNET_TETHERING

注意:需要info中,添加 URL Schemes為 prefs的url

相冊權(quán)限

1.1 使用說明

  1. 在合適的地方導入#import <AssetsLibrary/AssetsLibrary.h>
  2. 使用ALAuthorizationStatus類獲取當前權(quán)限狀態(tài)
  3. 在沒有權(quán)限的情況下彈出alertView提示跳轉(zhuǎn)。

1.2 代碼示例

代碼如下所示,alertView如相機權(quán)限中所示.

//相冊權(quán)限判斷
- (void)getAlbumAuthorizationStatus
{
    ALAuthorizationStatus authStatus = [ALAssetsLibrary authorizationStatus];
    if (authStatus == ALAuthorizationStatusDenied || authStatus == ALAuthorizationStatusRestricted) {
        // 沒有權(quán)限
     [self showAlert];
    }else{
       // 已經(jīng)獲取權(quán)限
    }

}

1.3 參數(shù)說明

typedef NS_ENUM (NSInteger, ALAuthorizationStatus) {
    kCLAuthorizationStatusNotDetermined = 0, // 用戶尚未做出選擇這個應用程序的問候
    kCLAuthorizationStatusRestricted,        // 此應用程序沒有被授權(quán)訪問的照片數(shù)據(jù)??赡苁羌议L控制權(quán)限
    kCLAuthorizationStatusDenied,            // 用戶已經(jīng)明確否認了這一照片數(shù)據(jù)的應用程序訪問
    kCLAuthorizationStatusAuthorized         // 用戶已經(jīng)授權(quán)應用訪問照片數(shù)據(jù)
} CLAuthorizationStatus;

定位權(quán)限

1.1 使用說明

  1. 在合適的地方導入#import <CoreLocation/CLLocation.h>
  2. 使用[CLLocationManager authorizationStatus]獲取當前權(quán)限狀態(tài)
  3. 在沒有權(quán)限的情況下彈出alertView提示跳轉(zhuǎn)。

1.2 代碼示例

代碼如下所示,alertView如相機權(quán)限中所示.

- (void)servicesEnabled
{
    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusRestricted) {
          // 沒有權(quán)限,
           [self alertAuth];
        }
}

1.3 參數(shù)說明

typedef NS_ENUM(int, CLAuthorizationStatus) {

 kCLAuthorizationStatusNotDetermined = 0, // 用戶尚未做出選擇這個應用程序的問候
 kCLAuthorizationStatusRestricted,  // 受限制的,非用戶行為,此應用程序沒有被授權(quán)訪問的照片數(shù)據(jù)。
 kCLAuthorizationStatusDenied,    / 用戶已經(jīng)明確否認了這一應用程序訪問
 kCLAuthorizationStatusAuthorizedAlways  定位服務授權(quán)狀態(tài)已經(jīng)被用戶允許在任何狀態(tài)下獲取位置信息。
 kCLAuthorizationStatusAuthorizedWhenInUse  定位服務授權(quán)狀態(tài)僅被允許在使用應用程序的時候。
 kCLAuthorizationStatusAuthorized NS_ENUM_DEPRECATED(10_6, NA, 2_0, 8_0, "Use kCLAuthorizationStatusAuthorizedAlways") __TVOS_PROHIBITED __WATCHOS_PROHIBITED = kCLAuthorizationStatusAuthorizedAlways
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,326評論 25 708
  • 努力大于選擇,從而有多少人知道自己在努力奔跑的旅途,丟失了方向所在處。 蚊子:很努力,一直在努力的旅途中遙遙...
    不被記憶閱讀 340評論 2 1
  • %-
    4a043bfe087b閱讀 126評論 0 0
  • 我試著從現(xiàn)在起,回想在過去幾年里——更確切的說是高中階段——讓我仍能印象深刻的事與人。也許在我記錄下這些種種之后,...
    一點都不酷的人閱讀 305評論 0 0
  • 那天, 你還住在小河西岸 那座高高的山巔。 纖細的腰身 叫人分外愛憐。 媽媽說, 中秋節(jié)就要到了, 只等月兒長得圓...
    梅開如雪閱讀 551評論 27 42

友情鏈接更多精彩內(nèi)容