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
);
Related
In my react native app which I ave published on the Google Play Store, I require Camera Roll permissions. This works during testing, but not in the published version of the app.
getPermissionAsync = async () => {
const { status } = await Permissions.askAsync(Permissions.CAMERA_ROLL);
if (status !== "granted") {
alert("Sorry, we need camera roll permissions to make this work!");
}
await this.setState({ permission: status === "granted" });
};
_pickMedia = async () => {
if (this.state.permission != true) {
await this.getPermissionAsync();
}
if (this.state.permission == true) {
// get image
}
};
In testing this works as expected, asking permission to access the camera roll every time the user tries to upload a picture till its given. However in production, the user is prompted once for permission to use the camera roll and whether or not they allow it, the alert box comes up and the user is unable to pick an image. If they try to add media again, they aren't prompted for permissions and it just alerts again.
As per the expo docs I have added READ_EXTERNAL_STORAGE to my permissions in app.json:
"android": {
"permissions": ["READ_EXTERNAL_STORAGE", "WRITE_EXTERNAL_STORAGE"],
}
Could someone tell me why this doesn't work in production?
it is not working in production becasue you did not add CAMERA permission.so, add CAMERA permission
"android": {
"permissions": ["CAMERA","READ_EXTERNAL_STORAGE", "WRITE_EXTERNAL_STORAGE"],
}
I didn't realise that edits to parts of app.json aren't updated for a published app by expo publish and you need to run expo build instead. The issue was that I hadn't initially added WRITE_EXTERNAL_STORAGE to the permissions and when I did later I used expo publish to update the app. This meant the published version wasn't actually updated. After running expo build now and generating a new app bundle, permissions started working correctly.
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 })
})
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 })
})
I had given my android-application for 'user_friends' permission review.
Today I got an alert that my application review has been completed.
When I go to 'Go to App Review' page it does not show 'Permission Rejected' Or 'Permission Approved'.
It is not available in 'Approved Items' list either!
My question : How would I know that my requested permission(user_friends) access has been granted or not!?
I created a simple app which should locate device on google maps.
My code follows official docs -
https://ionicframework.com/docs/native/geolocation/.
When I run ionic in browser (with ionic serve) it works as expected, no errors.
But when I run my app on real android device the promise never resolved nor rejected, though I am sure permission for location is granted.
this.geostate = 'asking';
this.geolocation.getCurrentPosition().then((position) => {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
this.geostate = 'coordinates ready';
processCoordinatesSomehow(latitude, longitude);
}).catch((error) => {
this.geostate = 'error';
console.log('Error getting location', error);
});
In browser it switches as expected.
But on android it always the same this.geostate === 'asking'.
For experiment reasons, in android settings for my app I set location permission to 'ask' mode.
When I tried to get location via calling appropriate method my smartphone showed me modal window asking for location permission.
I granted it, but nothing happened - this.geostate === 'asking'.
Why promise from geolocation not resolved nor rejected on android but works fine in browser?
*rebuild app not helped at all