cdts/xdts-ios 3/TreeHole/Code/Utility/VoiceRecoder/MTCameraTools.m

116 lines
4.7 KiB
Mathematica
Raw Permalink Normal View History

2023-07-27 09:20:00 +08:00
//
// 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