Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Location permission on iOS in react-native not working

No Location Permission pop up in ios app nor can see Permissions options in the settings of the App

Works fine on Android. As I can use PermissionsAndroid to get the permissions.

Already used the following options in the info.plist by looking at the other answers. Few answers only mentioned about android.

info.plist

<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
    <string>Location Permission</string>
    <key>NSLocationAlwaysUsageDescription</key>
    <string>Location Permission</string>
    <key>NSLocationUsageDescription</key>
    <string>GPS data is required to...</string>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>Location Permission</string>

codeinthefile.js

try {
    const granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
        {
            title: "Geolocation Permission",
            message: "App needs access to your phone's location.",
        }
    );
    if (granted === PermissionsAndroid.RESULTS.GRANTED) {
        Geolocation.getCurrentPosition(
            position => {
                Geocoder.from({
                    latitude: position.coords.latitude,
                    longitude: position.coords.longitude,
                })
                    .then(json => {
                        console.log(json);
                    })
                    .catch(error => {
                        console.log(error);
                    });
            },
            error => {
                console.log(error);
            },
            { enableHighAccuracy: true, timeout: 15000, maximumAge: 10000 }
        );
    } else {
        console.log('Location permission not granted!);
    }
} catch (err) {
    console.log('Location permission not granted!)
}

If by using the above-mentioned values in info.plist I should get the access to location then there should not be an error of no permission granted.

like image 228
Kai Avatar asked Sep 01 '25 16:09

Kai


2 Answers

Don't use PermissionAndroid in iOS, is enough to put the permission requirement in the info.plist,

try something like,

if(Platform.OS === "ios"){
   // your code using Geolocation and asking for authorisation with

   geolocation.requestAuthorization()
}else{
   // ask for PermissionAndroid as written in your code
}
like image 151
David Vittori Avatar answered Sep 06 '25 05:09

David Vittori


Thank you, Doug and David, Based on your suggestion I have made then changes to my code in the following way which worked for me:

if(Platform.OS === 'ios'){
    Geolocation.requestAuthorization();
    this.getGeoLocation();
}else {

    let granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
        {
            title: "App Geolocation Permission",
            message: "App needs access to your phone's location.",
        }
    );

    if (androidGranted === PermissionsAndroid.RESULTS.GRANTED) {

        this.getGeoLocation();

    } else {

        console.log('Location permission not granted!!!!');

    }

}
like image 23
Kai Avatar answered Sep 06 '25 05:09

Kai