Explain it simply
Working with Device Features (Camera, Location), in plain language
Before accessing a device feature (camera, location, contacts), you need to request permission from the user (an iOS/Android OS-level requirement) — packages like expo-camera and expo-location provide a requestPermissionsAsync() function, and you have to handle the user's 'Allow'/'Deny' choice. If permission is denied, gracefully disable/explain the feature — the app must not crash.
Problem → Why → Solution
The problem — why this concept matters
Calling getCurrentPositionAsync() right away without checking permission status first, and crashing when it's denied
A common mistake
Requesting camera/location permission the moment the app opens (with no clear context) — this makes users suspicious
Core concept — understand how it works
Build Working with Device Features (Camera, Location) as a lifecycle-aware flow covering permission, loading, empty, offline, timeout, cancellation, retry, foreground/background transitions, and stale data. Clean up subscriptions, timers, and requests.
- request or permission
- native module boundary
- result, error, or cleanup
Explain the React and native-platform behavior of Working with Device Features (Camera, Location)
Core syntax or pattern
import * as Location from 'expo-location';
import { useState, useEffect } from 'react';
import { Text } from 'react-native';
function LocationScreen() {
const [location, setLocation] = useState(null);
const [errorMsg, setErrorMsg] = useState(null);
useEffect(() => {
(async () => {
const { status } = await Location.requestForegroundPermissionsAsync();
if (status !== 'granted') {
setErrorMsg('Location permission was denied');
return;
}
const loc = await Location.getCurrentPositionAsync({});
setLocation(loc);
})();
}, []);
if (errorMsg) return <Text>{errorMsg}</Text>;
return <Text>{location ? JSON.stringify(location.coords) : 'Loading...'}</Text>;
}Build Working with Device Features (Camera, Location) as a lifecycle-aware flow covering permission, loading, empty, offline, timeout, cancellation, retry, foreground/background transitions, and stale data. Clean up subscriptions, timers, and requests.
Read the code step by step
- request or permission
import * as Location from 'expo-location';Before accessing a device feature (camera, location, contacts), you need to request permission from the user (an iOS/Android OS-level requirement) — packages like expo-camera and expo-location provide a requestPermissionsAsync() function, and you have to handle the user's 'Allow'/'Deny' choice. If permission is denied, gracefully disable/explain the feature — the app must not crash.
- native module boundary
setErrorMsg('Location permission was denied');Build Working with Device Features (Camera, Location) as a lifecycle-aware flow covering permission, loading, empty, offline, timeout, cancellation, retry, foreground/background transitions, and stale data. Clean up subscriptions, timers, and requests.
- result, error, or cleanup
}Test an original Working with Device Features (Camera, Location) example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.
- request or permission
- native module boundary
- result, error, or cleanup
Success
Explain the React and native-platform behavior of Working with Device Features (Camera, Location)
Failure
Calling getCurrentPositionAsync() right away without checking permission status first, and crashing when it's denied
Let's think about it this way for a second
Before accessing a device feature (camera, location, contacts), you need to request permission from the user (an iOS/Android OS-level requirement) — packages like expo-camera and expo-location provide a requestPermissionsAsync() function, and you have to handle the user's 'Allow'/'Deny' choice. If permission is denied, gracefully disable/explain the feature — the app must not crash.
Let's connect this to a real-world scenario
Install expo-location and call Location.requestForegroundPermissionsAsync(), and the OS's native permission dialog will appear — once you get 'Allow', you can fetch GPS coordinates with Location.getCurrentPositionAsync(). Always check the permission status (granted/denied), and if status !== 'granted', show a UI message explaining that the feature is limited.
Code Example
import * as Location from 'expo-location';
import { useState, useEffect } from 'react';
import { Text } from 'react-native';
function LocationScreen() {
const [location, setLocation] = useState(null);
const [errorMsg, setErrorMsg] = useState(null);
useEffect(() => {
(async () => {
const { status } = await Location.requestForegroundPermissionsAsync();
if (status !== 'granted') {
setErrorMsg('Location permission was denied');
return;
}
const loc = await Location.getCurrentPositionAsync({});
setLocation(loc);
})();
}, []);
if (errorMsg) return <Text>{errorMsg}</Text>;
return <Text>{location ? JSON.stringify(location.coords) : 'Loading...'}</Text>;
}A permission dialog appears, and tapping 'Allow' displays the device's GPS coordinates on screen.Try it in 5 minutes
Install expo-location and implement the permission request + current location fetch flow yourself (on the Expo Go app).
A quick word of caution
Location/camera data is sensitive personal information — clearly explain to the user why it's needed before requesting permission, and if you send it to a backend, follow the encryption/HTTPS concepts from the Cybersecurity tutorial.
Real-world usage
How to apply it in a real project
Test an original Working with Device Features (Camera, Location) example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.
Mini challenge
Build it yourself
Test an original Working with Device Features (Camera, Location) example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.
- Rebuild the example without copying it
- Test a normal input and an edge case
- Predict the compiler or output result before running it
import * as Location from 'expo-location';
import { useState, useEffect } from 'react';
import { Text } from 'react-native';
function LocationScreen() {
const [location, setLocation] = useState(null);
const [errorMsg, setErrorMsg] = useState(null);
useEffect(() => {
(async () => {
const { status } = await Location.requestForegroundPermissionsAsync();
if (status !== 'granted') {
setErrorMsg('Location permission was denied');
return;
}
const loc = await Location.getCurrentPositionAsync({});
setLocation(loc);
})();
}, []);
if (errorMsg) return <Text>{errorMsg}</Text>;
return <Text>{location ? JSON.stringify(location.coords) : 'Loading...'}</Text>;
}Expected: Explain the React and native-platform behavior of Working with Device Features (Camera, Location)
Show hint
Test an original Working with Device Features (Camera, Location) example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.
Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- request or permission → native module boundary → result, error, or cleanup
- Build Working with Device Features (Camera, Location) as a lifecycle-aware flow covering permission, loading, empty, offline, timeout, cancellation, retry, foreground/background transitions, and stale data. Clean up subscriptions, timers, and requests.
- Calling getCurrentPositionAsync() right away without checking permission status first, and crashing when it's denied
- Test an original Working with Device Features (Camera, Location) example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.
React Native Documentation — React Native