关于相机相册的一些实用技术

最近做项目遇到了一些问题,就搜集了一些资料,集中在了一起
感谢:
CSDN 博主 [小手一背爱谁谁]
http://blog.csdn.net/saw471/article/details/52679746
wentian的博客
http://www.2cto.com/kf/201608/532614.html
本人只是搬运工,如有冒犯,请见谅!

一、权限状态说明

相册、相机、通讯录等授权状态目前都有种,都可以对应以下几种状态:

AuthorizationStatusNotDetermined      // 用户从未进行过授权等处理,首次访问相应内容会提示用户进行授权
AuthorizationStatusAuthorized = 0,    // 用户已授权,允许访问
AuthorizationStatusDenied,            // 用户拒绝访问
AuthorizationStatusRestricted,        // 应用没有相关权限,且当前用户无法改变这个权限,比如:家长控制

二、权限获取(以下都是iOS8之后的)
1.相册权限
是否支持

[UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]

获取权限状态

PHAuthorizationStatus authStatus = [PHPhotoLibrary authorizationStatus];

请求权限

[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
                }];

权限状态

typedef NS_ENUM(NSInteger, PHAuthorizationStatus) {
    PHAuthorizationStatusNotDetermined = 0, // User has not yet made a choice with regards to this application
    PHAuthorizationStatusRestricted,        // This application is not authorized to access photo data.
                                            // The user cannot change this application’s status, possibly due to active restrictions
                                            //   such as parental controls being in place.
    PHAuthorizationStatusDenied,            // User has explicitly denied this application access to photos data.
    PHAuthorizationStatusAuthorized         // User has authorized this application to access photos data.
} NS_AVAILABLE_IOS(8_0);

2.拍照权限
是否支持

[UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]

获取权限状态

AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];

请求权限

[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
                }];

权限状态

typedef NS_ENUM(NSInteger, AVAuthorizationStatus) {
    AVAuthorizationStatusNotDetermined = 0,
    AVAuthorizationStatusRestricted,
    AVAuthorizationStatusDenied,
    AVAuthorizationStatusAuthorized
} NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;

三、拒绝授权的处理
用户拒绝授权后,如果访问相应内容可能会出现一些类似没有数据的情况,此时应该给用户提示,引导用户授权。

跳转到应用设置:

NSURL *settingUrl = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication] canOpenURL:settingUrl]) {
    [[UIApplication sharedApplication] openURL:settingUrl];
}

四、info.plist 里面的相关配置

<!-- 相册 -->   
<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>  

*有个封装的工具类,可使用WTAuthorizationTool,里面还有通讯录的权限

pod "WTAuthorizationTool"
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,007评论 25 709
  • 在此特此声明:一下所有链接均来自互联网,在此记录下我的查阅学习历程,感谢各位原创作者的无私奉献 ! 技术一点一点积...
    远航的移动开发历程阅读 13,891评论 12 197
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,457评论 4 61
  • “嗨~你好,欢迎收听午夜电台,我是……” 不开心的时候我喜欢听深夜电台 想安静一个人的时候 我也喜欢听深夜电台 不...
    Mr橘子阅读 4,066评论 0 1
  • 1,描了两张头像,练习分层涂色、阴影和高光、第二张女生发丝的阴影明显比男生好一点。下一步,默写出来。
    陆离_mio阅读 1,643评论 0 0

友情链接更多精彩内容