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