/
permissions.ios.js
108 lines (87 loc) · 2.58 KB
/
permissions.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
// @flow
import { NativeModules } from 'react-native'
const PermissionsIOS = NativeModules.ReactNativePermissions
type Status = 'authorized' | 'denied' | 'restricted' | 'undetermined'
type Rationale = { title: string, message: string }
type CheckOptions = string | { type: string }
type RequestOptions = string | { type: string, rationale?: Rationale }
const permissionTypes = [
'location',
'camera',
'microphone',
'photo',
'contacts',
'event',
'reminder',
'bluetooth',
'notification',
'backgroundRefresh',
'speechRecognition',
'motion',
]
const DEFAULTS = {
location: 'whenInUse',
notification: ['alert', 'badge', 'sound'],
}
class ReactNativePermissions {
canOpenSettings: () => Promise<boolean> = () =>
PermissionsIOS.canOpenSettings()
openSettings: () => Promise<*> = () => PermissionsIOS.openSettings()
getTypes: () => Array<string> = () => permissionTypes
check = (permission: string, options?: CheckOptions): Promise<Status> => {
if (!permissionTypes.includes(permission)) {
const error = new Error(
`ReactNativePermissions: ${
permission
} is not a valid permission type on iOS`,
)
return Promise.reject(error)
}
let type
if (typeof options === 'string') {
type = options
} else if (options && options.type) {
type = options.type
}
return PermissionsIOS.getPermissionStatus(
permission,
type || DEFAULTS[permission],
)
}
request = (permission: string, options?: RequestOptions): Promise<Status> => {
if (!permissionTypes.includes(permission)) {
const error = new Error(
`ReactNativePermissions: ${
permission
} is not a valid permission type on iOS`,
)
return Promise.reject(error)
}
if (permission == 'backgroundRefresh') {
const error = new Error(
'ReactNativePermissions: You cannot request backgroundRefresh',
)
return Promise.reject(error)
}
let type
if (typeof options === 'string') {
type = options
} else if (options && options.type) {
type = options.type
}
return PermissionsIOS.requestPermission(
permission,
type || DEFAULTS[permission],
)
}
checkMultiple = (permissions: Array<string>): Promise<{ [string]: string }> =>
Promise.all(permissions.map(permission => this.check(permission))).then(
result =>
result.reduce((acc, value, index) => {
const name = permissions[index]
acc[name] = value
return acc
}, {}),
)
}
export default new ReactNativePermissions()