Skip to content

Commit

Permalink
fix: issue with camera permissions when clicking the camera picker
Browse files Browse the repository at this point in the history
  • Loading branch information
khushal87 committed Nov 16, 2023
1 parent be241e1 commit 0e4bc60
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions package/native-package/src/handlers/takePhoto.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { AppState, Image, Linking, PermissionsAndroid, Platform } from 'react-native';
import ImagePicker from 'react-native-image-crop-picker';

let hadDeniedAndroidPermission = false;

export const takePhoto = async ({ compressImageQuality = Platform.OS === 'ios' ? 0.8 : 1 }) => {
if (Platform.OS === 'android') {
const cameraPermissions = await PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.CAMERA);
Expand All @@ -11,12 +9,9 @@ export const takePhoto = async ({ compressImageQuality = Platform.OS === 'ios' ?
PermissionsAndroid.PERMISSIONS.CAMERA,
);
if (androidPermissionStatus === PermissionsAndroid.RESULTS.DENIED) {
hadDeniedAndroidPermission = true;
return { cancelled: true };
} else if (androidPermissionStatus === PermissionsAndroid.RESULTS.NEVER_ASK_AGAIN) {
if (!hadDeniedAndroidPermission) {
Linking.openSettings();
}
Linking.openSettings();
return { cancelled: true };
}
}
Expand Down Expand Up @@ -62,9 +57,15 @@ export const takePhoto = async ({ compressImageQuality = Platform.OS === 'ios' ?
};
}
} catch (e: unknown) {
// on iOS: if it was in inactive state, then the user had just denied the permissions
if (Platform.OS === 'ios' && AppState.currentState === 'active') {
await Linking.openSettings();
if (e instanceof Error) {
// on iOS: if it was in inactive state, then the user had just denied the permissions
if (Platform.OS === 'ios' && AppState.currentState === 'active') {
const cameraPermissionDeniedMsg = 'User did not grant camera permission.';
// Open settings when the user did not allow camera permissions
if (e.message === cameraPermissionDeniedMsg) {
await Linking.openSettings();
}
}
}
}

Expand Down

0 comments on commit 0e4bc60

Please sign in to comment.