Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native - can't request location permissions using PermissionsAndroid

I am trying to request runtime location permissions but my promise keeps throwing this exception:

"Tried to use permissions API but the host Activity doesn't implement PermissionAwareActivity"

My code look like this:

  PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION)
  .then((preGranted) => {
    console.log('pre-granted', preGranted)
    if (!preGranted) {
      PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
        { 'title': 'Enable location', 'message': 'Boss said so..' }
      )
      .then((granted) => {
        if (granted) {
          console.log('GRANTED AFTER ASKING:', granted)
          debugger
        }
      })
    }
  })

In my manifest i have:

  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Has anyone come across that message? What is PermissionAwareActivity im having trouble finding anything about it online.

like image 310
fozzarelo Avatar asked Mar 16 '17 18:03

fozzarelo


People also ask

How do I enable location permissions in react-native?

It is important that GPS is enabled as the permissions library would not enable it even if the location permissions are enabled. To enable GPS, react-native-android-location-enabler can be used.

How do I request permissions in react-native?

So to ask permissions, React Native has a prebuilt feature in it which we can import and use it in our code. import { PermissionsAndroid } from 'react-native'; Before asking permissions to the user we have to declare that permissions in AndroidManifest. xml file.

How do I request location permissions in IOS react-native?

Geolocation is enabled by default when you create a project with react-native init . In order to enable geolocation in the background, you need to include the 'NSLocationAlwaysUsageDescription' key in Info. plist and add location as a background mode in the 'Capabilities' tab in Xcode.

How do you check if permission is granted react-native?

On devices before SDK version 23, the permissions are automatically granted if they appear in the manifest, so check should always result to true and request should always resolve to PermissionsAndroid. RESULTS. GRANTED .


2 Answers

you should implements PermissionAwareActivity in your activity, and define the Callback && PermissionListener .

public class ReactNativeIndexActivity extends AppCompatActivity implements DefaultHardwareBackBtnHandler,PermissionAwareActivity {
    private ReactRootView mReactRootView;
    private ReactInstanceManager mReactInstanceManager;
    private @Nullable   Callback mPermissionsCallback;
    private @Nullable PermissionListener mPermissionListener;
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ......
}

  @Override
    protected void onResume() {
        super.onResume();

        if (mReactInstanceManager != null) {
            mReactInstanceManager.onHostResume(this, this);
        }
        if (mPermissionsCallback != null) {
            mPermissionsCallback.invoke();
            mPermissionsCallback = null;
        }
    }

 @Override
    @TargetApi(Build.VERSION_CODES.M)
    public void requestPermissions(String[] permissions, int requestCode, PermissionListener listener) {
        mPermissionListener = listener;
        requestPermissions(permissions, requestCode);
    }
    @Override
    public void onRequestPermissionsResult(final int requestCode, @NonNull final String[] permissions, @NonNull final int[] grantResults) {
        mPermissionsCallback = new Callback() {
            @Override
            public void invoke(Object... args) {
                if (mPermissionListener != null && mPermissionListener.onRequestPermissionsResult(requestCode, permissions, grantResults)) {
                    mPermissionListener = null;
                }
            }
        };
    }
like image 54
yangcong Avatar answered Oct 09 '22 00:10

yangcong


PermissionAwareActivity is an interface in a third party library.

https://github.com/wix/react-native-navigation

It is not part of the standard Android SDK.

Instructions for requesting runtime permissions using the standard SDK can be found in the documentation.

https://developer.android.com/training/permissions/requesting.html

like image 29
Kuffs Avatar answered Oct 09 '22 02:10

Kuffs