ReactNativePermissions.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 "RNPPhoto.h"
  34. @interface ReactNativePermissions()
  35. @property (strong, nonatomic) RNPLocation *locationMgr;
  36. @property (strong, nonatomic) RNPNotification *notificationMgr;
  37. @end
  38. @implementation ReactNativePermissions
  39. RCT_EXPORT_MODULE();
  40. @synthesize bridge = _bridge;
  41. + (BOOL)requiresMainQueueSetup
  42. {
  43. return YES;
  44. }
  45. #pragma mark Initialization
  46. - (instancetype)init
  47. {
  48. if (self = [super init]) {
  49. }
  50. return self;
  51. }
  52. /**
  53. * run on the main queue.
  54. */
  55. - (dispatch_queue_t)methodQueue {
  56. return dispatch_get_main_queue();
  57. }
  58. RCT_REMAP_METHOD(canOpenSettings, canOpenSettings:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  59. {
  60. resolve(@(UIApplicationOpenSettingsURLString != nil));
  61. }
  62. RCT_EXPORT_METHOD(openSettings:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  63. {
  64. if (@(UIApplicationOpenSettingsURLString != nil)) {
  65. NSNotificationCenter * __weak center = [NSNotificationCenter defaultCenter];
  66. id __block token = [center addObserverForName:UIApplicationDidBecomeActiveNotification
  67. object:nil
  68. queue:nil
  69. usingBlock:^(NSNotification *note) {
  70. [center removeObserver:token];
  71. resolve(@YES);
  72. }];
  73. NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  74. [[UIApplication sharedApplication] openURL:url];
  75. }
  76. }
  77. RCT_REMAP_METHOD(getPermissionStatus, getPermissionStatus:(RNPType)type json:(id)json resolve:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  78. {
  79. NSString *status;
  80. switch (type) {
  81. case RNPTypeLocation: {
  82. NSString *locationPermissionType = [RCTConvert NSString:json];
  83. status = [RNPLocation getStatusForType:locationPermissionType];
  84. break;
  85. }
  86. case RNPTypePhoto:
  87. status = [RNPPhoto getStatus];
  88. break;
  89. case RNPTypeNotification:
  90. status = [RNPNotification getStatus];
  91. break;
  92. default:
  93. break;
  94. }
  95. resolve(status);
  96. }
  97. RCT_REMAP_METHOD(requestPermission, permissionType:(RNPType)type json:(id)json resolve:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  98. {
  99. NSString *status;
  100. switch (type) {
  101. case RNPTypeLocation:
  102. return [self requestLocation:json resolve:resolve];
  103. case RNPTypePhoto:
  104. return [RNPPhoto request:resolve];
  105. case RNPTypeNotification:
  106. return [self requestNotification:json resolve:resolve];
  107. default:
  108. break;
  109. }
  110. }
  111. - (void) requestLocation:(id)json resolve:(RCTPromiseResolveBlock)resolve
  112. {
  113. if (self.locationMgr == nil) {
  114. self.locationMgr = [[RNPLocation alloc] init];
  115. }
  116. NSString *type = [RCTConvert NSString:json];
  117. [self.locationMgr request:type completionHandler:resolve];
  118. }
  119. - (void) requestNotification:(id)json resolve:(RCTPromiseResolveBlock)resolve
  120. {
  121. NSArray *typeStrings = [RCTConvert NSArray:json];
  122. UIUserNotificationType types;
  123. if ([typeStrings containsObject:@"alert"])
  124. types = types | UIUserNotificationTypeAlert;
  125. if ([typeStrings containsObject:@"badge"])
  126. types = types | UIUserNotificationTypeBadge;
  127. if ([typeStrings containsObject:@"sound"])
  128. types = types | UIUserNotificationTypeSound;
  129. if (self.notificationMgr == nil) {
  130. self.notificationMgr = [[RNPNotification alloc] init];
  131. }
  132. [self.notificationMgr request:types completionHandler:resolve];
  133. }
  134. @end