RKSysAccess.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // RKSysAccess.m
  3. // YBVideo
  4. //
  5. // Created by YB007 on 2020/8/21.
  6. // Copyright © 2020 cat. All rights reserved.
  7. //
  8. #import "RKSysAccess.h"
  9. @interface RKSysAccess()
  10. @property(nonatomic,assign)BOOL alertIsShowing;
  11. @end
  12. @implementation RKSysAccess
  13. static RKSysAccess *_access = nil;
  14. +(instancetype)shareInstance {
  15. static dispatch_once_t onceToken;
  16. dispatch_once(&onceToken, ^{
  17. _access = [[super allocWithZone:NULL]init];
  18. });
  19. return _access;
  20. }
  21. + (instancetype)allocWithZone:(struct _NSZone *)zone{
  22. return [self shareInstance];
  23. }
  24. -(BOOL)checkMediaAccess {
  25. AVAuthorizationStatus videoAuthStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
  26. AVAuthorizationStatus audioAuthStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
  27. if (videoAuthStatus == AVAuthorizationStatusAuthorized && audioAuthStatus == AVAuthorizationStatusAuthorized) {
  28. return YES;
  29. }
  30. return NO;
  31. }
  32. -(void)requestMediaAccess:(RKAccessBlock)rkAccess{
  33. AVAuthorizationStatus videoAuthStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
  34. AVAuthorizationStatus audioAuthStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
  35. if (videoAuthStatus == AVAuthorizationStatusNotDetermined && audioAuthStatus == AVAuthorizationStatusNotDetermined) {
  36. //相机、麦克风首次授权
  37. [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
  38. if (granted == YES) {
  39. [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
  40. if (rkAccess) {
  41. rkAccess(granted);
  42. }
  43. }];
  44. }else {
  45. if (rkAccess) {
  46. rkAccess(NO);
  47. }
  48. }
  49. }];
  50. }else if (videoAuthStatus != AVAuthorizationStatusNotDetermined && audioAuthStatus == AVAuthorizationStatusNotDetermined){
  51. //麦克风首次授权
  52. [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
  53. if (rkAccess) {
  54. rkAccess(granted);
  55. }
  56. }];
  57. }else if (videoAuthStatus == AVAuthorizationStatusNotDetermined && audioAuthStatus != AVAuthorizationStatusNotDetermined){
  58. //相机首次授权
  59. [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
  60. if (rkAccess) {
  61. rkAccess(granted);
  62. }
  63. }];
  64. }else{
  65. if (rkAccess) {
  66. rkAccess(NO);
  67. }
  68. [self checkAccess:AVMediaTypeVideo];
  69. [self checkAccess:AVMediaTypeAudio];
  70. }
  71. // if (audioAuthStatus == AVAuthorizationStatusNotDetermined) {
  72. // [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
  73. // if (rkAccess) {
  74. // rkAccess(granted);
  75. // }
  76. // if (granted == NO) {
  77. // dispatch_async(dispatch_get_main_queue(), ^{
  78. // [[YBBaseAppDelegate sharedAppDelegate].topViewController.navigationController popViewControllerAnimated:YES];
  79. // });
  80. // }
  81. // }];
  82. // }
  83. // if (videoAuthStatus == AVAuthorizationStatusAuthorized && audioAuthStatus == AVAuthorizationStatusAuthorized ) {
  84. // //都同意
  85. // }else{
  86. // [self checkAccess:AVMediaTypeVideo];
  87. // [self checkAccess:AVMediaTypeAudio];
  88. // }
  89. }
  90. -(void)checkAccess:(AVMediaType)mediaType{
  91. if (_alertIsShowing) {
  92. return;
  93. }
  94. AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
  95. switch (authStatus) {
  96. case AVAuthorizationStatusRestricted: case AVAuthorizationStatusDenied:{
  97. //被拒或者受限
  98. _alertIsShowing = YES;
  99. NSDictionary *alerDic = @{@"title":YZMsg(@"提示"),@"msg":YZMsg(@"麦克风或相机权限未开启,请前往设置"),@"left":YZMsg(@"取消"),@"right":YZMsg(@"设置"),@"richImg":@""};
  100. [YBAlertView showAlertView:alerDic complete:^(int eventType) {
  101. _alertIsShowing = NO;
  102. if (eventType == 1) {
  103. [self goSet];
  104. }else{
  105. [[YBBaseAppDelegate sharedAppDelegate].topViewController.navigationController popViewControllerAnimated:YES];
  106. }
  107. }];
  108. }break;
  109. default:
  110. break;
  111. }
  112. }
  113. -(void)goSet{
  114. CGFloat systemVersion = [PublicObj getSysVersion];
  115. if (systemVersion >= 8.0 && systemVersion < 10.0) {
  116. NSURL * url = [NSURL URLWithString:@"prefs:root=General&path=Reset"];
  117. if ([[UIApplication sharedApplication] canOpenURL:url]) {
  118. [[UIApplication sharedApplication] openURL:url];
  119. }
  120. }else if (systemVersion >= 10.0) {
  121. NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  122. if ([[UIApplication sharedApplication] canOpenURL:url]) {
  123. if (@available(iOS 10.0, *)) {
  124. [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
  125. }];
  126. }
  127. }
  128. }
  129. }
  130. @end