React Native expo-permission deprecated 现在使用什么?
React Native expo-permission deprecated what to use now?
我正在使用 expo-permission 库中的权限来获取用户的位置坐标:
import * as Location from "expo-location";
import * as Permissions from 'expo-permissions';
const granted = await Permissions.askAsync(Permissions.LOCATION);
上面的方法有效,但不断发出警告,指出 expo-permissions 已被弃用。
如果我使用:
import {Location, Permissions } from 'expo';
它说无法读取未定义的 属性 'askAsync'。
有人知道我应该用什么吗?我使用 sdk42
谢谢!
正如这个 blog by Brent Vatne 所说,
expo-permissions has been deprecated in favor of module-specific permissions methods
You should migrate from using
Permissions.askAsync
and Permissions.getAsync
to the permissions
methods exported by modules that require the permissions.
For example: you should replace calls to
Permissions.askAsync(Permissions.CAMERA)
with
Camera.requestPermissionsAsync()
There shouldn’t be two ways to do an identical thing in a single SDK,
and so we picked our preferred approach and are consolidating around
it.
所以现在,您将不得不使用来自各个包的Permissions
对于位置,
首先,安装expo-location
expo install expo-location
那你就可以这样用了
import * as Location from 'expo-location';
let { status } = await Location.requestForegroundPermissionsAsync();
if (status !== 'granted') {
console.log('Permission to access location was denied');
return;
}
现在有了 expo,每个库都有自己的权限请求方法。
位置示例:
let { status } = await Location.requestForegroundPermissionsAsync();
如果有人来到这里并希望获得 ImagePicker
的权限,那么根据 docs 你应该这样做:
import * as ImagePicker from "expo-image-picker";
const getPermissionAsync = async () => {
const { status } = await ImagePicker.requestMediaLibraryPermissionsAsync();
if (status !== "granted") {
alert("...");
}
};
import { Camera } from "expo-camera";
this.state = {
hasCameraPermission: null,
type: Camera.Constants.Type.back,
};
componentDidMount = async () => {
const { status } = await Camera.requestCameraPermissionsAsync();
this.setState({ hasCameraPermission: status === "granted" });
};
作品:
从 'expo-camera' 导入 { 相机};
import * as ImagePicker from "expo-image-picker";
const resultPermision = await Camera.requestCameraPermissionsAsync();
const resultPermisionCamera = resultPermision.status;
if (resultPermisionCamera === "denied") {
toastRef.current.show("Gallery permissions are needed");
} else {
const result = await ImagePicker.launchImageLibraryAsync({
allowsEditing: true,
aspect: [4, 3]
})
.......
现在,expo-camera 工作得很好,但我相信 app.json 方面还有待处理。
您还需要添加这些行吗?:
“权限”:[
“相机”,
"WRITE_EXTERNAL_STORAGE",
“CAMERA_ROLL”
这里给大家提问:)
我正在使用 expo-permission 库中的权限来获取用户的位置坐标:
import * as Location from "expo-location";
import * as Permissions from 'expo-permissions';
const granted = await Permissions.askAsync(Permissions.LOCATION);
上面的方法有效,但不断发出警告,指出 expo-permissions 已被弃用。
如果我使用:
import {Location, Permissions } from 'expo';
它说无法读取未定义的 属性 'askAsync'。
有人知道我应该用什么吗?我使用 sdk42
谢谢!
正如这个 blog by Brent Vatne 所说,
expo-permissions has been deprecated in favor of
module-specific permissions methods
You should migrate from usingPermissions.askAsync
andPermissions.getAsync
to the permissions methods exported by modules that require the permissions.For example: you should replace calls to
Permissions.askAsync(Permissions.CAMERA)
withCamera.requestPermissionsAsync()
There shouldn’t be two ways to do an identical thing in a single SDK, and so we picked our preferred approach and are consolidating around it.
所以现在,您将不得不使用来自各个包的Permissions
对于位置,
首先,安装expo-location
expo install expo-location
那你就可以这样用了
import * as Location from 'expo-location';
let { status } = await Location.requestForegroundPermissionsAsync();
if (status !== 'granted') {
console.log('Permission to access location was denied');
return;
}
现在有了 expo,每个库都有自己的权限请求方法。
位置示例:
let { status } = await Location.requestForegroundPermissionsAsync();
如果有人来到这里并希望获得 ImagePicker
的权限,那么根据 docs 你应该这样做:
import * as ImagePicker from "expo-image-picker";
const getPermissionAsync = async () => {
const { status } = await ImagePicker.requestMediaLibraryPermissionsAsync();
if (status !== "granted") {
alert("...");
}
};
import { Camera } from "expo-camera";
this.state = {
hasCameraPermission: null,
type: Camera.Constants.Type.back,
};
componentDidMount = async () => {
const { status } = await Camera.requestCameraPermissionsAsync();
this.setState({ hasCameraPermission: status === "granted" });
};
作品: 从 'expo-camera' 导入 { 相机}; import * as ImagePicker from "expo-image-picker";
const resultPermision = await Camera.requestCameraPermissionsAsync(); const resultPermisionCamera = resultPermision.status;
if (resultPermisionCamera === "denied") {
toastRef.current.show("Gallery permissions are needed");
} else {
const result = await ImagePicker.launchImageLibraryAsync({
allowsEditing: true,
aspect: [4, 3]
})
.......
现在,expo-camera 工作得很好,但我相信 app.json 方面还有待处理。
您还需要添加这些行吗?: “权限”:[ “相机”, "WRITE_EXTERNAL_STORAGE", “CAMERA_ROLL”
这里给大家提问:)