我正在尝试将 TextInput 的值传递给,let bytes = buildUrlPayload('https://www.google.co.uk');
以便可以在应用程序中动态写入 url。谁能帮我实现这一目标?谢谢
function buildUrlPayload(valueToWrite) {
return Ndef.encodeMessage([
Ndef.uriRecord(valueToWrite),
]);
}
class AppV2Ndef extends React.Component {
constructor(props){
super(props)
this.state = {
log: "Ready...",
text: ""
}
}
componentDidMount() {
NfcManager.start();
}
componentWillUnmount() {
this._cleanUp();
}
onChangeText = (text) => {
this.setState({
text
})
}
render() {
return (
<TouchableWithoutFeedback onPress={Keyboard.dismiss}>
<View style={{padding: 20}}>
<Text style={styles.text_footer}>Web</Text>
<View style={styles.action}>
<Ionicons name="globe-outline" color="#05375a" size={20} />
<TextInput
placeholder="URL"
style={styles.textInput}
autoCapitalize="none"
autoCorrect="false"
onChangeText={this.onChangeText}
/>
</View>
<TouchableOpacity
style={{padding: 10, width: 200, margin: 20, borderWidth: 1, borderColor: 'black'}}
onPress={this._URLNdef}
>
<Text>Write to Tag</Text>
</TouchableOpacity>
_cleanUp = () => {
NfcManager.cancelTechnologyRequest().catch(() => 0);
}
_URLNdef = async () => {
try {
let resp = await NfcManager.requestTechnology(NfcTech.Ndef, {
alertMessage: 'Ready SparX Magic!'
});
console.warn(resp);
let ndef = await NfcManager.getNdefMessage();
console.warn(ndef);
let bytes = buildUrlPayload('https://www.google.co.uk');
await NfcManager.writeNdefMessage(bytes);
console.warn('successfully write ndef');
await NfcManager.setAlertMessageIOS('Get in there it works!');
this._cleanUp();
} catch (ex) {
console.warn('ex', ex);
this._cleanUp();
}
}
export default AppV2Ndef;