Nativescript: Enable Android 6 way for requesting permissions

Created on 28 Jan 2016  路  15Comments  路  Source: NativeScript/NativeScript

Quoting from the official Android guides:

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app.

Once we integrate the new functionality from the Android Runtime (https://github.com/NativeScript/android-runtime/issues/283) we need to base our Activity on AppCompatActivity and provide an implementation of the OnRequestPermissionsResultCallback interface. Then we need to design some JavaScript-friendly API to allow people to enable permissions more elegantly than calling native APIs.

done feature high

Most helpful comment

I've released the permissions plugin; however again this requires support that is in the masters; so it currently does NOT work on v1.7 unless you manually make the changes that I put into my two pull requests (Linked to earlier in this thread). https://github.com/NathanaelA/nativescript-permissions

All 15 comments

+1

+1

Essentially NativeScript doesn't fully support Android 6.0 then?

+1

@gopi-ar - just wanted to point out that it does support 6.0, this issue is to create a nice javascript API for devs to use and not deal with the native code dealing with permissions, it can be a bit tricky without any Android knowledge.

Just wanted to add that @bradmartin is right and if you wonder what you can do now, then here is an example. If you request permission before using the thing that requires the permission then all is fine.

Only in case you want to defer requesting permission until the thing that requires the permission is called and have it invoked immediately after the user has granted the permission - then you need the OnRequestPermissionsResultCallback API @atanasovg refers to.
Which I eagerly await by the way since I'm using this in 3 plugins now and want the developer to choose whether or not he wants to defer requesting permission or ask it upfront. Either way you want to check the user granted permission every time before triggering the code that may need the permission.

@eddyverbruggen does that really work? I tried hacking that together in a hurry the other night for RECORD_AUDIO and I failed miserably but I was hurried to just check if it was possible :)

@bradmartin Yes I have that in 4 NS plugins and use the same approach for about 10 Cordova plugins as well as they started to support OnRequestPermissionsResultCallback only in a recent version of the fwk and I didn't want to force users to not use SDK 23.

Note that you need to include the support v4 lib.

+1

I have just added a pull request that will add the basic functionality to core to be able to handle Android 6+ permissions. ;-) Hopefully they will accept it.

Any update about this? =/

The PR is merged.
You can go here and download a build from master if you are not willing to wait for the release.

Please note I have a plugin that I will be releasing shortly that makes permissions a breeze for all of us plugin developers (and other uses are perfectly valid also :grinning: )

permissions.getPermissions(camera)
   .then( function() {  /* has support */  })
   .catch(function() { /* user denied giving you permissions */ });

The permission promise persists until the promise can be resolved via the requestPermissions system on Android 6 and automatically resolves on Android 5 and lower since we don't have to request permissions on those OS's.

I've released the permissions plugin; however again this requires support that is in the masters; so it currently does NOT work on v1.7 unless you manually make the changes that I put into my two pull requests (Linked to earlier in this thread). https://github.com/NathanaelA/nativescript-permissions

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings