https://github.com/nativescript-community/perms
An unified permissions API for NativeScript on iOS and Android.
https://github.com/nativescript-community/perms
Last synced: about 1 year ago
JSON representation
An unified permissions API for NativeScript on iOS and Android.
- Host: GitHub
- URL: https://github.com/nativescript-community/perms
- Owner: nativescript-community
- License: apache-2.0
- Created: 2019-04-05T16:18:44.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2025-03-27T17:29:03.000Z (about 1 year ago)
- Last Synced: 2025-05-07T13:05:44.202Z (about 1 year ago)
- Language: TypeScript
- Homepage: https://nativescript-community.github.io/perms/
- Size: 3.7 MB
- Stars: 13
- Watchers: 15
- Forks: 10
- Open Issues: 14
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
@nativescript-community/perms
An unified permissions API for NativeScript on iOS and Android.
|
|
|
| --- | ----------- |
| iOS Demo | Android Demo |
[](#table-of-contents)
[](#table-of-contents)
## Table of Contents
* [Installation](#installation)
* [API](#api)
* [Permissions statuses](#permissions-statuses)
* [Supported permissions types](#supported-permissions-types)
* [Methods](#methods)
* [iOS Notes](#ios-notes)
* [App Store submission disclaimer](#app-store-submission-disclaimer)
* [Android Notes](#android-notes)
* [Troubleshooting](#troubleshooting)
* [Q: iOS - App crashes as soon as I request permission](#q-ios---app-crashes-as-soon-as-i-request-permission)
* [Q: iOS - App crashes when I change permission from settings](#q-ios---app-crashes-when-i-change-permission-from-settings)
[](#installation)
[](#installation)
## Installation
Run the following command from the root of your project:
`ns plugin add @nativescript-community/perms`
[](#api)
[](#api)
## API
### Permissions statuses
Promises resolve into ```[status:Status, always:boolean]``` where status is one of these statuses:
| Return value | Notes |
| -------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `authorized` | User has authorized this permission |
| `denied` | User has denied this permission at least once. On iOS this means that the user will not be prompted again. Android users can be prompted multiple times until they select 'Never ask me again' |
| `limited` | **iOS** - this means the permission is granted but with limitations |
| `restricted` | **iOS** - this means user is not able to grant this permission, either because it's not supported by the device or because it has been blocked by parental controls. **Android** - this means that the user has selected 'Never ask me again' while denying permission |
| `undetermined` | User has not yet been prompted with a permission dialog |
### Supported permissions types
The current supported permissions are:
| | Type | iOS | Android |
| ------------------ | ------------------- | --- | ------- |
| Location | `location` | ✔ | ✔ |
| Camera | `camera` | ✔ | ✔ |
| Microphone | `microphone` | ✔ | ✔ |
| Photos | `photo` | ✔ | ✔ |
| Videos | `video` | ❌ | ✔ (api >= 33) |
| Audio | `audio` | ❌ | ✔ (api >= 33) |
| Contacts | `contacts` | ✔ | ✔ |
| Events | `event` | ✔ | ✔ |
| Bluetooth | `bluetooth` | ✔ | ✔(api >= 31) |
| Reminders | `reminder` | ✔ | ❌ |
| Push Notifications | `notification` | ✔ | ✔ (api >= 33) |
| Background Refresh | `backgroundRefresh` | ✔ | ❌ |
| Speech Recognition | `speechRecognition` | ✔ | ❌ |
| Media Library | `mediaLibrary` | ✔ | ❌ |
| Motion Activity | `motion` | ✔ | ❌ |
| Storage | `storage` | ❌️ | ✔ |
| Phone Call | `callPhone` | ❌️ | ✔ |
| Read SMS | `readSms` | ❌️ | ✔ |
| Receive SMS | `receiveSms` | ❌️ | ✔ |
| Media Location | `mediaLocation` | ❌️ | ✔(api >= 29) |
| Bluetooth Scan | `bluetoothScan` | ❌️ | ✔(api >= 31) |
| Bluetooth Connect | `bluetoothConnect` | ❌️ | ✔(api >= 31) |
### Methods
| Method Name | Arguments | Notes |
| ------------------- | --------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `check()` | `type` | - Returns a promise with the permission status. See iOS Notes for special cases |
| `request()` | `type` | - Accepts any permission type except `backgroundRefresh`. If the current status is `undetermined`, shows the permission dialog and returns a promise with the resulting status. Otherwise, immediately return a promise with the current status. See iOS Notes for special cases |
| `checkMultiple()` | `{[key:string]:Record}` | - Accepts an array of permission types and returns a promise with an object mapping permission types to statuses |
| `getTypes()` | _none_ | - Returns an array of valid permission types |
| `openSettings()` | _none_ | - Switches the user to the settings page of your app |
| `canOpenSettings()` | _none_ | - Returns a boolean indicating if the device supports switching to the settings page |
### iOS Notes
* Permission type `bluetooth` represents the status of the
`CBPeripheralManager`. Don't use this if only need `CBCentralManager`
* Permission type `location` accepts a second parameter for `request()` and
`check()`; the second parameter is a string, either `always` or `whenInUse`
(default).
* Permission type `notification` accepts a second parameter for `request()`. The
second parameter is an array with the desired alert types. Any combination of
`alert`, `badge` and `sound` (default requests all three).
* iOS 12+: The second parameter also takes this type inside of the array `providesAppNotificationSettings`.
* If you are not requesting mediaLibrary then you can remove MediaPlayer.framework from the xcode project
```js
import { check as checkPermission, request as requestPermission } from '@nativescript-community/perms';
// example
checkPermission('location', { type: 'always' }).then(response => {
this.setState({ locationPermission: response[0] })
})
requestPermission('location', { type: 'always' }).then(response => {
this.setState({ locationPermission: response[0] })
})
requestPermission('notification', { type: ['alert', 'badge'] }).then(
response => {
this.setState({ notificationPermission: response[0] })
},
)
```
* You cannot request microphone permissions on the simulator.
* With Xcode 8, you now need to add usage descriptions for each permission you
will request. Open Xcode ➜ `Info.plist` ➜ Add a key (starting with "Privacy -
...") with your kit specific permission.
Example: If you need Contacts permission you have to add the key `Privacy -
Contacts Usage Description`.

#### App Store submission disclaimer
If you need to submit you application to the AppStore, you need to add to your
`Info.plist` all `*UsageDescription` keys with a string value explaining to the
user how the app uses this data. **Even if you don't use them**.
So before submitting your app to the App Store, make sure that in your
`Info.plist` you have the following keys:
```xml
NSBluetoothPeripheralUsageDescription
Some description
NSCalendarsUsageDescription
Some description
NSCameraUsageDescription
Some description
NSLocationWhenInUseUsageDescription
Some description
NSPhotoLibraryAddUsageDescription
Some description
NSPhotoLibraryUsageDescription
Some description
NSSpeechRecognitionUsageDescription
Some description
NSAppleMusicUsageDescription
Some description
NSMotionUsageDescription
Some description
```
This is required because during the phase of processing in the App Store
submission, the system detects that you app contains code to request the
permission `X` but don't have the `UsageDescription` key and then it rejects the
build.
> Please note that it will only be shown to the users the usage descriptions of
> the permissions you really require in your app.
You can find more information about this issue in #46.
### Android Notes
* `check` and `request` also allows you to directly pass android permission(s) as a value or an array. This would allow to request any new permission without a required update of this plugin
* All required permissions also need to be included in the `AndroidManifest.xml`
file before they can be requested. Otherwise `request()` will immediately
return `denied`.
* You can request write access to any of these types by also including the
appropriate write permission in the `AndroidManifest.xml` file. Read more
[here](https://developer.android.com/guide/topics/security/permissions.html#normal-dangerous).
* Permissions are automatically accepted for **targetSdkVersion < 23** but you
can still use `check()` to check if the user has disabled them from Settings.
You might need to elevate the **targetSdkVersion** version in your
`build.gradle`:
```groovy
android {
compileSdkVersion 23 // ← set at least 23
buildToolsVersion "23.0.1" // ← set at least 23.0.0
defaultConfig {
minSdkVersion 16
targetSdkVersion 23 // ← set at least 23
// ...
```
[](#troubleshooting)
[](#troubleshooting)
## Troubleshooting
#### Q: iOS - App crashes as soon as I request permission
> A: Starting with Xcode 8, you need to add permission descriptions. See iOS
> notes for more details. Thanks to [@jesperlndk](https://github.com/jesperlndk)
> for discovering this.
#### Q: iOS - App crashes when I change permission from settings
> A: This is normal. iOS restarts your app when your privacy settings change.
> Just google "iOS crash permission change"
[](#demos-and-development)
## Demos and Development
### Repo Setup
The repo uses submodules. If you did not clone with ` --recursive` then you need to call
```
git submodule update --init
```
The package manager used to install and link dependencies must be `pnpm` or `yarn`. `npm` wont work.
To develop and test:
if you use `yarn` then run `yarn`
if you use `pnpm` then run `pnpm i`
**Interactive Menu:**
To start the interactive menu, run `npm start` (or `yarn start` or `pnpm start`). This will list all of the commonly used scripts.
### Build
```bash
npm run build.all
```
WARNING: it seems `yarn build.all` wont always work (not finding binaries in `node_modules/.bin`) which is why the doc explicitly uses `npm run`
### Demos
```bash
npm run demo.[ng|react|svelte|vue].[ios|android]
npm run demo.svelte.ios # Example
```
Demo setup is a bit special in the sense that if you want to modify/add demos you dont work directly in `demo-[ng|react|svelte|vue]`
Instead you work in `demo-snippets/[ng|react|svelte|vue]`
You can start from the `install.ts` of each flavor to see how to register new demos
[](#contributing)
## Contributing
### Update repo
You can update the repo files quite easily
First update the submodules
```bash
npm run update
```
Then commit the changes
Then update common files
```bash
npm run sync
```
Then you can run `yarn|pnpm`, commit changed files if any
### Update readme
```bash
npm run readme
```
### Update doc
```bash
npm run doc
```
### Publish
The publishing is completely handled by `lerna` (you can add `-- --bump major` to force a major release)
Simply run
```shell
npm run publish
```
### modifying submodules
The repo uses https:// for submodules which means you won't be able to push directly into the submodules.
One easy solution is t modify `~/.gitconfig` and add
```
[url "ssh://git@github.com/"]
pushInsteadOf = https://github.com/
```
[](#questions)
## Questions
If you have any questions/issues/comments please feel free to create an issue or start a conversation in the [NativeScript Community Discord](https://nativescript.org/discord).