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