iOS系統(tǒng)授權及跳轉設置頁面引導

相機攝像頭的授權

部分代碼

+ (void)showAlertForMediaType:(NSString *)mediaType completionHandler:(void (^)(WLSystemAuthStatus status))handler{
    AVAuthorizationStatus authorizationStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
switch (authorizationStatus) {
        case AVAuthorizationStatusNotDetermined:{
            [AVCaptureDevice requestAccessForMediaType:mediaType completionHandler:^(BOOL granted) {
                if (granted) {
                    handler(WLSystemAuthStatusAuthorized);
                }
                else{
                    handler(WLSystemAuthStatusUnknownError);
                }
            }];
            break;
        }
        case AVAuthorizationStatusAuthorized:
            handler(WLSystemAuthStatusAuthorized);
            break;
        case AVAuthorizationStatusRestricted:
            NSLog(@"受限,有可能開啟了訪問限制");
        case AVAuthorizationStatusDenied:
            NSLog(@"訪問受限");
            [self showAlertViewWithType:WLSystemAuthTypeCamera];
            handler(WLSystemAuthStatusUnknownError);
            break;
    }
 }

照片授權

#ifdef __IPHONE_8_0
    if (kIOS8Later) {
        PHAuthorizationStatus authorizationStatus = [PHPhotoLibrary authorizationStatus];
        switch (authorizationStatus) {
            case PHAuthorizationStatusNotDetermined:{
                [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
                    status == PHAuthorizationStatusAuthorized?handler(WLSystemAuthStatusAuthorized):(void)NULL;
                }];
                break;
            }
            case PHAuthorizationStatusAuthorized:
                handler(WLSystemAuthStatusAuthorized);
                break;
            case PHAuthorizationStatusRestricted:
            case PHAuthorizationStatusDenied:
                [self showAlertViewWithType:WLSystemAuthTypePhotos];
                handler(WLSystemAuthStatusUnknownError);
                break;
        }
    } else {
        ALAuthorizationStatus authorizationStatus = [ALAssetsLibrary authorizationStatus];
        switch (authorizationStatus) {
            case ALAuthorizationStatusNotDetermined:{
                handler(WLSystemAuthStatusOther);
                break;
            }
            case ALAuthorizationStatusAuthorized:
                handler(WLSystemAuthStatusAuthorized);
                break;
            case ALAuthorizationStatusRestricted:
            case ALAuthorizationStatusDenied:
                [self showAlertViewWithType:WLSystemAuthTypePhotos];
                handler(WLSystemAuthStatusUnknownError);
                break;
        }
    }
#else
    ALAuthorizationStatus authorizationStatus = [ALAssetsLibrary authorizationStatus];
    switch (authorizationStatus) {
        case ALAuthorizationStatusNotDetermined:{
            handler(WLSystemAuthStatusOther);
            break;
        }
        case ALAuthorizationStatusAuthorized:
            handler(WLSystemAuthStatusAuthorized);
            break;
        case ALAuthorizationStatusRestricted:
        case ALAuthorizationStatusDenied:
            [self showAlertViewWithType:WLSystemAuthTypePhotos];
            handler(WLSystemAuthStatusUnknownError);
            break;
    }
#endif

位置授權

  CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
    switch (status) {
        case kCLAuthorizationStatusNotDetermined:{
            handler(WLSystemAuthStatusOther);
        }
            break;
        case kCLAuthorizationStatusAuthorized:
//        case kCLAuthorizationStatusAuthorizedAlways:
        case kCLAuthorizationStatusAuthorizedWhenInUse:
            if (handler) {
                handler(WLSystemAuthStatusAuthorized);
            }
            break;
        case kCLAuthorizationStatusRestricted:
        case kCLAuthorizationStatusDenied:
            [self showAlertViewWithType:WLSystemAuthTypeLocation];
            if (handler) {
                handler(WLSystemAuthStatusUnknownError);
            }
            break;
    }

通訊錄授權

// iOS8 以上待定
//    CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];

    ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
    
    switch (status) {
        case kABAuthorizationStatusNotDetermined:{
            ABAddressBookRef addressBook = NULL;
            ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
                if (granted) {
                    handler(WLSystemAuthStatusAuthorized);
                }
                else{
                    handler(WLSystemAuthStatusUnknownError);
                }
            });
        }
            break;
        case kABAuthorizationStatusAuthorized:
            handler(WLSystemAuthStatusAuthorized);
            break;
        case kABAuthorizationStatusRestricted:
        case kABAuthorizationStatusDenied:
            [self showAlertViewWithType:WLSystemAuthTypeContacts];
            handler(WLSystemAuthStatusUnknownError);
            break;
       
    }

跳轉到設置

引導用戶去打開 在 URL Types 添加 一個叫 prefs 的 URL Schemes

  • iOS 系統(tǒng)功能的 URL 匯總列表:
蜂窩網絡:prefs:root=MOBILE_DATA_SETTINGS_ID
VPN — prefs:root=General&path=Network/VPN
Wi-Fi:prefs:root=WIFI
定位服務:prefs:root=LOCATION_SERVICES
個人熱點:prefs:root=INTERNET_TETHERING
關于本機:prefs:root=General&path=About
輔助功能:prefs:root=General&path=ACCESSIBILITY
飛行模式:prefs:root=AIRPLANE_MODE
鎖定:prefs:root=General&path=AUTOLOCK
亮度:prefs:root=Brightness
藍牙:prefs:root=General&path=Bluetooth
時間設置:prefs:root=General&path=DATE_AND_TIME
FaceTime:prefs:root=FACETIME
設置:prefs:root=General
鍵盤設置:prefs:root=General&path=Keyboard
iCloud:prefs:root=CASTLE
iCloud備份:prefs:root=CASTLE&path=STORAGE_AND_BACKUP
語言:prefs:root=General&path=INTERNATIONAL
定位:prefs:root=LOCATION_SERVICES
音樂:prefs:root=MUSIC
相機:prefs:root=Privacy&&path=CAMERA
通訊錄: prefs:root=Privacy&path=CONTACTS
Music Equalizer — prefs:root=MUSIC&path=EQ
Music Volume Limit — 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
Software Update — prefs:root=General&path=SOFTWARE_UPDATE_LINK
Store — prefs:root=STORE
Twitter — prefs:root=TWITTER
Usage — prefs:root=General&path=USAGE
Wallpaper — prefs:root=Wallpaper

備注: 有的可能跳不過去 請把 path后面的 大寫

  • iOS8 以上貌似可以跳到對應的應用,然后去設置
NSString *prefs = [NSString stringWithFormat:@"prefs:root=%@",[[NSBundle mainBundle] bundleIdentifier]];
NSURL *url = [NSURL URLWithString:prefs];
if ([UIDevice currentDevice].systemVersion.doubleValue > 8.0f) {
      url= [NSURL URLWithString:UIApplicationOpenSettingsURLString];
}
if([[UIApplication sharedApplication]canOpenURL:url]){
    [[UIApplication sharedApplication]openURL:url];
}

配置下 url type

308911-4113b095143d50fd.png

源碼下載

https://github.com/cuiwe000/SystemAuth.git

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容