TUIUserAuthorizationCenter.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // TUIUserAuthorizationCenter.m
  3. // TUIChat
  4. //
  5. // Created by wyl on 2022/2/16.
  6. // Copyright © 2023 Tencent. All rights reserved.
  7. //
  8. #import "TUIUserAuthorizationCenter.h"
  9. #import <AVFoundation/AVCaptureDevice.h>
  10. #import <CoreLocation/CLLocationManager.h>
  11. #import <CoreMotion/CoreMotion.h>
  12. #import <EventKit/EventKit.h>
  13. #import <Photos/Photos.h>
  14. #import <Speech/Speech.h>
  15. #import <TIMCommon/TIMDefine.h>
  16. #import <UserNotifications/UserNotifications.h>
  17. #import <TUICore/TUIGlobalization.h>
  18. @implementation TUIUserAuthorizationCenter
  19. + (BOOL)isEnableCameraAuthorization {
  20. if (@available(iOS 7.0, *)) {
  21. return [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo] == AVAuthorizationStatusAuthorized;
  22. } else {
  23. return YES;
  24. }
  25. }
  26. + (void)cameraStateActionWithPopCompletion:(void (^)(void))completion {
  27. if ([AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo] == AVAuthorizationStatusNotDetermined) {
  28. [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo
  29. completionHandler:^(BOOL granted) {
  30. if (granted && completion) {
  31. completion();
  32. }
  33. }];
  34. } else {
  35. [self showAlert:TUIChatAuthControlTypeCamera];
  36. }
  37. }
  38. + (void)openSettingPage {
  39. if (@available(iOS 8.0, *)) {
  40. NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  41. if ([[UIApplication sharedApplication] canOpenURL:url]) {
  42. if (@available(iOS 10.0, *)) {
  43. [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
  44. } else {
  45. [[UIApplication sharedApplication] openURL:url];
  46. }
  47. }
  48. } else {
  49. // Fallback on earlier versions
  50. }
  51. }
  52. + (BOOL)isEnableMicroAuthorization {
  53. if (@available(iOS 7.0, *)) {
  54. return [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio] == AVAuthorizationStatusAuthorized;
  55. } else {
  56. return YES;
  57. }
  58. }
  59. + (void)microStateActionWithPopCompletion:(void (^)(void))completion {
  60. #if !TARGET_OS_MACCATALYST
  61. if ([AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio] == AVAuthorizationStatusNotDetermined) {
  62. [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio
  63. completionHandler:^(BOOL granted) {
  64. if (granted && completion) {
  65. completion();
  66. }
  67. }];
  68. } else {
  69. [self showAlert:TUIChatAuthControlTypeMicro];
  70. }
  71. #endif
  72. }
  73. + (BOOL)isEnableLocationAuthorization {
  74. CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
  75. if (@available(iOS 8.0, *)) {
  76. return status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse;
  77. } else {
  78. // Fallback on earlier versions
  79. return YES;
  80. }
  81. }
  82. + (void)showAlert:(TUIChatAuthControlType)type {
  83. NSString *title = @"";
  84. NSString *message = @"";
  85. NSString *laterMessage = @"";
  86. NSString *openSettingMessage = @"";
  87. if (TUIChatAuthControlTypeMicro == type) {
  88. title = TIMCommonLocalizableString(TUIKitInputNoMicTitle);
  89. message = TIMCommonLocalizableString(TUIKitInputNoMicTips);
  90. laterMessage = TIMCommonLocalizableString(TUIKitInputNoMicOperateLater);
  91. openSettingMessage = TIMCommonLocalizableString(TUIKitInputNoMicOperateEnable);
  92. } else if (TUIChatAuthControlTypeCamera == type) {
  93. title = TIMCommonLocalizableString(TUIKitInputNoCameraTitle);
  94. message = TIMCommonLocalizableString(TUIKitInputNoCameraTips);
  95. laterMessage = TIMCommonLocalizableString(TUIKitInputNoCameraOperateLater);
  96. openSettingMessage = TIMCommonLocalizableString(TUIKitInputNoCameraOperateEnable);
  97. } else if (TUIChatAuthControlTypePhoto == type) {
  98. title = TIMCommonLocalizableString(TUIKitInputNoPhotoTitle);
  99. message = TIMCommonLocalizableString(TUIKitInputNoPhotoTips);
  100. laterMessage = TIMCommonLocalizableString(TUIKitInputNoPhotoOperateLater);
  101. openSettingMessage = TIMCommonLocalizableString(TUIKitInputNoPhotoerateEnable);
  102. } else {
  103. return;
  104. }
  105. if (@available(iOS 8.0, *)) {
  106. UIAlertController *ac = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
  107. [ac tuitheme_addAction:[UIAlertAction actionWithTitle:laterMessage style:UIAlertActionStyleCancel handler:nil]];
  108. [ac tuitheme_addAction:[UIAlertAction actionWithTitle:openSettingMessage
  109. style:UIAlertActionStyleDefault
  110. handler:^(UIAlertAction *_Nonnull action) {
  111. UIApplication *app = [UIApplication sharedApplication];
  112. NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  113. if ([app canOpenURL:settingsURL]) {
  114. [app openURL:settingsURL];
  115. }
  116. }]];
  117. dispatch_async(dispatch_get_main_queue(), ^{
  118. [UIApplication.sharedApplication.keyWindow.rootViewController presentViewController:ac animated:YES completion:nil];
  119. // [self presentViewController:ac animated:YES completion:nil];
  120. });
  121. } else {
  122. // Fallback on earlier versions
  123. }
  124. }
  125. @end