cdts/xdts-ios 3/TreeHole/Code/Utility/VoiceRecoder/MTCameraTools.m
2023-07-27 09:20:00 +08:00

116 lines
4.7 KiB
Objective-C

//
// MTCameraTools.m
// Meet
//
// Created by ko1o on 2018/10/9.
// Copyright © 2018年 ko1o. All rights reserved.
//
#import "MTCameraTools.h"
#import <AVFoundation/AVFoundation.h>
#import "TZImagePickerController.h"
@implementation MTCameraTools
+ (instancetype)sharedInstance
{
static MTCameraTools * tool;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
tool = [MTCameraTools new];
});
return tool;
}
+ (void)isCameraAuthorized:(void(^)(BOOL authorized))completion
{
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if ((authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied)) {
NSDictionary *infoDict = [TZCommonTools tz_getInfoDictionary];
// 无权限 做一个友好的提示
NSString *appName = [infoDict valueForKey:@"CFBundleDisplayName"];
if (!appName) appName = [infoDict valueForKey:@"CFBundleName"];
dispatch_async(dispatch_get_main_queue(), ^{
if (completion) {
completion(NO);
}
});
NSString *message = [NSString stringWithFormat:[NSBundle tz_localizedStringForKey:@"Please allow %@ to access your camera in \"Settings -> Privacy -> Camera\""],appName];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSBundle tz_localizedStringForKey:@"Can not use camera"]
message:message
delegate:[MTCameraTools sharedInstance]
cancelButtonTitle:[NSBundle tz_localizedStringForKey:@"Cancel"]
otherButtonTitles:[NSBundle tz_localizedStringForKey:@"Setting"], nil];
[alert show];
} else if (authStatus == AVAuthorizationStatusNotDetermined) {
// fix issue 466, 防止用户首次拍照拒绝授权时相机页黑屏
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
dispatch_async(dispatch_get_main_queue(), ^{
if (completion) {
completion(granted);
}
});
}];
} else {
dispatch_async(dispatch_get_main_queue(), ^{
if (completion) {
completion(YES);
}
});
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) { // 去设置界面,开启访问权限
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}
}
+ (void)isMicrophoneAuthorized:(void(^)(BOOL authorized,BOOL thisTimeDetermined))completion
{
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
if ((authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied)) {
NSDictionary *infoDict = [TZCommonTools tz_getInfoDictionary];
// 无权限 做一个友好的提示
NSString *appName = [infoDict valueForKey:@"CFBundleDisplayName"];
if (!appName) appName = [infoDict valueForKey:@"CFBundleName"];
dispatch_async(dispatch_get_main_queue(), ^{
if (completion) {
completion(NO,NO);
}
});
NSString *message = [NSString stringWithFormat:[NSBundle tz_localizedStringForKey:@"Please allow %@ to access your microphone in \"Settings -> Privacy -> Microphone\""],appName];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSBundle tz_localizedStringForKey:@"Can not use microphone"]
message:message
delegate:[MTCameraTools sharedInstance]
cancelButtonTitle:[NSBundle tz_localizedStringForKey:@"Cancel"]
otherButtonTitles:[NSBundle tz_localizedStringForKey:@"Setting"], nil];
[alert show];
} else if (authStatus == AVAuthorizationStatusNotDetermined) {
// fix issue 466, 防止用户首次拍照拒绝授权时相机页黑屏
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
dispatch_async(dispatch_get_main_queue(), ^{
if (completion) {
completion(granted,YES);
}
});
}];
} else {
dispatch_async(dispatch_get_main_queue(), ^{
if (completion) {
completion(YES,NO);
}
});
}
}
@end