User Permissions in React Native for IOS/Android - 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');
}
}

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 do i add permissions to AndroidManifest via expo plugins?

My expo react native application comunicates with a zebra printer via a HTTP post request this works just fine in web , however on android and I suspect also iOS this isnt the case, from what i gather i need to add something along the lines of :
<uses-permission android:name="android.permission.INTERNET" />
Into androidmanifest.xml however expo doesnt give you direct access to that file , so i need to make a expo plugin that puts the permission into androidmanifest via expo.
I found this post teaching how to remove permissions however my current case requires me to add permissions ill still link the code from the post down here:
const { withAndroidManifest } = require("#expo/config-plugins")
module.exports = function androiManifestPlugin(config) {
return withAndroidManifest(config, async config => {
let androidManifest = config.modResults.manifest
// add the tools to apply permission remove
androidManifest.$ = {
...androidManifest.$,
"xmlns:tools": "http://schemas.android.com/tools",
}
// add remove property to the audio record permission
androidManifest["uses-permission"] = androidManifest["uses-permission"].map(
perm => {
if (perm.$["android:name"] === "android.permission.RECORD_AUDIO") {
perm.$["tools:node"] = "remove"
}
return perm
}
)
return config
})
}
Does anyone know how to add http permissions to my expo app? Thanks.
To add permissions you do not need expo-plugins.
You simply need to add the permission in your app.json (or app.config.js if you have dynamic configuration)
On Android, permissions are little bit simpler than iOS. In the managed workflow, permissions are controlled via the android.permissions property in your app.json file.
https://docs.expo.dev/guides/permissions/

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();

Getting list of available Wifi's on react native(android)

I want to get the available WiFi networks on my react-native android app. I went through a few blog posts and StackOverflow questions, but I am unable to achieve the desired functionality.
react-native-android-wifi is an npm package, which is expected to achieve the functionality, but I am getting an error of PermissionsAndroid not defined when trying to execute the code for asking user's permission to enable WiFi. Below is the code,
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
'title': 'Wifi networks',
'message': 'We need your permission in order to find wifi networks'
}
)
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log("Thank you for your permission! :)");
} else {
console.log("You will not able to retrieve wifi available networks list");
}
} catch (err) {
console.warn(err)
}
Can someone please help me out here and please tell me how can I reach my desired outcome.
I used this in one of my projects https://github.com/blackdeve/react-native-wifi
Actually the library doesn't have a documented method for listing out available wifis but looking at the source code you can see there is a public method called loadWifiList
You need to follow the instructions on the github page and add stuff to settings.gradle. Here is the method I am talking about:
#ReactMethod
public void loadWifiList(Callback successCallback, Callback errorCallback) {
}

React-Native: How to access react-native app's permissions in Android?

I'm trying to access my react-native app's specific permissions in Android. I can access and pull up my app's info page in Android, but I can't go directly into the permissions screen from my app.
I am using react-native-android-open-settings to to achieve this. By using this I am able to access the app's info page, but can't access the specific permissions page for the app without the user having to click on permissions in App Info. The library can be found here: https://www.npmjs.com/package/react-native-android-open-settings
import AndroidOpenSettings from 'react-native-android-open-settings'
async goToSettings() {
if(Platform.OS === 'android') {
AndroidOpenSettings.appDetailsSettings()
} else {
Linking.canOpenURL('app-settings:').then(supported => {
if (!supported) {
console.log('Can\'t handle settings url');
} else {
return Linking.openURL('app-settings:');
}
}).catch(err => console.error('An error occurred', err));
}
}
The expected result is for it to open the permissions page and not the app info page.

Categories

Resources