forked from samarchyan/react-native-permissions
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathReactNativePermissions.m
125 lines (94 loc) · 3.12 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
//
// ReactNativePermissions.m
// ReactNativePermissions
//
// Created by Yonah Forst on 18/02/16.
// Copyright © 2016 Yonah Forst. All rights reserved.
//
#import "ReactNativePermissions.h"
#if __has_include(<React/RCTConvert.h>)
#import <React/RCTConvert.h>
#elif __has_include("React/RCTConvert.h")
#import "React/RCTConvert.h"
#else
#import "RCTConvert.h"
#endif
#if __has_include(<React/RCTEventDispatcher.h>)
#import <React/RCTEventDispatcher.h>
#elif __has_include("React/RCTEventDispatcher.h")
#import "React/RCTEventDispatcher.h"
#else
#import "RCTEventDispatcher.h"
#endif
#import "RNPAudioVideo.h"
#import "RNPPhoto.h"
@interface ReactNativePermissions()
@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 RNPTypeCamera:
status = [RNPAudioVideo getStatus:@"video"];
break;
case RNPTypeMicrophone:
status = [RNPAudioVideo getStatus:@"audio"];
break;
case RNPTypePhoto:
status = [RNPPhoto 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 RNPTypeCamera:
return [RNPAudioVideo request:@"video" completionHandler:resolve];
case RNPTypeMicrophone:
return [RNPAudioVideo request:@"audio" completionHandler:resolve];
case RNPTypePhoto:
return [RNPPhoto request:resolve];
default:
break;
}
}
@end