|
| 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 | + |
| 9 | +@import Contacts; |
| 10 | + |
| 11 | +#import "ReactNativePermissions.h" |
| 12 | + |
| 13 | +#if __has_include(<React/RCTBridge.h>) |
| 14 | + #import <React/RCTBridge.h> |
| 15 | +#elif __has_include("RCTBridge.h") |
| 16 | + #import "RCTBridge.h" |
| 17 | +#else |
| 18 | + #import "React/RCTBridge.h" |
| 19 | +#endif |
| 20 | + |
| 21 | + |
| 22 | +#if __has_include("RCTConvert.h") |
| 23 | + #import "RCTConvert.h" |
| 24 | +#else |
| 25 | + #import <React/RCTConvert.h> |
| 26 | +#endif |
| 27 | + |
| 28 | +#if __has_include("RCTEventDispatcher.h") |
| 29 | + #import "RCTEventDispatcher.h" |
| 30 | +#else |
| 31 | + #import <React/RCTEventDispatcher.h> |
| 32 | +#endif |
| 33 | + |
| 34 | +#import "RNPLocation.h" |
| 35 | +#import "RNPBluetooth.h" |
| 36 | +#import "RNPNotification.h" |
| 37 | +#import "RNPAudioVideo.h" |
| 38 | +#import "RNPEvent.h" |
| 39 | +#import "RNPPhoto.h" |
| 40 | +#import "RNPContacts.h" |
| 41 | +#import "RNPBackgroundRefresh.h" |
| 42 | +#import "RNPSpeechRecognition.h" |
| 43 | + |
| 44 | +@interface ReactNativePermissions() |
| 45 | +@property (strong, nonatomic) RNPLocation *locationMgr; |
| 46 | +@property (strong, nonatomic) RNPNotification *notificationMgr; |
| 47 | +@property (strong, nonatomic) RNPBluetooth *bluetoothMgr; |
| 48 | +@end |
| 49 | + |
| 50 | +@implementation ReactNativePermissions |
| 51 | + |
| 52 | + |
| 53 | +RCT_EXPORT_MODULE(); |
| 54 | +@synthesize bridge = _bridge; |
| 55 | + |
| 56 | +#pragma mark Initialization |
| 57 | + |
| 58 | +- (instancetype)init |
| 59 | +{ |
| 60 | + if (self = [super init]) { |
| 61 | + } |
| 62 | + |
| 63 | + return self; |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * run on the main queue. |
| 68 | + */ |
| 69 | +- (dispatch_queue_t)methodQueue { |
| 70 | + return dispatch_get_main_queue(); |
| 71 | +} |
| 72 | + |
| 73 | + |
| 74 | +RCT_REMAP_METHOD(canOpenSettings, canOpenSettings:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) |
| 75 | +{ |
| 76 | + resolve(@(UIApplicationOpenSettingsURLString != nil)); |
| 77 | +} |
| 78 | + |
| 79 | + |
| 80 | +RCT_EXPORT_METHOD(openSettings:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) |
| 81 | +{ |
| 82 | + if (@(UIApplicationOpenSettingsURLString != nil)) { |
| 83 | + |
| 84 | + NSNotificationCenter * __weak center = [NSNotificationCenter defaultCenter]; |
| 85 | + id __block token = [center addObserverForName:UIApplicationDidBecomeActiveNotification |
| 86 | + object:nil |
| 87 | + queue:nil |
| 88 | + usingBlock:^(NSNotification *note) { |
| 89 | + [center removeObserver:token]; |
| 90 | + resolve(@YES); |
| 91 | + }]; |
| 92 | + |
| 93 | + NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; |
| 94 | + [[UIApplication sharedApplication] openURL:url]; |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | + |
| 99 | +RCT_REMAP_METHOD(getPermissionStatus, getPermissionStatus:(RNPType)type json:(id)json resolve:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) |
| 100 | +{ |
| 101 | + NSString *status; |
| 102 | + |
| 103 | + switch (type) { |
| 104 | + |
| 105 | + case RNPTypeLocation: { |
| 106 | + NSString *locationPermissionType = [RCTConvert NSString:json]; |
| 107 | + status = [RNPLocation getStatusForType:locationPermissionType]; |
| 108 | + break; |
| 109 | + } |
| 110 | + case RNPTypeCamera: |
| 111 | + status = [RNPAudioVideo getStatus:@"video"]; |
| 112 | + break; |
| 113 | + case RNPTypeMicrophone: |
| 114 | + status = [RNPAudioVideo getStatus:@"audio"]; |
| 115 | + break; |
| 116 | + case RNPTypePhoto: |
| 117 | + status = [RNPPhoto getStatus]; |
| 118 | + break; |
| 119 | + case RNPTypeContacts: |
| 120 | + status = [RNPContacts getStatus]; |
| 121 | + break; |
| 122 | + case RNPTypeEvent: |
| 123 | + status = [RNPEvent getStatus:@"event"]; |
| 124 | + break; |
| 125 | + case RNPTypeReminder: |
| 126 | + status = [RNPEvent getStatus:@"reminder"]; |
| 127 | + break; |
| 128 | + case RNPTypeBluetooth: |
| 129 | + status = [RNPBluetooth getStatus]; |
| 130 | + break; |
| 131 | + case RNPTypeNotification: |
| 132 | + status = [RNPNotification getStatus]; |
| 133 | + break; |
| 134 | + case RNPTypeBackgroundRefresh: |
| 135 | + status = [RNPBackgroundRefresh getStatus]; |
| 136 | + break; |
| 137 | + case RNPTypeSpeechRecognition: |
| 138 | + status = [RNPSpeechRecognition getStatus]; |
| 139 | + break; |
| 140 | + default: |
| 141 | + break; |
| 142 | + } |
| 143 | + |
| 144 | + resolve(status); |
| 145 | +} |
| 146 | + |
| 147 | +RCT_REMAP_METHOD(requestPermission, permissionType:(RNPType)type json:(id)json resolve:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) |
| 148 | +{ |
| 149 | + NSString *status; |
| 150 | + |
| 151 | + switch (type) { |
| 152 | + case RNPTypeLocation: |
| 153 | + return [self requestLocation:json resolve:resolve]; |
| 154 | + case RNPTypeCamera: |
| 155 | + return [RNPAudioVideo request:@"video" completionHandler:resolve]; |
| 156 | + case RNPTypeMicrophone: |
| 157 | + return [RNPAudioVideo request:@"audio" completionHandler:resolve]; |
| 158 | + case RNPTypePhoto: |
| 159 | + return [RNPPhoto request:resolve]; |
| 160 | + case RNPTypeContacts: |
| 161 | + return [RNPContacts request:resolve]; |
| 162 | + case RNPTypeEvent: |
| 163 | + return [RNPEvent request:@"event" completionHandler:resolve]; |
| 164 | + case RNPTypeReminder: |
| 165 | + return [RNPEvent request:@"reminder" completionHandler:resolve]; |
| 166 | + case RNPTypeBluetooth: |
| 167 | + return [self requestBluetooth:resolve]; |
| 168 | + case RNPTypeNotification: |
| 169 | + return [self requestNotification:json resolve:resolve]; |
| 170 | + case RNPTypeSpeechRecognition: |
| 171 | + return [RNPSpeechRecognition request:resolve]; |
| 172 | + default: |
| 173 | + break; |
| 174 | + } |
| 175 | + |
| 176 | + |
| 177 | +} |
| 178 | + |
| 179 | +- (void) requestLocation:(id)json resolve:(RCTPromiseResolveBlock)resolve |
| 180 | +{ |
| 181 | + if (self.locationMgr == nil) { |
| 182 | + self.locationMgr = [[RNPLocation alloc] init]; |
| 183 | + } |
| 184 | + |
| 185 | + NSString *type = [RCTConvert NSString:json]; |
| 186 | + |
| 187 | + [self.locationMgr request:type completionHandler:resolve]; |
| 188 | +} |
| 189 | + |
| 190 | +- (void) requestNotification:(id)json resolve:(RCTPromiseResolveBlock)resolve |
| 191 | +{ |
| 192 | + NSArray *typeStrings = [RCTConvert NSArray:json]; |
| 193 | + |
| 194 | + UIUserNotificationType types; |
| 195 | + if ([typeStrings containsObject:@"alert"]) |
| 196 | + types = types | UIUserNotificationTypeAlert; |
| 197 | + |
| 198 | + if ([typeStrings containsObject:@"badge"]) |
| 199 | + types = types | UIUserNotificationTypeBadge; |
| 200 | + |
| 201 | + if ([typeStrings containsObject:@"sound"]) |
| 202 | + types = types | UIUserNotificationTypeSound; |
| 203 | + |
| 204 | + |
| 205 | + if (self.notificationMgr == nil) { |
| 206 | + self.notificationMgr = [[RNPNotification alloc] init]; |
| 207 | + } |
| 208 | + |
| 209 | + [self.notificationMgr request:types completionHandler:resolve]; |
| 210 | + |
| 211 | +} |
| 212 | + |
| 213 | + |
| 214 | +- (void) requestBluetooth:(RCTPromiseResolveBlock)resolve |
| 215 | +{ |
| 216 | + if (self.bluetoothMgr == nil) { |
| 217 | + self.bluetoothMgr = [[RNPBluetooth alloc] init]; |
| 218 | + } |
| 219 | + |
| 220 | + [self.bluetoothMgr request:resolve]; |
| 221 | +} |
| 222 | + |
| 223 | + |
| 224 | + |
| 225 | + |
| 226 | +@end |
0 commit comments