Skip to content

Commit 39197f6

Browse files
committed
resolves yannbf#2
1 parent 2ac41b7 commit 39197f6

File tree

3 files changed

+29
-56
lines changed

3 files changed

+29
-56
lines changed
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1+
On newer android versions, permissions are handled differently. In most of the cases, the plugins take care of the permissions, but sometimes there are some exceptions.
12
In order to ask or check for user permissions, the Diagnostic plugin is needed.
23

34
Docs: https://ionicframework.com/docs/v2/native/diagnostic/
45

56
You have to install the plugin with:
67
```sh
7-
$ ionic plugin add cordova.plugins.diagnostic --save
8-
```
9-
10-
8+
$ ionic plugin add cordova.plugins.diagnostic --save

src/pages/miscellaneous/runtime-permissions/runtime-permissions.html

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,12 @@
55
Ionic pages and navigation.
66
-->
77
<ion-header>
8-
98
<ion-navbar>
109
<ion-title>RuntimePermissions</ion-title>
1110
</ion-navbar>
12-
1311
</ion-header>
14-
15-
1612
<ion-content padding>
17-
<ion-grid>
18-
<ion-row>
19-
<ion-col>
20-
<button ion-button full (click)="requestPermission(PERMISSION.READ_EXTERNAL)">Read External</button>
21-
</ion-col>
22-
<ion-col>
23-
<button ion-button full (click)="requestPermission(PERMISSION.WRITE_EXTERNAL)">Write External</button>
24-
</ion-col>
25-
</ion-row>
26-
<ion-row>
27-
<ion-col>
28-
<button ion-button full (click)="requestPermission(PERMISSION.CAMERA)">Use Camera</button>
29-
</ion-col>
30-
<ion-col>
31-
<button ion-button full (click)="requestPermission(PERMISSION.READ_CONTACTS)">Read Contacts</button>
32-
</ion-col>
33-
</ion-row>
34-
<ion-row>
35-
<ion-col>
36-
<button ion-button full (click)="requestPermission(PERMISSION.RECORD_AUDIO)">Record Audio</button>
37-
</ion-col>
38-
<ion-col>
39-
<button ion-button full (click)="requestPermission(PERMISSION.CALL_PHONE)">Call Phone</button>
40-
</ion-col>
41-
</ion-row>
42-
</ion-grid>
13+
<button ion-button full (click)="requestPermission(PERMISSION.WRITE_EXTERNAL)">Read/Write External</button>
14+
<button ion-button full (click)="requestCameraPermission()">Use Camera</button>
4315
<button ion-button full (click)="requestAllPermissions()">All requests at once</button>
44-
45-
46-
</ion-content>
16+
</ion-content>

src/pages/miscellaneous/runtime-permissions/runtime-permissions.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,53 @@ import { NavController, NavParams } from 'ionic-angular';
88
})
99
export class RuntimePermissionsPage {
1010

11+
// You can add many other permissions
1112
PERMISSION = {
1213
WRITE_EXTERNAL: Diagnostic.permission.WRITE_EXTERNAL_STORAGE,
1314
READ_EXTERNAL: Diagnostic.permission.READ_EXTERNAL_STORAGE,
1415
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,
1816
};
1917

2018
constructor(public navCtrl: NavController, public navParams: NavParams) { }
2119

20+
// You can use this kind of method, which is passing a permission value..
2221
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+
}
3428
}, error => {
3529
console.error('permission error:', error);
3630
});
3731
}
3832

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.
4036
Diagnostic.isCameraAuthorized().then((authorized) => {
4137
if (authorized) {
38+
alert('camera is already authorized!');
4239
} else {
43-
Diagnostic.requestContactsAuthorization
44-
Diagnostic.requestContactsAuthorization().then((status) => {
40+
Diagnostic.requestCameraAuthorization().then((status) => {
4541
if (status == Diagnostic.permissionStatus.GRANTED) {
46-
42+
alert('Permission granted!');
4743
} else {
48-
// Permissions not granted
44+
alert('Permission not granted. STATUS: ' + status);
4945
}
5046
});
5147
}
5248
});
5349
}
5450

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+
}
5560
}

0 commit comments

Comments
 (0)