React native React Native:获得权限后调用函数
我只需要在获得位置权限后调用一个函数。 这是我的代码:React native React Native:获得权限后调用函数,react-native,React Native,我只需要在获得位置权限后调用一个函数。 这是我的代码: componentDidMount() { this.samplePermissionRequest(); alert('componentDidMount'); } samplePermissionRequest=()=>{ async function requestLocationPermission() { try { const granted = await
componentDidMount() {
this.samplePermissionRequest();
alert('componentDidMount');
}
samplePermissionRequest=()=>{
async function requestLocationPermission() {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
'title': 'Cool Location tracking App Permission',
'message': 'This Cool App needs access to your location '
}
)
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
this.justStart();
alert("You can use the app");
} else {
alert("You can not use the app");
}
} catch (err) {
console.warn(err);
}
}
requestLocationPermission();
}
justStart=()=>{
alert("start with permission");
}
当我运行这段代码时,我只得到第一个警报的对话框(警报('componentDidMount'))
如果删除行“this.justStart();”,则还会显示第二个警报。所以问题是“this.justStart();”不起作用
为什么?您必须使用Promise对象,您可以这样更改代码:
componentDidMount() {
this.requestLocationPermission().then(
alert('componentDidMount')
);
}
async requestLocationPermission() {
return PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
'title': 'Cool Location tracking App Permission',
'message': 'This Cool App needs access to your location '
}
).then(granted => {
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
this.justStart();
alert("You can use the app");
} else {
alert("You can not use the app");
}
}).catch(e => console.warn(e));
}
在保证异步方法返回时,您可以使用.then and.catch您必须使用Promise对象,您可以如下更改代码:
componentDidMount() {
this.requestLocationPermission().then(
alert('componentDidMount')
);
}
async requestLocationPermission() {
return PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
'title': 'Cool Location tracking App Permission',
'message': 'This Cool App needs access to your location '
}
).then(granted => {
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
this.justStart();
alert("You can use the app");
} else {
alert("You can not use the app");
}
}).catch(e => console.warn(e));
}
在保证异步方法返回时,您可以使用.then和.catch,因为
这个将在requestLocationPermission
函数中未定义。
因此,this.justStart()
将抛出类型错误,该错误将被try/catch语句捕获
解决方案1
调用requestLocationPermission
时绑定此
requestLocationPermission.call(此)
解决方案2
删除函数requestLocationPermission
,直接使用samplePermissionRequest
,而不是使用内部函数并再次调用它
async samplePermissionRequest = () => {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
'title': 'Cool Location tracking App Permission',
'message': 'This Cool App needs access to your location '
}
)
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
this.justStart();
alert("You can use the app");
} else {
alert("You can not use the app");
}
} catch (err) {
console.warn(err);
}
}
希望有帮助。因为这个将在requestLocationPermission
函数中未定义。
因此,this.justStart()
将抛出类型错误,该错误将被try/catch语句捕获
解决方案1
调用requestLocationPermission
时绑定此
requestLocationPermission.call(此)
解决方案2
删除函数requestLocationPermission
,直接使用samplePermissionRequest
,而不是使用内部函数并再次调用它
async samplePermissionRequest = () => {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
'title': 'Cool Location tracking App Permission',
'message': 'This Cool App needs access to your location '
}
)
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
this.justStart();
alert("You can use the app");
} else {
alert("You can not use the app");
}
} catch (err) {
console.warn(err);
}
}
希望能有帮助