iOS(跳转设置界面,权限相关,UIApplicationOpenSettingsURLString的使用)
相册权限 【iOS(跳转设置界面,权限相关,UIApplicationOpenSettingsURLString的使用)】(只需要判断是否为ALAuthorizationStatusDenied即可)
typedef NS_ENUM(NSInteger, ALAuthorizationStatus) {
ALAuthorizationStatusNotDetermined = 0, 用户尚未做出了选择这个应用程序的问候
ALAuthorizationStatusRestricted, 此应用程序没有被授权访问的照片数据。可能是家长控制权限。
ALAuthorizationStatusDenied, 用户已经明确否认了这一照片数据的应用程序访问.
ALAuthorizationStatusAuthorized 用户已授权应用访问照片数据.
}
2.访问相机时,优先访问相册,可以理解为相机为相册的子功能。
3.关于IOS8之后新跳转方法:UIApplicationOpenSettingsURLString,这个跳转方法可以直接跳转到设置中的项目设置页面的方法,其中的允许“xxx”访问中的项目不是自己添加的,而是系统自动识别的,每请求一次这个权限时,系统会自动记录并添加!!!
4.block作为参数传递:
- (void)test:(返回值类型(^)(参数1,参数2))参数名;
// 参数名称可省略
-(void)calculate:(int(^)(int))calculateBlock;
## 弹出对话框,进行选择
- (void)handleTakePicture {
__weak typeof(self) weakSelf = self;
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[weakSelf openCamera];
}];
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"从相机选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[weakSelf openAlbum];
}];
UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:action1];
[alert addAction:action2];
[alert addAction:action3];
[self.targetVC presentViewController:alert animated:YES completion:nil];
}## 操作
2.1 打开相机
- (void)openCamera {// 打开相机
[self checkAuthorizateCameraWithBlock:^(BOOL cameraAuthSuccess, BOOL albumAuthSuccess) {
if(!albumAuthSuccess) {
//[GTAppUtils showAlertViewWithTitle:@"" message:@"请到 设置》隐私》相册 开启相册权限."];
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"相册未授权" message:@"授权xxx访问你的照片" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"去设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:url];
}];
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {}];
[alertVC addAction:action1];
[alertVC addAction:action2];
[self.targetVC presentViewController:alertVC animated:YES completion:nil];
return;
}
if(cameraAuthSuccess) {
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {// 手机支持相机拍摄的情况下执行下列代码
UIImagePickerController* imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
imagePicker.mediaTypes = @[(NSString *)kUTTypeImage];
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;
[self.targetVC presentViewController:imagePicker animated:YES completion:nil];
NSLog(@"---");
}
} else {
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"相机未授权" message:@"授权xxx访问你的照片" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"去设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:url];
}];
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {}];
[alertVC addAction:action1];
[alertVC addAction:action2];
[self.targetVC presentViewController:alertVC animated:YES completion:nil];
}
}];
}// 验证权限
- (void)checkAuthorizateCameraWithBlock:(void(^)(BOOL cameraAuthSuccess, BOOL albumAuthSuccess))block {
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
dispatch_async(dispatch_get_main_queue(), ^{
NSInteger cameraAuth = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
block(cameraAuth != AVAuthorizationStatusDenied, status != PHAuthorizationStatusDenied);
});
}];
}
推荐阅读
- 第6.2章(设置属性)
- 2020-04-07vue中Axios的封装和API接口的管理
- iOS中的Block
- 记录iOS生成分享图片的一些问题,根据UIView生成固定尺寸的分享图片
- 2019-08-29|2019-08-29 iOS13适配那点事
- Hacking|Hacking with iOS: SwiftUI Edition - SnowSeeker 项目(一)
- iOS面试题--基础
- 15、IDEA学习系列之其他设置(生成javadoc、缓存和索引的清理等)
- 接口|axios接口报错-参数类型错误解决
- iOS|iOS 笔记之_时间戳 + DES 加密