When a user downloads and opens our app, they are presented with a tutorial on how to use the app. If they hit the skip button, or continue once they’ve gone through the tutorial, they are supposed to be presented with the locations permission modal. However, this doesn’t work for all devices, and the following error is logged: “This method can cause UI unresponsiveness if invoked on the main thread. Instead, consider waiting for the -locationManagerDidChangeAuthorization:
callback and checking authorizationStatus
first.”
This is the code for the buttons:
<DTButton
buttonStyle={styles.continueButton}
onPress={async () => {
const granted = await requestLocationAccess();
if (!granted) {
showLocationAlert();
}
navigateAway();
}}>
Continue
</DTButton>
And this is the code for the requestLocationAccess() :
export const requestLocationAccess = async () => {
let granted;
try {
if (Platform.OS === 'ios') {
granted = await Geolocation.requestAuthorization('whenInUse');
} else {
granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
);
}
if (
granted &&
granted !== 'disabled' &&
granted !== 'restricted' &&
granted !== 'denied'
) {
return true;
} else {
Alert.alert('Error', 'Location services are required to track.');
return false;
}
} catch (error) {
Alert.alert(
'Error',
'Something went wrong, unable to request locations permissions',
);
return false;
}
I have seen in other forums something about using a dispatch queue, but I am not sure where that would fit into the current code. I have also seen suggestions to wrap the CLLocationManager call in an async function, but I am not seeing CLLocationManager being directly called anywhere in our code. Please let me know if you have an idea for how these solutions, or a new solution, could work. Thank you!
Scott Guenther is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.