https://www.jianshu.com/p/58cf8b21f268
plist文件里面添加,Privacy - Photo Library Usage Description,Value值為描述,彈出的提示框會顯示出來。
升到iOS10之后,需要設置權限的有:
麥克風權限:Privacy - Microphone Usage Description 是否允許此App使用你的麥克風?
相機權限: Privacy - Camera Usage Description 是否允許此App使用你的相機?
相冊權限: Privacy - Photo Library Usage Description 是否允許此App訪問你的媒體資料庫?
通訊錄權限: Privacy - Contacts Usage Description 是否允許此App訪問你的通訊錄?
藍牙權限:Privacy - Bluetooth Peripheral Usage Description 是否許允此App使用藍牙?
語音轉文字權限:Privacy - Speech Recognition Usage Description 是否允許此App使用語音識別?
日歷權限:Privacy - Calendars Usage Description
定位權限:Privacy - Location When In Use Usage Description
定位權限: Privacy - Location Always Usage Description
位置權限:Privacy - Location Usage Description
媒體庫權限:Privacy - Media Library Usage Description
健康分享權限:Privacy - Health Share Usage Description
健康更新權限:Privacy - Health Update Usage Description
運動使用權限:Privacy - Motion Usage Description
音樂權限:Privacy - Music Usage Description
提醒使用權限:Privacy - Reminders Usage Description
Siri使用權限:Privacy - Siri Usage Description
電視供應商使用權限:Privacy - TV Provider Usage Description
視頻用戶賬號使用權限:Privacy - Video Subscriber Account Usage Description
權限的plist
<key>NSAppleMusicUsageDescription</key> <string>App需要您的同意,才能訪問媒體資料庫</string> <key>NSBluetoothPeripheralUsageDescription</key> <string>App需要您的同意,才能訪問藍牙</string> <key>NSCalendarsUsageDescription</key> <string>App需要您的同意,才能訪問日歷</string> <key>NSCameraUsageDescription</key> <string>App需要您的同意,才能訪問相機</string> <key>NSHealthShareUsageDescription</key> <string>App需要您的同意,才能訪問健康分享</string> <key>NSHealthUpdateUsageDescription</key> <string>App需要您的同意,才能訪問健康更新 </string> <key>NSLocationAlwaysUsageDescription</key> <string>App需要您的同意,才能始終訪問位置</string> <key>NSLocationUsageDescription</key> <string>App需要您的同意,才能訪問位置</string> <key>NSLocationWhenInUseUsageDescription</key> <string>App需要您的同意,才能在使用期間訪問位置</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>
ios訪問權限
https://blog.csdn.net/LVXIANGAN/article/details/47172337
從ios7開始,用戶可以在設置->隱私->中開啟或關閉某些系統權限,比如訪問相冊,相機 ,通訊錄,地圖,麥克風等。因此,在我們的程序中,如果要訪問系統的某些功能,則最好判斷一下權限是否開啟。否則用戶不能正常使用,也一頭霧水,還以為程序出錯了。
訪問攝像頭:
需要導入
#import <AVFoundation/AVFoundation.h>
-
if(isIOS7AndLater) {
-
-
NSString *mediaType = AVMediaTypeVideo;// Or AVMediaTypeAudio
-
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
-
NSLog(@"---cui--authStatus--------%d",authStatus);
-
// This status is normally not visible—the AVCaptureDevice class methods for discovering devices do not return devices the user is restricted from accessing.
-
if(authStatus ==AVAuthorizationStatusRestricted){
-
NSLog(@"Restricted");
-
} else if(authStatus == AVAuthorizationStatusDenied){
-
// The user has explicitly denied permission for media capture.
-
NSLog(@"Denied"); //應該是這個,如果不允許的話
-
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
-
message: @"請在設備的"設置-隱私-相機"中允許訪問相機。"
-
delegate: self
-
cancelButtonTitle: @"確定"
-
otherButtonTitles: nil];
-
[alert show];
-
[alert release];
-
return;
-
}
-
else if(authStatus == AVAuthorizationStatusAuthorized){//允許訪問
-
// The user has explicitly granted permission for media capture, or explicit user permission is not necessary for the media type in question.
-
NSLog(@"Authorized");
-
-
} else if(authStatus == AVAuthorizationStatusNotDetermined){
-
// Explicit user permission is required for media capture, but the user has not yet granted or denied such permission.
-
[ AVCaptureDevice requestAccessForMediaType:mediaType completionHandler:^(BOOL granted) {
-
if(granted){//點擊允許訪問時調用
-
//用戶明確許可與否,媒體需要捕獲,但用戶尚未授予或拒絕許可。
-
NSLog(@"Granted access to %@", mediaType);
-
}
-
else {
-
NSLog(@"Not granted access to %@", mediaType);
-
}
-
-
}];
-
} else {
-
NSLog(@"Unknown authorization status");
-
}
-
}
麥克風權限檢測:
-
//檢測麥克風功能是否打開
-
[[ AVAudioSessionsharedInstance]requestRecordPermission:^(BOOL granted) {
-
if (!granted)
-
{
-
[ViewUtilalertViewWithString: NSLocalizedString(@"麥克風功能未開啟",nil)];
-
}
-
else
-
{
-
-
[selfrecord:sender];
-
}
-
}];
相冊權限檢測:需要
#import <AssetsLibrary/AssetsLibrary.h> //導入此類和AssetsLibrary.framework框架
-
int author = [ALAssetsLibrary authorizationStatus];
-
NSLog(@"author type:%d",author);
-
if(author == ALAuthorizationStatusRestricted || author == ALAuthorizationStatusDenied) {
-
// The user has explicitly denied permission for media capture.
-
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"無法使用相冊"
-
message: @"請在iPhone的\"設置-隱私-照片\"中允許訪問照片。"
-
delegate: self
-
cancelButtonTitle: @"確定"
-
otherButtonTitles: nil];
-
[alert show];
-
[alert release];
-
return;
ALAssetsLibrary詳解
ALAssetsLibrary類是代表系統中整個資源庫,使用它可以訪問資源庫中的資源和保存照片,視頻等功能。
_library = [[ALAssetsLibrary alloc]init];
//判斷當前應用是否能訪問相冊資源
/*
typedef NS_ENUM(NSInteger, ALAuthorizationStatus) {
ALAuthorizationStatusNotDetermined = 0, 用戶尚未做出了選擇這個應用程序的問候
ALAuthorizationStatusRestricted, 此應用程序沒有被授權訪問的照片數據。可能是家長控制權限。
ALAuthorizationStatusDenied, 用戶已經明確否認了這一照片數據的應用程序訪問.
ALAuthorizationStatusAuthorized 用戶已授權應用訪問照片數據.
}
*/
int author = [ALAssetsLibrary authorizationStatus];
NSLog(@"author type:%d",author);
//關閉監聽共享照片流產生的頻繁通知信息
[ALAssetsLibrary disableSharedPhotoStreamsSupport];
//創建一個相冊到相冊資源中,並通過block返回創建成功的相冊ALAssetsGroup
[_library addAssetsGroupAlbumWithName:@"test" resultBlock:^(ALAssetsGroup *group) {
//NSString *const ALAssetsGroupPropertyName;
//NSString *const ALAssetsGroupPropertyType;
//NSString *const ALAssetsGroupPropertyPersistentID;
//NSString *const ALAssetsGroupPropertyURL;
//查看相冊的名字
NSLog(@"ALAssetsGroupPropertyName:%@",[group valueForProperty:ALAssetsGroupPropertyName]);
//查看相冊的類型
NSLog(@"ALAssetsGroupPropertyType:%@",[group valueForProperty:ALAssetsGroupPropertyType]);
//查看相冊的存儲id
NSLog(@"ALAssetsGroupPropertyPersistentID:%@",[group valueForProperty:ALAssetsGroupPropertyPersistentID]);
//查看相冊存儲的位置地址
NSLog(@"ALAssetsGroupPropertyURL:%@",[group valueForProperty:ALAssetsGroupPropertyURL]);
groupURL = [group valueForProperty:ALAssetsGroupPropertyURL];
} failureBlock:^(NSError *error) {
}];
新添加了一個名為test的相冊
[NSThread sleepForTimeInterval:0.5];
//通過url地址在相冊資源中獲取該地址的資源文件ALAsset,有可能是相片或視頻
[_library assetForURL:[NSURL URLWithString:@""] resultBlock:^(ALAsset *asset) {
/*
NSString *const ALAssetPropertyType;
NSString *const ALAssetPropertyLocation;
NSString *const ALAssetPropertyDuration;
NSString *const ALAssetPropertyOrientation;
NSString *const ALAssetPropertyDate;
NSString *const ALAssetPropertyRepresentations;
NSString *const ALAssetPropertyURLs;
NSString *const ALAssetPropertyAssetURL;
*/
//查看資源的地理位置信息
NSLog(@"ALAssetPropertyLocation:%@",[asset valueForProperty:ALAssetPropertyLocation]);
//如果資源是視頻,查看視頻的時長
NSLog(@"ALAssetPropertyDuration:%@",[asset valueForProperty:ALAssetPropertyDuration]);
//查看資源的方向,圖片的旋轉方向
NSLog(@"ALAssetPropertyOrientation:%@",[asset valueForProperty:ALAssetPropertyOrientation]);
//查看資源的創建時間
NSLog(@"ALAssetPropertyDate:%@",[asset valueForProperty:ALAssetPropertyDate]);
//查看資源的描述信息
NSLog(@"ALAssetPropertyRepresentations:%@",[asset valueForProperty:ALAssetPropertyRepresentations]);
NSLog(@"ALAssetPropertyURLs:%@",[asset valueForProperty:ALAssetPropertyURLs]);
//查看資源的url路徑
NSLog(@"ALAssetPropertyAssetURL:%@",[asset valueForProperty:ALAssetPropertyAssetURL]);
} failureBlock:^(NSError *error) {
}];
//通過url地址獲取相冊資源中的一個相冊
[_library groupForURL:groupURL resultBlock:^(ALAssetsGroup *group) {
NSLog(@"ALAssetsGroupPropertyName:%@",[group valueForProperty:ALAssetsGroupPropertyName]);
} failureBlock:^(NSError *error) {
}];
//根據選擇的類型遍歷相冊資源中的相對應類型的所有相冊,其中stop行參是指針,表示是否停止迭代,當賦值為false則停止
/*
enum {
ALAssetsGroupLibrary = (1 << 0),
ALAssetsGroupAlbum = (1 << 1),
ALAssetsGroupEvent = (1 << 2),
ALAssetsGroupFaces = (1 << 3),
ALAssetsGroupSavedPhotos = (1 << 4),
ALAssetsGroupPhotoStream = (1 << 5),
ALAssetsGroupAll = 0xFFFFFFFF,
};
*/
[_library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
NSLog(@"group name:%@",[group valueForProperty:ALAssetsGroupPropertyName]);
} failureBlock:^(NSError *error) {
}];
//保存圖片到系統默認的相冊中,使用nsdata的形式,並返回照片的url地址
[_library writeImageDataToSavedPhotosAlbum:nil metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
}];
//保存圖片到系統默認的相冊中,使用cgimageref的形式,並返回照片的url地址
[_library writeImageToSavedPhotosAlbum:nil metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
}];
//保存圖片到系統默認的相冊中,使用cgimageref的形式,並且選擇圖片以什么旋轉方向的形式保存,並返回照片的url地址
/*
typedef enum {
ALAssetOrientationUp, // default orientation
ALAssetOrientationDown, // 180 deg rotation
ALAssetOrientationLeft, // 90 deg CCW
ALAssetOrientationRight, // 90 deg CW
ALAssetOrientationUpMirrored, // as above but image mirrored along other axis. horizontal flip
ALAssetOrientationDownMirrored, // horizontal flip
ALAssetOrientationLeftMirrored, // vertical flip
ALAssetOrientationRightMirrored, // vertical flip
} ALAssetOrientation;
*/
UIImage* image = [UIImage imageNamed:@"test.png"];
[_library writeImageToSavedPhotosAlbum:[image CGImage] orientation:ALAssetOrientationLeft completionBlock:^(NSURL *assetURL, NSError *error) {
NSLog(@"save image:%@",assetURL);
}];