Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Commit

Permalink
📝 add explanation about permissions to readme
Browse files Browse the repository at this point in the history
  • Loading branch information
tafelnl committed Dec 1, 2020
1 parent f7bc666 commit 17a200a
Showing 1 changed file with 78 additions and 1 deletion.
79 changes: 78 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,84 @@ The latter will just appear a little slower to the user.

### Permissions

This plugin does not handle permissions (yet). Your app will not crash when you call this API without having the right permission granted. But it will not be able to start up the camera. So you will have to take care of permissions yourself.
This plugin does not automatically handle permissions. But the plugin _does_ have a utility method to check and request the permission. You will have to request the permission from JavaScript. A simple example follows:

```js
const checkPermission = async () => {
// check or request permission
const status = await BarcodeScanner.checkPermission({ force: true });

if (status.granted) {
// the user granted permission
return true;
}

return false;
};
```

A more detailed and more UX-optimized example:

```js
const didUserGrantPermission = async () => {
const { BarcodeScanner } = Plugins;
// check if user already granted permission
const status = await BarcodeScanner.checkPermission({ force: false });

if (status.granted) {
// user granted permission
return true;
}

if (status.denied) {
// user denied permission
return false;
}

if (status.asked) {
// system requested the user for permission during this call
// only possible when force set to true
}

if (status.neverAsked) {
// user has not been requested this permission before
// it is advised to show the user some sort of prompt
// this way you will not waste your only chance to ask for the permission
const c = confirm(
'We need your permission to use your camera to be able to scan barcodes',
);
if (!c) {
return false;
}
}

if (status.restricted || status.unknown) {
// ios only
// probably means the permission has been denied
return false;
}

// user has not denied permission
// but the user also has not yet granted the permission
// so request it
const statusRequest = await BarcodeScanner.checkPermission({ force: true });

if (status.asked) {
// system requested the user for permission during this call
// only possible when force set to true
}

if (statusRequest.granted) {
// the user did grant the permission now
return true;
}

// user did not grant the permission, so he must have declined the request
return false;
};

checkPermission();
```

## TODO

Expand Down

0 comments on commit 17a200a

Please sign in to comment.