iOS-硬件授权检测【通讯录、相机、相册、日历、麦克风、定位授权】

时间:2021-12-25 17:09:36

总结下几个常用到的获取手机权限,从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>

<!-- xcode9以后必须要加-->

<key>NSPhotoLibraryAddUsageDescription</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{
// 已授权
} }