iOS 權限判斷 跳轉對應設置界面

相機權限

1.1 使用說明

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

1.2 代碼示例

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

- (void)showAlert
{
    
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"相機權限未開啟"
                                                        message:@"相機權限未開啟,請進入系統【設置】>【隱私】>【相機】中打開開關,開啟相機功能"
                                                       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 ,跳入系統設置界面
        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"prefs:General&path=Reset"]];
#endif 
    }
}];
    [alertView show];
}

1.3 參數說明

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

  • 跳轉到系統設定界面的字段
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

相冊權限

1.1 使用說明

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

1.2 代碼示例

代碼如下所示,alertView如相機權限中所示.

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

}

1.3 參數說明

typedef NS_ENUM (NSInteger, ALAuthorizationStatus) {
    kCLAuthorizationStatusNotDetermined = 0, // 用戶尚未做出選擇這個應用程序的問候
    kCLAuthorizationStatusRestricted,        // 此應用程序沒有被授權訪問的照片數據。可能是家長控制權限
    kCLAuthorizationStatusDenied,            // 用戶已經明確否認了這一照片數據的應用程序訪問
    kCLAuthorizationStatusAuthorized         // 用戶已經授權應用訪問照片數據
} CLAuthorizationStatus;

定位權限

1.1 使用說明

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

1.2 代碼示例

代碼如下所示,alertView如相機權限中所示.

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

1.3 參數說明

typedef NS_ENUM(int, CLAuthorizationStatus) {

 kCLAuthorizationStatusNotDetermined = 0, // 用戶尚未做出選擇這個應用程序的問候
 kCLAuthorizationStatusRestricted,  // 受限制的,非用戶行為,此應用程序沒有被授權訪問的照片數據。
 kCLAuthorizationStatusDenied,    / 用戶已經明確否認了這一應用程序訪問
 kCLAuthorizationStatusAuthorizedAlways  定位服務授權狀態已經被用戶允許在任何狀態下獲取位置信息。
 kCLAuthorizationStatusAuthorizedWhenInUse  定位服務授權狀態僅被允許在使用應用程序的時候。
 kCLAuthorizationStatusAuthorized NS_ENUM_DEPRECATED(10_6, NA, 2_0, 8_0, "Use kCLAuthorizationStatusAuthorizedAlways") __TVOS_PROHIBITED __WATCHOS_PROHIBITED = kCLAuthorizationStatusAuthorizedAlways
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容