How to ask multiple permissions in a react-native application? - android

Right now I am building an applications which uses multiple device features like location, camera, storage and contacts.
I want to know if there is any way that allows users to enter into my application if and only if all the permissions are granted.
I've tried using react-native-permissions and the code snippet is as follows
Permissions.request('photo').then(() => {
Permissions.request('location').then(() => {
Permissions.request('camera').then(() => {
Permissions.request('contacts').then(() => {
Permissions.request('notification').then(() => {
console.log('PERMISSIONS ASKED');
});
});
});
});
});
The above code works only at the first time and if the user clicks deny then it is not asking again. Can any one help me with this.
Thanks in advance

Yes, once a user denies a permission he won't be asked for the permission again(iOS). He have to go back to settings and manually update the permissions.
It is not a good practice to ask all the permissions at once, the right way to ask is to alert for the permission when user initiate the usage of services like (location, camera).
Any way if you would like to ask all the permissions at once then you can check if the permission is in denied state, if so you can show an alert to open settings and change the permission.
Permissions.check('location', { type: 'always' }).then(response => {
this.setState({ locationPermission: response })
})

Related

Expo react native doesn't request location permission

I'm building an app that needs to track user's locations for app functionality. I'm requesting user's location using expo Location
useEffect(() =>
{
(async ()=>{
var {granted}=await Location.
requestForegroundPermissionsAsync()
// if granted do something
})()
}, [])
The app asks for user's permission the first time, but if user selects "allow only this time" option,closes the app and reopens it, app doesn't ask the location permission again. How can I request permission again on app start if user previously user selected "only this time".
check the location permission first using this code in useEffect
PermissionsAndroid.check(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION
);

React Native Permission rationale not showing

I'm having a trouble with react native permission rationale. It is not showing though I already put it in my code.
const rationale = { title: 'Permission', message: 'message' }
const res = await PermissionsAndroid.request(PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION, rationale);
What is the problem why it's not showing? Thank you for your help.
Late reply, but perhaps this will help someone else who lands here in the future:
I struggled with this as well recently, until I dug into the documentation on PermissionsAndroid:
If rationale is provided, this function checks with the OS whether it is necessary to show a dialog explaining why the permission is needed (https://developer.android.com/training/permissions/requesting.html#explain) and then shows the system permission dialog.
While not immediately clear from that line alone, following the provided link to Google's Documentation yielded this valuable snippet:
If the ContextCompat.checkSelfPermission() method returns PERMISSION_DENIED, call shouldShowRequestPermissionRationale(). If this method returns true, show an educational UI to the user. In this UI, describe why the feature, which the user wants to enable, needs a particular permission.
Essentially, you won't see the rationale on the initial request; it will only be presented after the user has expressly denied the permission (either via the system dialog or through settings). Hope it helps!

How to ask for LOCATION PERMISSION in React Native?

I am developing a simple app that show your current position ( latitude and longitude). I am using the functions:
- navigator.geolocation.getCurrentPosition
- navigator.geolocation.watchPosition
But I have noticed that the permission is granted without asking for permission at the user.
Is this agaist the google's rules?
If yes, how can I show a dialog that ask for the permission?
For android, You can use PermissionAndroid by importing from 'react-native'.
import { PermissionsAndroid } from 'react-native'; //
Checkout this link for more info.
For iOS, You have to fill the .plist file and it pops for permission. No need to handle it explicitly.
As per the android docs
Android apps must request permission to access sensitive user data (such as contacts and SMS), as well as certain system features (such as camera and internet)
If the device is running Android 6.0 (API level 23) or higher, and the app's targetSdkVersion is 23 or higher, the user isn't notified of any app permissions at install time. Your app must ask the user to grant the dangerous permissions at runtime.
If the device is running Android 5.1.1 (API level 22) or lower, or the app's targetSdkVersion is 22 or lower while running on any version of Android, the system automatically asks the user to grant all dangerous permissions for your app at install-time.
To grant the Permissions manually you must use PermissionsAndroid before each request which is well mentioned in this link
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.CAMERA,
{
'title': 'Cool Photo App Camera Permission',
'message': 'Cool Photo App needs access to your camera ' +
'so you can take awesome pictures.'
}
)
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log("You can use the camera")
} else {
console.log("Camera permission denied")
}
} catch (err) {
console.warn(err)
}
The list of dangerous permissions are mentioned here
I think you should be looking at doing this with the react-native-permissions
Permissions.request('location', { type: 'always' }).then(response => {
this.setState({ locationPermission: response })
})

Handle "dont ask again " Location Permissions Xamarin Forms

I got this dilemma. I am using TK-TORBEN-Maps ,Xamarin Forms. I did implement all the runtime permissions . however, if an user clicks "dont ask me again" what can I do to stop the app from crashing.
I need to let them pass even if they didnt accept and the first page is the map. is there anyway I could still display the map with those permission being denied?
It seems that you could not use the map without the permission.
I think you could explain to your users why the app needs the permission. For example, you could use ShouldShowRequestPermissionRationale.
string permission = Manifest.Permission.AccessFineLocation;
if (ShouldShowRequestPermissionRationale(permission))
{
//Explain to the user why we need to read the contacts
Snackbar.Make(layout, "Location access is required for some reason ", Snackbar.LengthIndefinite)
.SetAction("OK", v => RequestPermissions(PermissionsLocation, RequestLocationId))
.Show();
return;
}
You could refer to this blog for more information.
If you want the app not crash, you could try to navigate to another page after user denies the request.

Android not requesting permission in ionic app

I am building app that allows people to post pictures and videos, mainly trying to learn about ionic, cordova, android, and the like, but for some reason whenever I try to open a file using the cordova File plugin, it doesn't ask the user permission to access storage, and the code fails and the user is stuck on a loading screen.
The code is pretty simple, they take a video, it gets transcoded, then the transcoded file is uploaded to a firebase storage bucket. If I quit the app, go to settings->apps->my app->permissions and then manually turn on the storage permission, it works. The problem is, I need to ask the user for permission either at run time or on install, and it doesn't. Here is the code..
this.media.captureVideo().then(file => {
this.editor.transcodeVideo({
fileUri: file[0].fullPath,
outputFileType: this.editor.OutputFileType.MPEG4,
outputFileName: 'temp_test',
saveToLibrary: false,
height: 800,
width: 800,
maintainAspectRatio: false
}).then(fileUri => {
let pathIndex = fileUri.lastIndexOf('/'),
finalPath = 'file://' + fileUri.substr(0, pathIndex),
name = fileUri.substr(pathIndex + 1, fileUri.length);
this.file.readAsArrayBuffer(finalPath, name).then(file => {
//upload
}).catch(err => { console.log(err); });
});
});
However, this only works if I open up the settings on my phone, go to apps, then my app, then permissions, then enable storage manually. Is there some way to request the permission in ionic? I have searched and searched and all the answers I can find are specific to camera, or work in Java, but ionic isn't java. Thank you.
Effectively cordova-plugin-media-capture uses cordova-plugin-file, so the READ_EXTERNAL_STORAGE permission must be programatically asked.
Ionic offers native support for Android Permissions:
this.androidPermissions.hasPermission(this.androidPermissions.PERMISSION.READ_EXTERNAL_STORAGE)
.then(status => {
if (status.hasPermission) {
this.captureVideo();
} else {
this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.READ_EXTERNAL_STORAGE)
.then(status =>{
if(status.hasPermission) this.captureVideo();
});
}
}
Hope it helps.
The only thing that worked for me was the Android Diagnostic plugin. Check this post.

Categories

Resources