iOS10適配 完美解決相機、相簿等許可權的使用
阿新 • • 發佈:2019-01-02
解決相機相簿呼叫奔潰:
崩潰:[access] This app has crashed because it attempted to access
privacy-sensitive data without a usage description. The app’s
Info.plist must contain an NSPhotoLibraryUsageDescription key with a
string value explaining to the user how the app uses this data.
ios 10 中許可權適配
升級到iOS10之後,需要設定許可權的有:
<!-- 相簿 -->
<key>NSPhotoLibraryUsageDescription</key>
<string>App需要您的同意,才能訪問相簿</string>
<!-- 相機 -->
<key>NSCameraUsageDescription</key>
<string>App需要您的同意,才能訪問相機</string>
<!-- 麥克風 -->
<key>NSMicrophoneUsageDescription</key >
<string>App需要您的同意,才能訪問麥克風</string>
<!-- 位置 -->
<key>NSLocationUsageDescription</key>
<string>App需要您的同意,才能訪問位置</string>
<!-- 在使用期間訪問位置 -->
<key>NSLocationWhenInUseUsageDescription</key>
<string>App需要您的同意,才能在使用期間訪問位置</string >
<!-- 始終訪問位置 -->
<key>NSLocationAlwaysUsageDescription</key>
<string>App需要您的同意,才能始終訪問位置</string>
<!-- 日曆 -->
<key>NSCalendarsUsageDescription</key>
<string>App需要您的同意,才能訪問日曆</string>
<!-- 提醒事項 -->
<key>NSRemindersUsageDescription</key>
<string>App需要您的同意,才能訪問提醒事項</string>
<!-- 運動與健身 -->
<key>NSMotionUsageDescription</key> <string>App需要您的同意,才能訪問運動與健身</string>
<!-- 健康更新 -->
<key>NSHealthUpdateUsageDescription</key>
<string>App需要您的同意,才能訪問健康更新 </string>
<!-- 健康分享 -->
<key>NSHealthShareUsageDescription</key>
<string>App需要您的同意,才能訪問健康分享</string>
<!-- 藍芽 -->
<key>NSBluetoothPeripheralUsageDescription</key>
<string>App需要您的同意,才能訪問藍芽</string>
<!-- 媒體資料庫 -->
<key>NSAppleMusicUsageDescription</key>
<string>App需要您的同意,才能訪問媒體資料庫</string>
info.plist中根據自己的需求複製新增
在使用時 .m 中新增
//相機
#import <AVFoundation/AVCaptureDevice.h>
#import <AVFoundation/AVMediaFormat.h>
//相簿
#import <AssetsLibrary/AssetsLibrary.h>
程式碼示例:
switch (buttonIndex) {
case 1:
{
//相機許可權
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (authStatus ==AVAuthorizationStatusRestricted ||//此應用程式沒有被授權訪問的照片資料。
authStatus ==AVAuthorizationStatusDenied) //使用者已經明確否認了這一照片資料的應用程式訪問
{
// 無許可權 引導去開啟
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication]canOpenURL:url]) {
[[UIApplication sharedApplication]openURL:url];
}
}else{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[self loadImage:UIImagePickerControllerSourceTypeCamera];
}
else
{
NSLog(@"手機不支援相機");
}
}
}
break;
case 2:
{
//相簿許可權
ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus];
if (author ==ALAuthorizationStatusRestricted || author ==ALAuthorizationStatusDenied){
//無許可權 引導去開啟
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
}else{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
[self loadImage:UIImagePickerControllerSourceTypePhotoLibrary];
}
else
{
NSLog(@"手機不支援相簿");
}
}
}
break;
default:
break;
}