友好訪問許可權篇:訪問語音、相簿、通訊錄----iOS

Guessplease發表於2021-01-03

我們經常使用到iOS訪問相簿、語音等一些許可權,當使用者使用到此功能時就給出彈出框,iOS預設第一次訪問時會自動去請求系統許可權。如果沒有其他互動貌似可以符合我們的要求。但當我們有互動,比如點選錄音我們自己有介面互動。

所以我們需要在點選時獲取使用者許可權狀態,然後再根據狀態值去判斷我們相應操作。是需要彈出使用者允許許可權,還是需要通知到設定中開啟許可權,還是已經允許直接給出我們程式碼互動邏輯。

1,在plist檔案設定許可權

	Privacy - Microphone Usage Description:設定麥克風許可權
	Privacy - Photo Library Additions Usage Description:設定儲存圖片到相簿許可權
	Privacy - Photo Library Usage Description:設定訪問相簿許可權
	Privacy - Camera Usage Description:開啟相機許可權
	Privacy - Contacts Usage Description:設定通訊錄許可權
	或者設定開啟plist——> Source Code新增
	`<string>需要拍照上傳圖片資訊,是否允許開啟相機?</string>
<key>NSFaceIDUsageDescription</key>
<string>需要您的同意才能訪問Face ID</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>拍照會獲取您的地理位置按地理資訊分類照片</string>
<key>NSMicrophoneUsageDescription</key>
<string>請求訪問麥克風</string>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>儲存圖片</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>需要新增圖片,是否允許開啟相簿</string>
<key>NSContactsUsageDescription</key>
<string>訪問通訊錄</string>`

2,程式碼邏輯編寫。

	獲取訪問相簿許可權匯入#import <Photos/Photos.h>
// 詢問 相簿許可權
- (void)getPhotoLibrary:(void(^)(BOOL))result {
    /// 獲取當前的狀態
    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
    if (status == PHAuthorizationStatusAuthorized) {
        result(YES);
        return;
    }
    if (status == PHAuthorizationStatusRestricted || status == PHAuthorizationStatusDenied) {
        result(NO); //無許可權
        return ;
    }
    if (status == PHAuthorizationStatusNotDetermined) {
        [self getPhotoLibrary];
        ;
    }
    
}
- (void)getPhotoLibrary{
    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
        NSLog(@"%@",[NSThread currentThread]);
        dispatch_async(dispatch_get_main_queue(), ^{
            
            
        });
    }];
}
- (void)goset{
    NSString *message = @"無法訪問相簿,請在設定中開啟訪問相簿許可權";
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"去設定", nil];
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (buttonIndex == 1) { // 去設定介面,開啟相機訪問許可權
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
    }
    
}
訪問麥克風許可權,匯入#import<AVFoundation/AVFoundation.h>
- (void) checkAudioStatus{
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
    switch (authStatus) {
        case AVAuthorizationStatusNotDetermined:
            [self getAVMediaTypeAudio];
            break;
        case AVAuthorizationStatusRestricted:
            [self goset];
            
            
            break;
        case AVAuthorizationStatusDenied:
            [self goset];
            
            break;
        case AVAuthorizationStatusAuthorized:
            //          已授權做自己的事;
            break;
        default:
            break;
    }
}

- (void)getAVMediaTypeAudio{
    [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
        dispatch_async(dispatch_get_main_queue(), ^{
            
        });
    }];
}





- (void)goset{
    NSString *message = @"無法錄音,請在設定中開啟麥克風訪問許可權";
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"去設定", nil];
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (buttonIndex == 1) { // 去設定介面,開啟麥克風訪問許可權
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
    }
    
}

獲取通訊錄許可權:iOS9之後ContactsUI代替了AddressBookUI,Contacts代替了AddressBook。匯入#import <ContactsUI/ContactsUI.h>
#pragma mark 請求通訊錄許可權
- (void)requestAuthorizationForAddressBook{
    
    CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
    if (status == CNAuthorizationStatusNotDetermined) { //使用者沒有設定
        //       獲取許可權
            [self getAddressBook];
    }
    else if(status == CNAuthorizationStatusRestricted ||status == CNAuthorizationStatusDenied){//彈出提示
        [self goset];
    }
    else if (status == CNAuthorizationStatusAuthorized){//已經授權
        //做自己的事情
    }
    
}

- (void)getAddressBook{
    //請求通訊錄許可權
    CNContactStore *store = [[CNContactStore alloc] init];
    [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError*  _Nullable error) {
        if (error) {
            NSLog(@"授權失敗");
        }else {
            NSLog(@"成功授權");
        }
    }];
}

- (void)goset{
    NSString *message = @"無法訪問通訊錄,請在設定中開啟通訊錄訪問許可權";
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"去設定", nil];
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (buttonIndex == 1) { // 去設定介面,開啟通訊錄訪問許可權
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
    }
    
}

其他許可權類似,就不一一寫出來了。如果大家有什麼更好的方式,歡迎留言,謝謝。

相關文章