diff --git a/README.md b/README.md index bb3c9670b..411d68b35 100644 --- a/README.md +++ b/README.md @@ -57,14 +57,21 @@ import CameraRoll from "@react-native-community/cameraroll"; ### Permissions +**iOS** + The user's permission is required in order to access the Camera Roll on devices running iOS 10 or later. Add the `NSPhotoLibraryUsageDescription` key in your `Info.plist` with a string that describes how your app will use this data. This key will appear as `Privacy - Photo Library Usage Description` in Xcode. If you are targeting devices running iOS 11 or later, you will also need to add the `NSPhotoLibraryAddUsageDescription` key in your `Info.plist`. Use this key to define a string that describes how your app will use this data. By adding this key to your `Info.plist`, you will be able to request write-only access permission from the user. If you try to save to the camera roll without this permission, your app will exit. -On Android permission is required to read the external storage. Add below line to your manifest to request this permission on app install. -On Android permission is required to write in the external storage when saving to camera roll. Add below line to your manifest to request this permission on app install. +**Android** -``` +Permission is required to read and write to the external storage. + +On Expo, follow the guide [here](https://docs.expo.io/versions/latest/sdk/permissions/) for requesting the permission. + +On react-native-cli or ejected apps, adding the following lines will add the capability for the app to request the permission. Find more info on Android Permissions [here](https://reactnative.dev/docs/permissionsandroid). + +```xml ... @@ -73,6 +80,33 @@ On Android permission is required to write in the external storage when saving t ``` +Then you have to explicitly ask for the permission + +```javascript +import { PermissionsAndroid, Platform } from "react-native"; +import CameraRoll from "@react-native-community/cameraroll"; + +async function hasAndroidPermission() { + const permission = PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE; + + const hasPermission = await PermissionsAndroid.check(permission); + if (hasPermission) { + return true; + } + + const status = await PermissionsAndroid.request(permission); + return status === 'granted'; +} + +async function savePicture() { + if (Platform.OS === "android" && !(await hasAndroidPermission())) { + return; + } + + CameraRoll.saveToCameraRoll(tag, [type]); +}; +``` + ### Methods * [`save`](#save)