ReactNativePermissions.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //
  2. // ReactNativePermissions.m
  3. // ReactNativePermissions
  4. //
  5. // Created by Yonah Forst on 18/02/16.
  6. // Copyright © 2016 Yonah Forst. All rights reserved.
  7. //
  8. @import Contacts;
  9. #import "ReactNativePermissions.h"
  10. #if __has_include(<React/RCTBridge.h>)
  11. #import <React/RCTBridge.h>
  12. #elif __has_include("React/RCTBridge.h")
  13. #import "React/RCTBridge.h"
  14. #else
  15. #import "RCTBridge.h"
  16. #endif
  17. #if __has_include(<React/RCTConvert.h>)
  18. #import <React/RCTConvert.h>
  19. #elif __has_include("React/RCTConvert.h")
  20. #import "React/RCTConvert.h"
  21. #else
  22. #import "RCTConvert.h"
  23. #endif
  24. #if __has_include(<React/RCTEventDispatcher.h>)
  25. #import <React/RCTEventDispatcher.h>
  26. #elif __has_include("React/RCTEventDispatcher.h")
  27. #import "React/RCTEventDispatcher.h"
  28. #else
  29. #import "RCTEventDispatcher.h"
  30. #endif
  31. #import "RNPLocation.h"
  32. #import "RNPNotification.h"
  33. #import "RNPAudioVideo.h"
  34. #import "RNPPhoto.h"
  35. @interface ReactNativePermissions()
  36. @property (strong, nonatomic) RNPLocation *locationMgr;
  37. @property (strong, nonatomic) RNPNotification *notificationMgr;
  38. @end
  39. @implementation ReactNativePermissions
  40. RCT_EXPORT_MODULE();
  41. @synthesize bridge = _bridge;
  42. + (BOOL)requiresMainQueueSetup
  43. {
  44. return YES;
  45. }
  46. #pragma mark Initialization
  47. - (instancetype)init
  48. {
  49. if (self = [super init]) {
  50. }
  51. return self;
  52. }
  53. /**
  54. * run on the main queue.
  55. */
  56. - (dispatch_queue_t)methodQueue {
  57. return dispatch_get_main_queue();
  58. }
  59. RCT_REMAP_METHOD(canOpenSettings, canOpenSettings:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  60. {
  61. resolve(@(UIApplicationOpenSettingsURLString != nil));
  62. }
  63. RCT_EXPORT_METHOD(openSettings:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  64. {
  65. if (@(UIApplicationOpenSettingsURLString != nil)) {
  66. NSNotificationCenter * __weak center = [NSNotificationCenter defaultCenter];
  67. id __block token = [center addObserverForName:UIApplicationDidBecomeActiveNotification
  68. object:nil
  69. queue:nil
  70. usingBlock:^(NSNotification *note) {
  71. [center removeObserver:token];
  72. resolve(@YES);
  73. }];
  74. NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  75. [[UIApplication sharedApplication] openURL:url];
  76. }
  77. }
  78. RCT_REMAP_METHOD(getPermissionStatus, getPermissionStatus:(RNPType)type json:(id)json resolve:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  79. {
  80. NSString *status;
  81. switch (type) {
  82. case RNPTypeLocation: {
  83. NSString *locationPermissionType = [RCTConvert NSString:json];
  84. status = [RNPLocation getStatusForType:locationPermissionType];
  85. break;
  86. }
  87. case RNPTypeCamera:
  88. status = [RNPAudioVideo getStatus:@"video"];
  89. break;
  90. case RNPTypePhoto:
  91. status = [RNPPhoto getStatus];
  92. break;
  93. case RNPTypeNotification:
  94. status = [RNPNotification getStatus];
  95. break;
  96. default:
  97. break;
  98. }
  99. resolve(status);
  100. }
  101. RCT_REMAP_METHOD(requestPermission, permissionType:(RNPType)type json:(id)json resolve:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  102. {
  103. NSString *status;
  104. switch (type) {
  105. case RNPTypeLocation:
  106. return [self requestLocation:json resolve:resolve];
  107. case RNPTypeCamera:
  108. return [RNPAudioVideo request:@"video" completionHandler:resolve];
  109. case RNPTypePhoto:
  110. return [RNPPhoto request:resolve];
  111. case RNPTypeNotification:
  112. return [self requestNotification:json resolve:resolve];
  113. default:
  114. break;
  115. }
  116. }
  117. - (void) requestLocation:(id)json resolve:(RCTPromiseResolveBlock)resolve
  118. {
  119. if (self.locationMgr == nil) {
  120. self.locationMgr = [[RNPLocation alloc] init];
  121. }
  122. NSString *type = [RCTConvert NSString:json];
  123. [self.locationMgr request:type completionHandler:resolve];
  124. }
  125. - (void) requestNotification:(id)json resolve:(RCTPromiseResolveBlock)resolve
  126. {
  127. NSArray *typeStrings = [RCTConvert NSArray:json];
  128. UIUserNotificationType types;
  129. if ([typeStrings containsObject:@"alert"])
  130. types = types | UIUserNotificationTypeAlert;
  131. if ([typeStrings containsObject:@"badge"])
  132. types = types | UIUserNotificationTypeBadge;
  133. if ([typeStrings containsObject:@"sound"])
  134. types = types | UIUserNotificationTypeSound;
  135. if (self.notificationMgr == nil) {
  136. self.notificationMgr = [[RNPNotification alloc] init];
  137. }
  138. [self.notificationMgr request:types completionHandler:resolve];
  139. }
  140. @end