2
0

LBXPermissionPhotos.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // LBXPermissionPhotos.m
  3. // LBXKits
  4. //
  5. // Created by lbxia on 2017/9/10.
  6. // Copyright © 2017年 lbx. All rights reserved.
  7. //
  8. #import "LBXPermissionPhotos.h"
  9. #import <Photos/Photos.h>
  10. #import <AssetsLibrary/AssetsLibrary.h>
  11. @implementation LBXPermissionPhotos
  12. + (BOOL)authorized
  13. {
  14. return [self authorizationStatus] == 3;
  15. }
  16. /**
  17. photo permission status
  18. @return
  19. 0 :NotDetermined
  20. 1 :Restricted
  21. 2 :Denied
  22. 3 :Authorized
  23. */
  24. + (NSInteger)authorizationStatus
  25. {
  26. if (@available(iOS 8,*))
  27. {
  28. return [PHPhotoLibrary authorizationStatus];
  29. }
  30. else
  31. {
  32. return [ALAssetsLibrary authorizationStatus];
  33. }
  34. }
  35. + (void)authorizeWithCompletion:(void(^)(BOOL granted,BOOL firstTime))completion
  36. {
  37. if (@available(iOS 8.0, *)) {
  38. PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
  39. switch (status) {
  40. case PHAuthorizationStatusAuthorized:
  41. {
  42. if (completion) {
  43. completion(YES,NO);
  44. }
  45. }
  46. break;
  47. case PHAuthorizationStatusRestricted:
  48. case PHAuthorizationStatusDenied:
  49. {
  50. if (completion) {
  51. completion(NO,NO);
  52. }
  53. }
  54. break;
  55. case PHAuthorizationStatusNotDetermined:
  56. {
  57. [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
  58. if (completion) {
  59. dispatch_async(dispatch_get_main_queue(), ^{
  60. completion(status == PHAuthorizationStatusAuthorized,YES);
  61. });
  62. }
  63. }];
  64. }
  65. break;
  66. default:
  67. {
  68. if (completion) {
  69. completion(NO,NO);
  70. }
  71. }
  72. break;
  73. }
  74. }else{
  75. ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
  76. switch (status) {
  77. case ALAuthorizationStatusAuthorized:
  78. {
  79. if (completion) {
  80. completion(YES, NO);
  81. }
  82. }
  83. break;
  84. case ALAuthorizationStatusNotDetermined:
  85. {
  86. ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
  87. [library enumerateGroupsWithTypes:ALAssetsGroupAll
  88. usingBlock:^(ALAssetsGroup *assetGroup, BOOL *stop) {
  89. if (*stop) {
  90. if (completion) {
  91. dispatch_async(dispatch_get_main_queue(), ^{
  92. completion(YES, NO);
  93. });
  94. }
  95. } else {
  96. *stop = YES;
  97. }
  98. }
  99. failureBlock:^(NSError *error) {
  100. if (completion) {
  101. dispatch_async(dispatch_get_main_queue(), ^{
  102. completion(NO, YES);
  103. });
  104. }
  105. }];
  106. } break;
  107. case ALAuthorizationStatusRestricted:
  108. case ALAuthorizationStatusDenied:
  109. {
  110. if (completion) {
  111. completion(NO, NO);
  112. }
  113. }
  114. break;
  115. }
  116. }
  117. }
  118. @end