0

I have created a simple app which has a login page and when the user clicks on login button It navigates to another page. It works fine whenever I run my app using react-native run-android. But app crashes whenever I run the .apk file in a higher version of Android devices(it works fine in android 5 and 6).

package.json

{
    "name": "awsUpload",
    "version": "0.0.1",
    "private": true,
    "scripts": {
        "start": "node node_modules/react-native/local-cli/cli.js start",
        "test": "jest"
    },
    "dependencies": {
        "aws-sdk": "^2.395.0",
        "link": "^0.1.5",
        "react": "16.6.3",
        "react-native": "^0.58.3",
        "react-native-aws3": "0.0.8",
        "react-native-device-info": "^0.26.2",
        "react-native-file-upload": "^1.0.4",
        "react-native-image-picker": "^0.28.0",
        "react-native-material-dropdown": "^0.11.1",
        "react-native-router-flux": "^4.0.6",
        "uniqid": "^5.0.3"
    },
    "devDependencies": {
        "babel-core": "7.0.0-bridge.0",
        "babel-jest": "24.0.0",
        "jest": "24.0.0",
        "metro-react-native-babel-preset": "0.51.1",
        "react-test-renderer": "16.6.3"
    },
    "jest": {
        "preset": "react-native"
    }
}

I am using below code to get the geolocation. As I know, there is already an issue to get the geolocation in higher version of the Android device due to the protection and there is a solution in stack overflow. But I did not get it how to use it.

My code is ,

import React, {Component} from 'react';
import {Text, View} from 'react-native';

export default class App extends Component<Props> {

       constructor(props) {
        super(props);
        this.state = {

          currentLongitude: 'unknown',//Initial Longitude
          currentLatitude: 'unknown',//Initial Latitude

        }
      }
    componentDidMount = () => {
        navigator.geolocation.getCurrentPosition(
          //Will give you the current location
           (position) => {
              const currentLongitude = JSON.stringify(position.coords.longitude);
              //getting the Longitude from the location json
              const currentLatitude = JSON.stringify(position.coords.latitude);
              //getting the Latitude from the location json
              this.setState({ currentLongitude:currentLongitude });
              //Setting state Longitude to re re-render the Longitude Text
              this.setState({ currentLatitude:currentLatitude });
              //Setting state Latitude to re re-render the Longitude Text
           },
           (error) => alert(error.message),
           { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 }
        );
        this.watchID = navigator.geolocation.watchPosition((position) => {
          //Will give you the location on location change
            console.log(position);
            const currentLongitude = JSON.stringify(position.coords.longitude);
            //getting the Longitude from the location json
            const currentLatitude = JSON.stringify(position.coords.latitude);
            //getting the Latitude from the location json
           this.setState({ currentLongitude:currentLongitude });
           //Setting state Longitude to re re-render the Longitude Text
           this.setState({ currentLatitude:currentLatitude });
           //Setting state Latitude to re re-render the Longitude Text
        });
    }
   componentWillUnmount = () => {
      navigator.geolocation.clearWatch(this.watchID);

   }
    render() {
      return (
                <View>
                   <Text>Longitude: {this.state.currentLongitude}</Text>
                   <Text>Latitude: {this.state.currentLatitude}</Text>

                   {this.state.error ? <Text>Error: {this.state.error}</Text> : null}
                </View> 
          );
        }
      }

Solution link is,

https://stackoverflow.com/questions/33865445/gps-location-provider-requires-access-fine-location-permission-for-android-6-0/33866959 
Vidya Kabber
  • 171
  • 1
  • 16

1 Answers1

1

the error is completely clear, you need to get Permission for accessing location.

there are two types of permissions:

1- ones that can be granted just by adding permission to manifest.xml

2- ones that user, himself must accept that permission.(these usually shows within a dialog and asks user to give the app access to ... ).

Location permission is second type. so adding that permission to Manifest.xml won't do anything.

you can check the lists of permissions in https://facebook.github.io/react-native/docs/permissionsandroid

the way to ask user permission for that is explained in links below:

How do I request permission for Android Device Location in React Native at run-time?

Amir Hedieh
  • 1,120
  • 1
  • 14
  • 28