Why does the react-native-permissions cannot request the permission in Android? - android

Information
I tried to create an app to request permission in the react native side. There is a button in a screen to be clicked to check and request permission.
Problem
react-native-permission link: https://www.npmjs.com/package/react-native-permissions
When I follow the instruction in the link and create a demo app, I cannot request the permission and the request result is blocked.
Screenshots
Step 1: The main screen of app with permission request button
Step 2: The pop-out window
I click button at main screen(step 1), the step 2 will show and disappear quickly and back to screen at step 1.
I am new to React-Native, I don't know how to solve the problem. It is appreciated that anyone can help me to solve the problem. Thank you very much.
Github Link
https://github.com/TrifaC/HradwarePermissionTestApp.git

Try PermissionsAndroid from react-native package
import { PermissionsAndroid} from "react-native";
const requestCameraPermission = async () => {
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.",
buttonNeutral: "Ask Me Later",
buttonNegative: "Cancel",
buttonPositive: "OK"
}
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log("You can use the camera");
} else {
console.log("Camera permission denied");
}
} catch (err) {
console.warn(err);
}
};

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Please Add in your AndroidManifest.xml

Related

Expo Media Library permissions error on Android [React Native]

Hi I'm working on a react native app and I'm using expo-media-library to get user's photos but, only on android, when I try to ask for permissions using the following command I get the status as never_ask_again even if it's the first time I'm asking for permissions and I don't event refuse them because the popup didn't show up.
MediaLibrary.requestPermissionsAsync()
Then I tried to manually give permissions to the app, through settings, but when expo try to access to media library, here's the error that occurs.
Error: Missing MEDIA_LIBRARY permissions.
I also tried to use react-native-permissions to ask for permissions, but the result is the same.
Maybe someone has had this problem before and can help me, thanks.
=> Do you add this in your manifest file or not..?
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
const permission = await Permissions.getAsync(Permissions.CAMERA_ROLL);
if (permission.status !== 'granted') {
const newPermission = await Permissions.askAsync(Permissions.CAMERA_ROLL);
if (newPermission.status === 'granted') {
//its granted.
}
} else {
....your code
}

How to detect permission dialog is denied by user

I am implementing take pictures from galley in my app, for which I am adding storage permission.
And I am requesting the permission from user using permission_handler package. And it is working perfectly in Android because it manages user state like permanently denied etc.
But the main issue in the iOS in which either user deny or allow always shows user granted permission(Means always providing the status == granted)
Please help if someone have same issue or I am doing anything wrong.
Thank you.
Add permission_handler: ^6.1.1 to you pubspec file
import it
import 'package:permission_handler/permission_handler.dart';
in you landing page or main.dart try calling this bellow functoin
bool storagePer = false;
void storageCheck() async {
var status = await Permission.storage.status;
if (!status.isGranted) {
await Permission.storage.request();
storagePer = true;
print('per: $storagePer');
}
}
To check if you have permission or not, use status.isGranted if it return true => permissionGranted else denied
to request permition use this await Permission.storage.request();

Remove background location access on Android

I am using react-native-geolocation-service to fetch the location of the user. On iOS it is easy to just ask for location access when the app is in the foreground, you just skip adding background location access as a capability. However, I don't understand how you remove this permission on Android. I don't have the ACCESS_BACKGROUND_LOCATION permission set and I'm still getting the option "Allow all the time" on e.g. my Pixel 2.
This is the code I am using to fetch the location of the user:
const getCurrentPosition = async (onSuccess, onError) => {
if (Platform.OS === 'android') {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
title: 'My title',
message: 'My message',
buttonPositive: 'Continue',
}
);
if (granted !== PermissionsAndroid.RESULTS.GRANTED) {
onError();
return;
}
}
Geolocation.getCurrentPosition(
(result) => {
const position = {
longitude: result.coords.longitude,
latitude: result.coords.latitude,
};
onSuccess(position);
},
(error) => {
onError();
},
{ enableHighAccuracy: true, maximumAge: 10000, timeout: 15000 }
);
};
Look into your main AndroidManifest.xml file (and by main I mean under android/app/src/main(COULD_BE_SOMETHING_ELSE_USUALLY_MAIN_BUT_YMMV)/AndroidManifest.xml)
You need to find a bunch of <uses-permission />. Check if one of these bad boys got ACCESS_BACKGROUND_LOCATION:
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
No luck? Fear not, my dear friend. Here's a remedy for your battle-fatigued-from-dealings-with-react-native mind. Add this line under the last <uses-permission> tag.
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"
tools:node="remove" />
May peace prevail.
Technical nitty gritty:
Actually, a library, like react-native-geolocation-service or react-native-background-geolocation, can alter final AndroidManifest.xml, which will go to a bundle edited heavily.
Simply deleting a line won't cut it, because you don't have an access to that final draft, cause it's auto-generated. You need to be one step ahead and use tools:node="remove" to make sure it will be deleted from the final version of the manifest file.
Sources:
[1] https://developer.android.com/studio/build/manifest-merge
[2] https://github.com/transistorsoft/react-native-background-geolocation/issues/1149

ReactNative : permission always return 'never ask again'

am using following code base to request the permission but it always return 'never ask again'
async requestPermission(request){
try{
const response= 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.'
})
console.log(response)
}catch(err){
}
this.getcurrentLocation()
}
//Response never_ask_again
I recently faced this issue about LOCATION service permission.
I forgot to add android.permission.ACCESS_FINE_LOCATION in AndroidManifest.xml.
So, I think you also forgot to add
<uses-permission android:name="android.permission.CAMERA" />
in your AndroidManifest.xml
I did clean and rebuild several times and finally started working, somehow the build cache is not properly updating.
Removing tools:node="remove" from AndroidManifest.xml fixed mine.
Replace
<uses-permission tools:node="remove" android:name="android.permission.CAMERA"/>
with
<uses-permission android:name="android.permission.CAMERA"/>
https://github.com/zoontek/react-native-permissions/issues/504#issuecomment-914417383

User Permissions in React Native for IOS/Android

I want to use Internet in app and need to ask for permission for both IOS/Android. I have checked out react-native-permissions but could not set up the module because of the react native link command. What is the easiest way to grant permissions in React-native? Any examples?
You can use internet by default if you use Expo (no need to ask internet permission). But if you want to ask the others permission check this link https://docs.expo.io/versions/latest/sdk/permissions.html
Example code :
async function getLocationAsync() {
const { Location, Permissions } = Expo;
const { status } = await Permissions.askAsync(Permissions.LOCATION);
if (status === 'granted') {
return Location.getCurrentPositionAsync({enableHighAccuracy: true});
} else {
throw new Error('Location permission not granted');
}
}

Categories

Resources