12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //
  2. // RNPPhoto.m
  3. // ReactNativePermissions
  4. //
  5. // Created by Yonah Forst on 11/07/16.
  6. // Copyright © 2016 Yonah Forst. All rights reserved.
  7. //
  8. #import "RNPPhoto.h"
  9. #import <AssetsLibrary/AssetsLibrary.h>
  10. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_9_0
  11. @import Photos;
  12. #endif
  13. @implementation RNPPhoto
  14. + (NSString *)getStatus
  15. {
  16. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_9_0
  17. int status = [PHPhotoLibrary authorizationStatus];
  18. switch (status) {
  19. case PHAuthorizationStatusAuthorized:
  20. return RNPStatusAuthorized;
  21. case PHAuthorizationStatusDenied:
  22. return RNPStatusDenied;
  23. case PHAuthorizationStatusRestricted:
  24. return RNPStatusRestricted;
  25. default:
  26. return RNPStatusUndetermined;
  27. }
  28. #else
  29. int status = ABAddressBookGetAuthorizationStatus();
  30. switch (status) {
  31. case kABAuthorizationStatusAuthorized:
  32. return RNPStatusAuthorized;
  33. case kABAuthorizationStatusDenied:
  34. return RNPStatusDenied;
  35. case kABAuthorizationStatusRestricted:
  36. return RNPStatusRestricted;
  37. default:
  38. return RNPStatusUndetermined;
  39. }
  40. #endif
  41. }
  42. + (void)request:(void (^)(NSString *))completionHandler
  43. {
  44. void (^handler)(void) = ^(void) {
  45. dispatch_async(dispatch_get_main_queue(), ^{
  46. completionHandler([self.class getStatus]);
  47. });
  48. };
  49. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_9_0
  50. [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
  51. handler();
  52. }];
  53. #else
  54. ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
  55. [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
  56. handler()
  57. *stop = YES;
  58. } failureBlock:^(NSError *error) {
  59. handler();
  60. }];
  61. #endif
  62. }
  63. @end