पाठ 16 / 25
Permissions and Device APIs
Ask at the right moment.
Runtime permissions with Expo modules
Sensitive features such as the camera, location, microphone, photos and contacts need runtime permission from the user. Expo modules expose them with a common pattern: a request...PermissionsAsync function or a use...Permissions hook returning granted, canAskAgain and similar fields. iOS also requires usage description strings (for example, why you need the camera) in the app config, which Expo config plugins write into Info.plist; Android permissions are declared in the manifest. Ask in context (when the user taps "Scan code", not on first launch), explain why first, and handle denial gracefully, including linking to settings when the user cannot be asked again. Common modules include expo-camera, expo-location, expo-image-picker and expo-notifications.
Camera, location, notifications and native code
Expo modules give typed JavaScript APIs for device features, guarded by runtime permissions.
Getting the current location
expo-location; check the docs for your SDK version.
import * as Location from "expo-location";
import { Alert, Linking } from "react-native";
export async function getCurrentPosition() {
const { status, canAskAgain } = await Location.requestForegroundPermissionsAsync();
if (status !== "granted") {
if (!canAskAgain) {
Alert.alert("Location is off", "Enable location in Settings to find stores near you.", [
{ text: "Cancel", style: "cancel" },
{ text: "Open Settings", onPress: () => Linking.openSettings() },
]);
}
return null;
}
const position = await Location.getCurrentPositionAsync({});
return { lat: position.coords.latitude, lng: position.coords.longitude };
}
// app.json (config plugin sets the iOS usage string):
// "plugins": [["expo-location", {
// "locationWhenInUsePermission": "Show stores near you." }]]Request the least access you need
Prefer foreground location over background, and approximate over precise when that is enough. Store reviewers and users both notice excessive permissions.
त्वरित जाँच: When is the best time to request camera permission?
- Never; the camera works without permission
- Immediately on first launch for every permission
- In context, when the user starts a feature that needs the camera
- Only after the app is published
Answer
In context, when the user starts a feature that needs the camera — Context-aware requests are granted more often.