1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #import "RNPermissionHandlerMediaLibrary.h"
  2. @import MediaPlayer;
  3. @implementation RNPermissionHandlerMediaLibrary
  4. + (NSArray<NSString *> * _Nonnull)usageDescriptionKeys {
  5. return @[@"NSAppleMusicUsageDescription"];
  6. }
  7. + (NSString * _Nonnull)handlerUniqueId {
  8. return @"ios.permission.MEDIA_LIBRARY";
  9. }
  10. - (void)checkWithResolver:(void (^ _Nonnull)(RNPermissionStatus))resolve
  11. rejecter:(void (__unused ^ _Nonnull)(NSError * _Nonnull))reject {
  12. #if TARGET_OS_SIMULATOR
  13. resolve(RNPermissionStatusNotAvailable);
  14. #else
  15. if (@available(iOS 9.3, *)) {
  16. switch ([MPMediaLibrary authorizationStatus]) {
  17. case MPMediaLibraryAuthorizationStatusNotDetermined:
  18. return resolve(RNPermissionStatusNotDetermined);
  19. case MPMediaLibraryAuthorizationStatusRestricted:
  20. return resolve(RNPermissionStatusRestricted);
  21. case MPMediaLibraryAuthorizationStatusDenied:
  22. return resolve(RNPermissionStatusDenied);
  23. case MPMediaLibraryAuthorizationStatusAuthorized:
  24. return resolve(RNPermissionStatusAuthorized);
  25. }
  26. } else {
  27. resolve(RNPermissionStatusAuthorized);
  28. }
  29. #endif
  30. }
  31. - (void)requestWithResolver:(void (^ _Nonnull)(RNPermissionStatus))resolve
  32. rejecter:(void (^ _Nonnull)(NSError * _Nonnull))reject {
  33. #if TARGET_OS_SIMULATOR
  34. resolve(RNPermissionStatusNotAvailable);
  35. #else
  36. if (@available(iOS 9.3, *)) {
  37. [MPMediaLibrary requestAuthorization:^(__unused MPMediaLibraryAuthorizationStatus status) {
  38. [self checkWithResolver:resolve rejecter:reject];
  39. }];
  40. } else {
  41. resolve(RNPermissionStatusAuthorized);
  42. }
  43. #endif
  44. }
  45. @end