forked from zoontek/react-native-permissions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReactNativePermissions.ios.js
109 lines (96 loc) · 2.53 KB
/
ReactNativePermissions.ios.js
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
'use strict';
var React = require('react-native');
var RNPermissions = React.NativeModules.ReactNativePermissions;
const RNPTypes = [
'location',
'camera',
'microphone',
'photo',
'contacts',
'event',
'reminder',
'bluetooth',
'notification',
'backgroundRefresh',
]
class ReactNativePermissions {
constructor() {
//legacy support
this.StatusUndetermined = 'undetermined'
this.StatusDenied = 'denied'
this.StatusAuthorized = 'authorized'
this.StatusRestricted = 'restricted'
RNPTypes.forEach(type => {
let methodName = `${type}PermissionStatus`
this[methodName] = p => {
console.warn(`ReactNativePermissions: ${methodName} is depricated. Use getPermissionStatus('${type}') instead.`)
return this.getPermissionStatus(p == 'reminder' ? p : type)
}
})
}
canOpenSettings() {
return RNPermissions.canOpenSettings()
}
openSettings() {
return RNPermissions.openSettings()
}
getPermissionTypes() {
return RNPTypes;
}
getPermissionStatus(permission) {
if (RNPTypes.includes(permission)) {
return RNPermissions.getPermissionStatus(permission)
} else {
return Promise.reject(`ReactNativePermissions: ${permission} is not a valid permission type`)
}
}
requestPermission(permission, type) {
switch (permission) {
case "location":
return RNPermissions.requestLocation(type || 'always')
case "camera":
return RNPermissions.requestCamera();
case "microphone":
return RNPermissions.requestMicrophone();
case "photo":
return RNPermissions.requestPhoto();
case "contacts":
return RNPermissions.requestContacts();
case "event":
return RNPermissions.requestEvent();
case "reminder":
return RNPermissions.requestReminder();
case "bluetooth":
return RNPermissions.requestBluetooth();
case "notification":
return RNPermissions.requestNotification(type || ['alert', 'badge', 'sound'])
case "backgroundRefresh":
return Promise.reject('You cannot request backgroundRefresh')
default:
return Promise.reject('invalid type: ' + type)
}
}
//recursive funciton to chain a promises for a list of permissions
checkMultiplePermissions(permissions) {
let i = permissions.length
let that = this
const obj = {}
function processNext() {
i--
let p = permissions[i]
if (!p) {
return obj
}
return that.getPermissionStatus(p)
.then(res => {
obj[p] = res
return processNext()
}).catch(e => {
console.warn(e)
return processNext()
})
}
return processNext()
}
}
module.exports = new ReactNativePermissions()