React native 反应本机未处理的承诺拒绝p0
您好,当我运行我的项目时,我遇到了上述错误,我做错了什么React native 反应本机未处理的承诺拒绝p0,react-native,React Native,您好,当我运行我的项目时,我遇到了上述错误,我做错了什么 import * as WebBrowser from 'expo-web-browser'; import React , {Component} from 'react'; import {PermissionsAndroid} from 'react-native'; import { Image, Platform, ScrollView, StyleSheet, Text, But
import * as WebBrowser from 'expo-web-browser';
import React , {Component} from 'react';
import {PermissionsAndroid} from 'react-native';
import {
Image,
Platform,
ScrollView,
StyleSheet,
Text,
Button,
TouchableOpacity,
View,
Switch
} from 'react-native';
import { MonoText } from '../components/StyledText';
import {Linking , SMS }from 'expo';
import * as Permissions from 'expo-permissions'
class Sendsms extends Component {
state = {switch1Value: false}
toggleSwitch1 = (value) => {
this.setState({switch1Value: value});
console.log('Switch 1 is: ' + value);
}
askSMSPermissionsAsync = async () => {
await Permissions.askAsync(Permissions.SMS);
};
HandlePress = () =>{
console.log('try to send sms');
}
render(){
return(
<View>
<Text>{this.state.switch1Value ? 'Switch is ON' : 'Switch is OFF'}</Text>
<Switch onValueChange = {this.toggleSwitch1.bind(this)} value = {this.state.switch1Value}/>
<Button title="sendSMS" onPress={this.askSMSPermissionsAsync} />
</View>
)
}
}
您需要将您的
wait
呼叫包装为try/catch
,以处理被拒绝的承诺。我不确定它为什么会被拒绝,但您可以通过执行以下操作console.log
来了解:
askSMSPermissionsAsync = async () => {
try {
await Permissions.askAsync(Permissions.SMS);
} catch(e) {
console.log(e);
}
};
记住,
await
只是承诺的语法糖。await Permissions.askAsync(Permissions.SMS)代码>导致可能被拒绝的承诺。使用承诺时,请始终使用try{}catch(e){}
捕获任何拒绝。
有关拒绝的更多信息,请查看日志
因此:
查看世博会许可的文档:它应该是getAsync
,而不是askAsync
。
askSMSPermissionsAsync = async () => {
try {
await Permissions.askAsync(Permissions.SMS);
} catch(e) {
console.log(e);
}
};
askSMSPermissionsAsync = async () => {
try {
await Permissions.askAsync(Permissions.SMS);
} catch (e) {
console.error(e);
}
};