Commit c6c1c5e57aec62b2d0768635a66939172a702e66
1 parent
010cefa4
no message
Showing
4 changed files
with
146 additions
and
0 deletions
CNLiveManager.podspec
| ... | ... | @@ -47,6 +47,11 @@ TODO: 网家家-管理类. |
| 47 | 47 | ss.source_files = 'CNLiveManager/Classes/RootController/*.{h,m}' |
| 48 | 48 | end |
| 49 | 49 | |
| 50 | + # 权限 | |
| 51 | + s.subspec 'Authorization' do |ss| | |
| 52 | + ss.source_files = 'CNLiveManager/Classes/Authorization/*.{h,m}' | |
| 53 | + end | |
| 54 | + | |
| 50 | 55 | # s.resource_bundles = { |
| 51 | 56 | # 'CNLiveManager' => ['CNLiveManager/Assets/*.png'] |
| 52 | 57 | # } | ... | ... |
CNLiveManager/Classes/Authorization/CNLiveAuthorizationManager.h
0 → 100644
| 1 | +// | |
| 2 | +// CNLiveAuthorizationManager.h | |
| 3 | +// CNLiveManager | |
| 4 | +// | |
| 5 | +// Created by 153993236@qq.com on 07/22/2019. | |
| 6 | +// Copyright (c) 2019 153993236@qq.com. All rights reserved. | |
| 7 | +// | |
| 8 | + | |
| 9 | +/** | |
| 10 | + * 权限 | |
| 11 | + */ | |
| 12 | + | |
| 13 | +#import <Foundation/Foundation.h> | |
| 14 | + | |
| 15 | +NS_ASSUME_NONNULL_BEGIN | |
| 16 | + | |
| 17 | +@interface CNLiveAuthorizationManager : NSObject | |
| 18 | + | |
| 19 | +#pragma mark - 相机 | |
| 20 | ++ (void)getAVAuthorizationStatus:(void (^)(void))block; | |
| 21 | + | |
| 22 | +#pragma mark - 相册 | |
| 23 | ++ (void)getPHAuthorizationStatus:(void (^)(void))block; | |
| 24 | + | |
| 25 | +@end | |
| 26 | + | |
| 27 | +NS_ASSUME_NONNULL_END | ... | ... |
CNLiveManager/Classes/Authorization/CNLiveAuthorizationManager.m
0 → 100644
| 1 | +// | |
| 2 | +// CNLiveAuthorizationManager.h | |
| 3 | +// CNLiveManager | |
| 4 | +// | |
| 5 | +// Created by 153993236@qq.com on 07/22/2019. | |
| 6 | +// Copyright (c) 2019 153993236@qq.com. All rights reserved. | |
| 7 | +// | |
| 8 | + | |
| 9 | +#import "CNLiveAuthorizationManager.h" | |
| 10 | +#import <AVFoundation/AVCaptureDevice.h> | |
| 11 | +#import <AVFoundation/AVMediaFormat.h> | |
| 12 | +#import <Photos/Photos.h> | |
| 13 | + | |
| 14 | +@interface CNLiveAuthorizationManager () | |
| 15 | + | |
| 16 | +@end | |
| 17 | +@implementation CNLiveAuthorizationManager | |
| 18 | +#pragma mark - 获取跟控制器 | |
| 19 | ++ (UIViewController *)getRootController{ | |
| 20 | + UIViewController *vc = [UIApplication sharedApplication].delegate.window.rootViewController; | |
| 21 | + return vc; | |
| 22 | +} | |
| 23 | + | |
| 24 | +#pragma mark - 相机 | |
| 25 | ++ (void)getAVAuthorizationStatus:(void (^)(void))block{ | |
| 26 | + AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; | |
| 27 | + if (device) { | |
| 28 | + AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; | |
| 29 | + switch (status) { | |
| 30 | + case AVAuthorizationStatusNotDetermined: { | |
| 31 | + [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) { | |
| 32 | + if (granted) { | |
| 33 | + dispatch_sync(dispatch_get_main_queue(), ^{ | |
| 34 | + block(); | |
| 35 | + }); | |
| 36 | + NSLog(@"用户第一次同意了访问相机权限 - - %@", [NSThread currentThread]); | |
| 37 | + } else { | |
| 38 | + NSLog(@"用户第一次拒绝了访问相机权限 - - %@", [NSThread currentThread]); | |
| 39 | + } | |
| 40 | + }]; | |
| 41 | + break; | |
| 42 | + } | |
| 43 | + case AVAuthorizationStatusAuthorized: { | |
| 44 | + block(); | |
| 45 | + break; | |
| 46 | + } | |
| 47 | + case AVAuthorizationStatusDenied: { | |
| 48 | + NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary]; | |
| 49 | + NSString *app_Name = [infoDict objectForKey:@"CFBundleDisplayName"]; | |
| 50 | + if (app_Name == nil) { | |
| 51 | + app_Name = [infoDict objectForKey:@"CFBundleName"]; | |
| 52 | + } | |
| 53 | + NSString *messageString = [NSString stringWithFormat:@"请在iPhone的\"设置 - 隐私 - 相机\"选项中,允许%@访问你的相机", app_Name]; | |
| 54 | + UIAlertController *alertC = [UIAlertController alertControllerWithTitle:nil message:messageString preferredStyle:(UIAlertControllerStyleAlert)]; | |
| 55 | + UIAlertAction *alertA = [UIAlertAction actionWithTitle:@"好的" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) { | |
| 56 | + }]; | |
| 57 | + | |
| 58 | + [alertC addAction:alertA]; | |
| 59 | + [[self getRootController] presentViewController:alertC animated:YES completion:nil]; | |
| 60 | + break; | |
| 61 | + } | |
| 62 | + case AVAuthorizationStatusRestricted: { | |
| 63 | + NSLog(@"因为系统原因, 无法访问相册"); | |
| 64 | + break; | |
| 65 | + } | |
| 66 | + | |
| 67 | + default: | |
| 68 | + break; | |
| 69 | + } | |
| 70 | + return; | |
| 71 | + } | |
| 72 | + | |
| 73 | +} | |
| 74 | + | |
| 75 | +#pragma mark - 相册 | |
| 76 | ++ (void)getPHAuthorizationStatus:(void (^)(void))block{ | |
| 77 | + // 1.获取当前App的相册授权状态 | |
| 78 | + PHAuthorizationStatus authorizationStatus = [PHPhotoLibrary authorizationStatus]; | |
| 79 | + | |
| 80 | + // 2.判断授权状态 | |
| 81 | + if (authorizationStatus == PHAuthorizationStatusAuthorized) { | |
| 82 | + // 2.1 如果已经授权, 保存图片(调用步骤2的方法) | |
| 83 | + block(); | |
| 84 | + | |
| 85 | + } else if (authorizationStatus == PHAuthorizationStatusNotDetermined) { // 如果没决定, 弹出指示框, 让用户选择 | |
| 86 | + [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) { | |
| 87 | + // 如果用户选择授权, 则保存图片 | |
| 88 | + if (status == PHAuthorizationStatusAuthorized) { | |
| 89 | + dispatch_async(dispatch_get_main_queue(), ^{ | |
| 90 | + block(); | |
| 91 | + }); | |
| 92 | + } | |
| 93 | + }]; | |
| 94 | + | |
| 95 | + } else { | |
| 96 | + NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary]; | |
| 97 | + NSString *app_Name = [infoDict objectForKey:@"CFBundleDisplayName"]; | |
| 98 | + if (app_Name == nil) { | |
| 99 | + app_Name = [infoDict objectForKey:@"CFBundleName"]; | |
| 100 | + } | |
| 101 | + NSString *messageString = [NSString stringWithFormat:@"请在iPhone的\"设置 - 隐私 - 相机\"选项中,允许%@访问你的相册", app_Name]; | |
| 102 | + UIAlertController *alertC = [UIAlertController alertControllerWithTitle:nil message:messageString preferredStyle:(UIAlertControllerStyleAlert)]; | |
| 103 | + UIAlertAction *alertA = [UIAlertAction actionWithTitle:@"好的" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) { | |
| 104 | + | |
| 105 | + }]; | |
| 106 | + [alertC addAction:alertA]; | |
| 107 | + [[self getRootController] presentViewController:alertC animated:YES completion:nil]; | |
| 108 | + } | |
| 109 | +} | |
| 110 | + | |
| 111 | +@end | ... | ... |