Skip to content

Commit c2f3055

Browse files
Update README.md (#1179)
1 parent 55d1903 commit c2f3055

File tree

1 file changed

+71
-45
lines changed

1 file changed

+71
-45
lines changed

permission_handler/README.md

Lines changed: 71 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
[![pub package](https://img.shields.io/pub/v/permission_handler.svg)](https://pub.dartlang.org/packages/permission_handler) [![Build status](https://github.com/Baseflow/flutter-permission-handler/actions/workflows/permission_handler.yaml/badge.svg?branch=master)](https://github.com/Baseflow/flutter-permission-handler/actions/workflows/permission_handler.yaml) [![style: effective dart](https://img.shields.io/badge/style-effective_dart-40c4ff.svg)](https://github.com/tenhobi/effective_dart) [![codecov](https://codecov.io/gh/Baseflow/flutter-permission-handler/branch/master/graph/badge.svg)](https://codecov.io/gh/Baseflow/flutter-permission-handler)
22

33
On most operating systems, permissions aren't just granted to apps at install time.
4-
Rather, developers have to ask the user for permissions while the app is running.
4+
Rather, developers have to ask the user for permission while the app is running.
55

66
This plugin provides a cross-platform (iOS, Android) API to request permissions and check their status.
7-
You can also open the device's app settings so users can grant a permission.
8-
On Android, you can show a rationale for requesting a permission.
7+
You can also open the device's app settings so users can grant permission.
8+
On Android, you can show a rationale for requesting permission.
99

1010
See the [FAQ](#faq) section for more information on common questions when using the permission_handler plugin.
1111

@@ -16,13 +16,13 @@ While the permissions are being requested during runtime, you'll still need to t
1616
<details>
1717
<summary>Android</summary>
1818

19-
**Upgrade pre 1.12 Android projects**
19+
**Upgrade pre-1.12 Android projects**
2020

21-
Since version 4.4.0 this plugin is implemented using the Flutter 1.12 Android plugin APIs. Unfortunately this means App developers also need to migrate their Apps to support the new Android infrastructure. You can do so by following the [Upgrading pre 1.12 Android projects](https://github.com/flutter/flutter/wiki/Upgrading-pre-1.12-Android-projects) migration guide. Failing to do so might result in unexpected behaviour. Most common known error is the permission_handler not returning after calling the `.request()` method on a permission.
21+
Since version 4.4.0 this plugin is implemented using the Flutter 1.12 Android plugin APIs. Unfortunately, this means App developers also need to migrate their Apps to support the new Android infrastructure. You can do so by following the [Upgrading pre 1.12 Android projects](https://github.com/flutter/flutter/wiki/Upgrading-pre-1.12-Android-projects) migration guide. Failing to do so might result in unexpected behavior. The most common known error is the permission_handler not returning after calling the `.request()` method on permission.
2222

2323
**AndroidX**
2424

25-
As of version 3.1.0 the <kbd>permission_handler</kbd> plugin switched to the AndroidX version of the Android Support Libraries. This means you need to make sure your Android project is also upgraded to support AndroidX. Detailed instructions can be found [here](https://flutter.dev/docs/development/packages-and-plugins/androidx-compatibility).
25+
As of version 3.1.0, the <kbd>permission_handler</kbd> plugin switched to the AndroidX version of the Android Support Libraries. This means you need to make sure your Android project is also upgraded to support AndroidX. Detailed instructions can be found [here](https://flutter.dev/docs/development/packages-and-plugins/androidx-compatibility).
2626

2727
The TL;DR version is:
2828

@@ -45,7 +45,7 @@ android {
4545
3. Make sure you replace all the `android.` dependencies to their AndroidX counterparts (a full list can be found [here](https://developer.android.com/jetpack/androidx/migrate)).
4646

4747
Add permissions to your `AndroidManifest.xml` file.
48-
There's a `debug`, `main` and `profile` version which are chosen depending on how you start your app.
48+
There are `debug`, `main`, and `profile` versions which are chosen depending on how you start your app.
4949
In general, it's sufficient to add permission only to the `main` version.
5050
[Here](https://github.com/Baseflow/flutter-permission-handler/blob/master/permission_handler/example/android/app/src/main/AndroidManifest.xml)'s an example `AndroidManifest.xml` with a complete list of all possible permissions.
5151

@@ -57,82 +57,82 @@ In general, it's sufficient to add permission only to the `main` version.
5757
Add permission to your `Info.plist` file.
5858
[Here](https://github.com/Baseflow/flutter-permission-handler/blob/master/permission_handler/example/ios/Runner/Info.plist)'s an example `Info.plist` with a complete list of all possible permissions.
5959

60-
> IMPORTANT: ~~You will have to include all permission options when you want to submit your App.~~ This is because the `permission_handler` plugin touches all different SDKs and because the static code analyser (run by Apple upon App submission) detects this and will assert if it cannot find a matching permission option in the `Info.plist`. More information about this can be found [here](https://github.com/Baseflow/flutter-permission-handler/issues/26).
60+
> IMPORTANT: ~~You will have to include all permission options when you want to submit your App.~~ This is because the `permission_handler` plugin touches all different SDKs and because the static code analyzer (run by Apple upon App submission) detects this and will assert if it cannot find a matching permission option in the `Info.plist`. More information about this can be found [here](https://github.com/Baseflow/flutter-permission-handler/issues/26).
6161
6262
The <kbd>permission_handler</kbd> plugin use [macros](https://github.com/Baseflow/flutter-permission-handler/blob/master/permission_handler_apple/ios/Classes/PermissionHandlerEnums.h) to control whether a permission is enabled.
6363

64-
You must list permission you want to use in your application:
64+
You must list the permission you want to use in your application:
6565

6666
1. Add the following to your `Podfile` file:
6767

6868
```ruby
6969
post_install do |installer|
7070
installer.pods_project.targets.each do |target|
7171
... # Here are some configurations automatically generated by flutter
72-
72+
7373
# Start of the permission_handler configuration
7474
target.build_configurations.each do |config|
75-
75+
7676
# You can enable the permissions needed here. For example to enable camera
7777
# permission, just remove the `#` character in front so it looks like this:
7878
#
7979
# ## dart: PermissionGroup.camera
8080
# 'PERMISSION_CAMERA=1'
8181
#
82-
# Preprocessor definitions can be found in: https://github.com/Baseflow/flutter-permission-handler/blob/master/permission_handler_apple/ios/Classes/PermissionHandlerEnums.h
82+
# Preprocessor definitions can be found at: https://github.com/Baseflow/flutter-permission-handler/blob/master/permission_handler_apple/ios/Classes/PermissionHandlerEnums.h
8383
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
8484
'$(inherited)',
85-
85+
8686
## dart: PermissionGroup.calendar
8787
# 'PERMISSION_EVENTS=1',
88-
88+
8989
## dart: PermissionGroup.reminders
9090
# 'PERMISSION_REMINDERS=1',
91-
91+
9292
## dart: PermissionGroup.contacts
9393
# 'PERMISSION_CONTACTS=1',
94-
94+
9595
## dart: PermissionGroup.camera
9696
# 'PERMISSION_CAMERA=1',
97-
97+
9898
## dart: PermissionGroup.microphone
9999
# 'PERMISSION_MICROPHONE=1',
100-
100+
101101
## dart: PermissionGroup.speech
102102
# 'PERMISSION_SPEECH_RECOGNIZER=1',
103-
103+
104104
## dart: PermissionGroup.photos
105105
# 'PERMISSION_PHOTOS=1',
106-
106+
107107
## dart: [PermissionGroup.location, PermissionGroup.locationAlways, PermissionGroup.locationWhenInUse]
108108
# 'PERMISSION_LOCATION=1',
109-
109+
110110
## dart: PermissionGroup.notification
111111
# 'PERMISSION_NOTIFICATIONS=1',
112-
112+
113113
## dart: PermissionGroup.mediaLibrary
114114
# 'PERMISSION_MEDIA_LIBRARY=1',
115-
115+
116116
## dart: PermissionGroup.sensors
117-
# 'PERMISSION_SENSORS=1',
118-
117+
# 'PERMISSION_SENSORS=1',
118+
119119
## dart: PermissionGroup.bluetooth
120120
# 'PERMISSION_BLUETOOTH=1',
121-
121+
122122
## dart: PermissionGroup.appTrackingTransparency
123123
# 'PERMISSION_APP_TRACKING_TRANSPARENCY=1',
124-
124+
125125
## dart: PermissionGroup.criticalAlerts
126126
# 'PERMISSION_CRITICAL_ALERTS=1'
127127
]
128-
129-
end
128+
129+
end
130130
# End of the permission_handler configuration
131131
end
132132
end
133133
```
134134

135-
2. Remove the `#` character in front of the permission you do want to use. For example if you need access to the calendar make sure the code looks like this:
135+
2. Remove the `#` character in front of the permission you want to use. For example, if you need access to the calendar make sure the code looks like this:
136136

137137
```ruby
138138
## dart: PermissionGroup.calendar
@@ -157,8 +157,9 @@ You must list permission you want to use in your application:
157157
| PermissionGroup.mediaLibrary | NSAppleMusicUsageDescription, kTCCServiceMediaLibrary | PERMISSION_MEDIA_LIBRARY |
158158
| PermissionGroup.sensors | NSMotionUsageDescription | PERMISSION_SENSORS |
159159
| PermissionGroup.bluetooth | NSBluetoothAlwaysUsageDescription, NSBluetoothPeripheralUsageDescription | PERMISSION_BLUETOOTH |
160-
| PermissionGroup.appTrackingTransparency | NSUserTrackingUsageDescription | PERMISSION_APP_TRACKING_TRANSPARENCY |
160+
| PermissionGroup.appTrackingTransparency | NSUserTrackingUsageDescription | PERMISSION_APP_TRACKING_TRANSPARENCY |
161161
| PermissionGroup.criticalAlerts | PermissionGroupCriticalAlerts | PERMISSION_CRITICAL_ALERTS |
162+
162163
4. Clean & Rebuild
163164

164165
</details>
@@ -171,15 +172,40 @@ You can get a `Permission`'s `status`, which is either `granted`, `denied`, `res
171172
```dart
172173
var status = await Permission.camera.status;
173174
if (status.isDenied) {
174-
// We didn't ask for permission yet or the permission has been denied before, but not permanently.
175+
// We haven't asked for permission yet or the permission has been denied before, but not permanently.
175176
}
176177
177-
// You can can also directly ask the permission about its status.
178+
// You can also directly ask permission about its status.
178179
if (await Permission.location.isRestricted) {
179-
// The OS restricts access, for example because of parental controls.
180+
// The OS restricts access, for example, because of parental controls.
180181
}
181182
```
182183

184+
Can use also this style for better readability of your code when using the `Permission` class.
185+
186+
```dart
187+
await Permission.camera
188+
.onDeniedCallback(() {
189+
// Your code
190+
})
191+
.onGrantedCallback(() {
192+
// Your code
193+
})
194+
.onPermanentlyDeniedCallback(() {
195+
// Your code
196+
})
197+
.onRestrictedCallback(() {
198+
// Your code
199+
})
200+
.onLimitedCallback(() {
201+
// Your code
202+
})
203+
.onProvisionalCallback(() {
204+
// Your code
205+
})
206+
.request();
207+
```
208+
183209
Call `request()` on a `Permission` to request it.
184210
If it has already been granted before, nothing happens.
185211
`request()` returns the new status of the `Permission`.
@@ -197,7 +223,7 @@ Map<Permission, PermissionStatus> statuses = await [
197223
print(statuses[Permission.location]);
198224
```
199225

200-
Some permissions, for example location or acceleration sensor permissions, have an associated service, which can be `enabled` or `disabled`.
226+
Some permissions, for example, location or acceleration sensor permissions, have an associated service, which can be `enabled` or `disabled`.
201227

202228
```dart
203229
if (await Permission.locationWhenInUse.serviceStatus.isEnabled) {
@@ -211,27 +237,27 @@ You can also open the app settings:
211237
if (await Permission.speech.isPermanentlyDenied) {
212238
// The user opted to never again see the permission request dialog for this
213239
// app. The only way to change the permission's status now is to let the
214-
// user manually enable it in the system settings.
240+
// user manually enables it in the system settings.
215241
openAppSettings();
216242
}
217243
```
218244

219-
On Android, you can show a rationale for using a permission:
245+
On Android, you can show a rationale for using permission:
220246

221247
```dart
222248
bool isShown = await Permission.contacts.shouldShowRequestRationale;
223249
```
224250

225251
Some permissions will not show a dialog asking the user to allow or deny the requested permission.
226252
This is because the OS setting(s) of the app are being retrieved for the corresponding permission.
227-
The status of the setting will determine whether the permission is `granted` or `denied`.
253+
The status of the setting will determine whether the permission is `granted` or `denied`.
228254

229-
The following permissions will show no dialog:
255+
The following permissions will show no dialog:
230256

231257
- Notification
232258
- Bluetooth
233259

234-
The following permissions will show no dialog, but will open the corresponding setting intent for the user to change the permission status:
260+
The following permissions will show no dialog, but will open the corresponding setting intent for the user to change the permission status:
235261

236262
- manageExternalStorage
237263
- systemAlertWindow
@@ -246,23 +272,23 @@ This will then bring up another permission popup asking you to `Keep Only While
246272

247273
### Requesting "storage" permissions always returns "denied" on Android 13+. What can I do?
248274

249-
On Android the `Permission.storage` permission is linked to the Android `READ_EXTERNAL_STORAGE` and `WRITE_EXTERNAL_STORAGE` permissions. Starting from Android 10 (API 29) the `READ_EXTERNAL_STORAGE` and `WRITE_EXTERNAL_STORAGE` permissions have been marked deprecated and have been fully removed/disabled since Android 13 (API 33).
275+
On Android, the `Permission.storage` permission is linked to the Android `READ_EXTERNAL_STORAGE` and `WRITE_EXTERNAL_STORAGE` permissions. Starting from Android 10 (API 29) the `READ_EXTERNAL_STORAGE` and `WRITE_EXTERNAL_STORAGE` permissions have been marked deprecated and have been fully removed/disabled since Android 13 (API 33).
250276

251-
If your application needs access to media files Google recommends using the `READ_MEDIA_IMAGES`, `READ_MEDIA_VIDEOS` or `READ_MEDIA_AUDIO` permissions instead. These can be requested using the `Permission.photos`, `Permission.videos` and `Permission.audio` respectively. To request these permissions make sure the `compileSdkVersion` in the `android/app/build.gradle` file is set to `33`.
277+
If your application needs access to media files Google recommends using the `READ_MEDIA_IMAGES`, `READ_MEDIA_VIDEOS`, or `READ_MEDIA_AUDIO` permissions instead. These can be requested using the `Permission.photos`, `Permission.videos`, and `Permission.audio` respectively. To request these permissions make sure the `compileSdkVersion` in the `android/app/build.gradle` file is set to `33`.
252278

253279
If your application needs access to Android's file system, it is possible to request the `MANAGE_EXTERNAL_STORAGE` permission (using `Permission.manageExternalStorage`). As of Android 11 (API 30), the `MANAGE_EXTERNAL_STORAGE` permission is considered a high-risk or sensitive permission. Therefore it is required to [declare the use of these permissions](https://support.google.com/googleplay/android-developer/answer/9214102) if you intend to release the application via the Google Play Store.
254280

255281
### Requesting `Permission.locationAlways` always returns "denied" on Android 10+ (API 29+). What can I do?
256282

257-
Starting with Android 10, apps are required to first obtain the permission to read the device's location in the foreground, before requesting to read the location in the background as well. When requesting for the 'location always' permission directly, or when requesting both permissions at the same time, the system will ignore the request. So, instead of calling only `Permission.location.request()`, make sure to first call either `Permission.location.request()` or `Permission.locationWhenInUse.request()`, and obtain permission to read the GPS. Once you obtain this permission, you can call `Permission.locationAlways.request()`. This will present the user with the option to update the settings so the location can always be read in the background. For more information, visit the [Android documentation on requesting location permissions](https://developer.android.com/training/location/permissions#request-only-foreground).
283+
Starting with Android 10, apps are required to first obtain permission to read the device's location in the foreground, before requesting to read the location in the background as well. When requesting the 'location always' permission directly, or when requesting both permissions at the same time, the system will ignore the request. So, instead of calling only `Permission.location.request()`, make sure to first call either `Permission.location.request()` or `Permission.locationWhenInUse.request()`, and obtain permission to read the GPS. Once you obtain this permission, you can call `Permission.locationAlways.request()`. This will present the user with the option to update the settings so the location can always be read in the background. For more information, visit the [Android documentation on requesting location permissions](https://developer.android.com/training/location/permissions#request-only-foreground).
258284

259285
## Issues
260286

261-
Please file any issues, bugs or feature request as an issue on our [GitHub](https://github.com/Baseflow/flutter-permission-handler/issues) page. Commercial support is available if you need help with integration with your app or services. You can contact us at [hello@baseflow.com](mailto:hello@baseflow.com).
287+
Please file any issues, bugs, or feature requests as an issue on our [GitHub](https://github.com/Baseflow/flutter-permission-handler/issues) page. Commercial support is available if you need help with integration with your app or services. You can contact us at [hello@baseflow.com](mailto:hello@baseflow.com).
262288

263289
## Want to contribute
264290

265-
If you would like to contribute to the plugin (e.g. by improving the documentation, solving a bug or adding a cool new feature), please carefully review our [contribution guide](../CONTRIBUTING.md) and send us your [pull request](https://github.com/Baseflow/flutter-permission-handler/pulls).
291+
If you would like to contribute to the plugin (e.g. by improving the documentation, solving a bug, or adding a cool new feature), please carefully review our [contribution guide](../CONTRIBUTING.md) and send us your [pull request](https://github.com/Baseflow/flutter-permission-handler/pulls).
266292

267293
## Author
268294

0 commit comments

Comments
 (0)