總結下幾個常用到的獲取手機許可權,從iOS8以後,獲取手機某種許可權需要在info.plist文件中添加許可權的描述文件 1.通訊錄 頭文件 獲取通訊錄許可權並請求授權 2.相機 頭文件 獲取相機許可權(直接跳相機,在跳到相機時,會提示是否允許訪問相機) 3.相冊 頭文件 獲取相冊許可權(直接跳相冊,在跳到相冊 ...
總結下幾個常用到的獲取手機許可權,從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{ // 已授權 } }