iOS-硬體授權檢測【通訊錄、相機、相簿、日曆、麥克風、定位授權】,ios-通訊錄

來源:互聯網
上載者:User

iOS-硬體授權檢測【通訊錄、相機、相簿、日曆、麥克風、定位授權】,ios-通訊錄

總結下幾個常用到的擷取手機許可權,從iOS8以後,擷取手機某種許可權需要在info.plist檔案中添加許可權的描述檔案

    <key>NSContactsUsageDescription</key>    <string>App需要您的允許,才能訪問通訊錄</string>    <key>NSBluetoothPeripheralUsageDescription</key>    <string>App需要您的允許,才能訪問藍芽</string>    <key>NSCalendarsUsageDescription</key>    <string>App需要您的允許,才能訪問日曆</string>    <key>NSCameraUsageDescription</key>    <string>App需要您的允許,才能訪問相機</string>    <key>NSHealthUpdateUsageDescription</key>    <string>App需要您的允許,才能訪問健康更新 </string>    <key>NSLocationAlwaysUsageDescription</key>    <string></string>    <key>NSLocationUsageDescription</key>    <string>App需要您的允許,才能訪問位置</string>    <key>NSLocationWhenInUseUsageDescription</key>    <string></string>    <key>NSMicrophoneUsageDescription</key>    <string>App需要您的允許,才能訪問麥克風</string>    <key>NSMotionUsageDescription</key>    <string>App需要您的允許,才能訪問運動與健身</string>    <key>NSPhotoLibraryUsageDescription</key>    <string>App需要您的允許,才能訪問相簿</string>    <key>NSRemindersUsageDescription</key>    <string>App需要您的允許,才能訪問提醒事項</string>
1.通訊錄

標頭檔

#import <AddressBook/AddressBook.h>

擷取通訊錄許可權並請求授權

- (void)addressAuthorization{    ABAuthorizationStatus authStatus = ABAddressBookGetAuthorizationStatus();    if (authStatus == kABAuthorizationStatusNotDetermined) {        // 請求授權        ABAddressBookRef ressBookRef = ABAddressBookCreate();        ABAddressBookRequestAccessWithCompletion(ressBookRef, ^(bool granted, CFErrorRef error) {            if (granted) {                NSLog(@"授權成功!");            } else {                NSLog(@"授權失敗!");            }        });    }    else if(authStatus == kABAuthorizationStatusRestricted || authStatus == kABAuthorizationStatusDenied) {        // 未授權            }    else{        // 已授權    }}
2.相機

標頭檔

#import <AVFoundation/AVFoundation.h>#import <AssetsLibrary/AssetsLibrary.h>

擷取相機許可權(直接跳相機,在跳到相機時,會提示是否允許訪問相機)

- (void)cameraAuthorization{    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];    if (authStatus == ALAuthorizationStatusDenied||authStatus == ALAuthorizationStatusRestricted) {        NSLog(@"未授權!");        return ;    }}
3.相簿

標頭檔

#import <AVFoundation/AVFoundation.h>#import <AssetsLibrary/AssetsLibrary.h>

擷取相簿許可權(直接跳相簿,在跳到相簿時,會提示是否允許訪問相簿)

- (void)photoAuthorization{    //判斷是否已授權    ALAuthorizationStatus authStatus = [ALAssetsLibrary authorizationStatus];    if (authStatus == ALAuthorizationStatusDenied) {        NSLog(@"未授權!");        return;    }}
4.日曆

標頭檔

#import <EventKit/EventKit.h>

擷取日曆許可權並請求授權

-(void)calendarAuthorization{    EKAuthorizationStatus authStatus = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent];    if (EKAuthorizationStatusNotDetermined == authStatus) {        EKEventStore *event = [[EKEventStore alloc] init];        //授權成功,執行後續操作        [event requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {            if (granted) {                NSLog(@"授權成功!");            } else {                NSLog(@"授權失敗!");            }        }];    }    else if(authStatus == EKAuthorizationStatusRestricted || authStatus == EKAuthorizationStatusDenied) {        // 未授權            }    else{        // 已授權    }}
5.麥克風

標頭檔

擷取麥克風許可權並請求授權

- (void)microphoneAuthorization{    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];    if (authStatus == AVAuthorizationStatusNotDetermined) {// 未詢問使用者是否授權        //第一次詢問使用者是否進行授權        [[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {            if (granted) {                 NSLog(@"授權成功!");            }            else {                 NSLog(@"授權失敗!");            }        }];    }    else if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied) {        // 未授權           }    else{        // 已授權    }}
6.定位

標頭檔

#import <CoreLocation/CoreLocation.h>#import <LocalAuthentication/LocalAuthentication.h>

擷取定位許可權(請求定位時,會提示是否允許訪問位置)

- (void)locationAuthorization{    CLAuthorizationStatus authStatus = [CLLocationManager  authorizationStatus];    //第一次詢問使用者是否進行授權    if (kCLAuthorizationStatusNotDetermined == authStatus) {        //[[[CLLocationManager alloc] init] requestWhenInUseAuthorization];        //開始定位使用者的位置        //[self.locMgr startUpdatingLocation];    }    else if(authStatus == kCLAuthorizationStatusRestricted || authStatus == kCLAuthorizationStatusDenied) {        // 未授權    }    else{        // 已授權    }    }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.