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{ // 已授權 } }