2
0

LBXPermissionCamera.m 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //
  2. // LBXPermissionCamera.m
  3. // LBXKits
  4. //
  5. // Created by lbxia on 2017/9/10.
  6. // Copyright © 2017年 lbx. All rights reserved.
  7. //
  8. #import "LBXPermissionCamera.h"
  9. @implementation LBXPermissionCamera
  10. + (BOOL)authorized
  11. {
  12. if ([AVCaptureDevice respondsToSelector:@selector(authorizationStatusForMediaType:)])
  13. {
  14. AVAuthorizationStatus permission =
  15. [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
  16. return permission == AVAuthorizationStatusAuthorized;
  17. } else {
  18. // Prior to iOS 7 all apps were authorized.
  19. return YES;
  20. }
  21. }
  22. + (AVAuthorizationStatus)authorizationStatus
  23. {
  24. if ([AVCaptureDevice respondsToSelector:@selector(authorizationStatusForMediaType:)])
  25. {
  26. return [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
  27. } else {
  28. // Prior to iOS 7 all apps were authorized.
  29. return AVAuthorizationStatusAuthorized;
  30. }
  31. }
  32. + (void)authorizeWithCompletion:(void(^)(BOOL granted,BOOL firstTime))completion
  33. {
  34. if ([AVCaptureDevice respondsToSelector:@selector(authorizationStatusForMediaType:)]) {
  35. AVAuthorizationStatus permission =
  36. [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
  37. switch (permission) {
  38. case AVAuthorizationStatusAuthorized:
  39. completion(YES,NO);
  40. break;
  41. case AVAuthorizationStatusDenied:
  42. case AVAuthorizationStatusRestricted:
  43. completion(NO,NO);
  44. break;
  45. case AVAuthorizationStatusNotDetermined:
  46. {
  47. [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo
  48. completionHandler:^(BOOL granted) {
  49. if (completion) {
  50. dispatch_async(dispatch_get_main_queue(), ^{
  51. completion(granted,YES);
  52. });
  53. }
  54. }];
  55. }
  56. break;
  57. }
  58. } else {
  59. // Prior to iOS 7 all apps were authorized.
  60. completion(YES,NO);
  61. }
  62. }
  63. @end