@@ -8,48 +8,53 @@ import { NavController, NavParams } from 'ionic-angular';
8
8
} )
9
9
export class RuntimePermissionsPage {
10
10
11
+ // You can add many other permissions
11
12
PERMISSION = {
12
13
WRITE_EXTERNAL : Diagnostic . permission . WRITE_EXTERNAL_STORAGE ,
13
14
READ_EXTERNAL : Diagnostic . permission . READ_EXTERNAL_STORAGE ,
14
15
CAMERA : Diagnostic . permission . CAMERA ,
15
- READ_CONTACTS : Diagnostic . permission . READ_CONTACTS ,
16
- RECORD_AUDIO : Diagnostic . permission . RECORD_AUDIO ,
17
- CALL_PHONE : Diagnostic . permission . CALL_PHONE ,
18
16
} ;
19
17
20
18
constructor ( public navCtrl : NavController , public navParams : NavParams ) { }
21
19
20
+ // You can use this kind of method, which is passing a permission value..
22
21
requestPermission ( permission ) {
23
- Diagnostic . requestRuntimePermission ( permission ) . then ( ( ) => {
24
- console . log ( 'permission granted' ) ;
25
- } , error => {
26
- console . error ( 'permission error:' , error ) ;
27
- } ) ;
28
- }
29
-
30
- requestAllPermissions ( ) {
31
- var permissions = Object . keys ( this . PERMISSION ) . map ( k => this . PERMISSION [ k ] ) ;
32
- Diagnostic . requestRuntimePermissions ( permissions ) . then ( ( ) => {
33
- console . log ( 'permission granted' ) ;
22
+ Diagnostic . requestRuntimePermission ( permission ) . then ( ( status ) => {
23
+ if ( status == Diagnostic . permissionStatus . GRANTED ) {
24
+ alert ( 'Permission granted!' ) ;
25
+ } else {
26
+ alert ( 'Permission not granted. STATUS: ' + status ) ;
27
+ }
34
28
} , error => {
35
29
console . error ( 'permission error:' , error ) ;
36
30
} ) ;
37
31
}
38
32
39
- funca ( ) {
33
+ // ..Or opt for Diagnostic's specific methods, like requestCameraAuthorization.
34
+ requestCameraPermission ( ) {
35
+ // Checks if permission is already granted. Otherwise, asks for it.
40
36
Diagnostic . isCameraAuthorized ( ) . then ( ( authorized ) => {
41
37
if ( authorized ) {
38
+ alert ( 'camera is already authorized!' ) ;
42
39
} else {
43
- Diagnostic . requestContactsAuthorization
44
- Diagnostic . requestContactsAuthorization ( ) . then ( ( status ) => {
40
+ Diagnostic . requestCameraAuthorization ( ) . then ( ( status ) => {
45
41
if ( status == Diagnostic . permissionStatus . GRANTED ) {
46
-
42
+ alert ( 'Permission granted!' ) ;
47
43
} else {
48
- // Permissions not granted
44
+ alert ( 'Permission not granted. STATUS: ' + status ) ;
49
45
}
50
46
} ) ;
51
47
}
52
48
} ) ;
53
49
}
54
50
51
+ // There is also a method that takes an array of permissions to ask for them at once
52
+ requestAllPermissions ( ) {
53
+ var permissions = Object . keys ( this . PERMISSION ) . map ( k => this . PERMISSION [ k ] ) ;
54
+ Diagnostic . requestRuntimePermissions ( permissions ) . then ( ( status ) => {
55
+ alert ( JSON . stringify ( status ) ) ;
56
+ } , error => {
57
+ console . error ( 'permission error:' , error ) ;
58
+ } ) ;
59
+ }
55
60
}
0 commit comments