react-native-nfc-manager
react-native-nfc-manager copied to clipboard
NFC not working on IOS after clicking to quickly
I'm trying to create a basic NFC reading app, but I have a problem: When I click on the TouchableOpacity, then close the pop-up and open it again too quickly, nothing shows up... is there anything I forgot on the code ? No error is shown... It might be related to the nfc process taking too long... Here is what I wrote:
import React, {useState} from 'react';
import {View, Text, TouchableOpacity, StyleSheet} from 'react-native';
import NfcManager, {NfcTech} from 'react-native-nfc-manager';
// Pre-step, call this before any NFC operations
NfcManager.start();
function App() {
const [loaded, setLoaded] = useState(true);
async function readNdef() {
try {
setLoaded(false);
// register for the NFC tag with NDEF in it
await NfcManager.requestTechnology(NfcTech.Ndef);
// the resolved tag object will contain `ndefMessage` property
const tag = await NfcManager.getTag();
console.warn('Tag found', tag);
} catch (ex) {
console.warn('Oops!', ex);
} finally {
// stop the nfc scanning
try {
await NfcManager.cancelTechnologyRequest();
} catch (ex) {
console.warn('Error while canceling technology request', ex);
} finally {
setLoaded(true);
}
}
}
return (
<View style={styles.wrapper}>
{loaded && (
<TouchableOpacity onPress={readNdef}>
<Text style={{color: 'white'}}>Scan a Tag</Text>
</TouchableOpacity>
)}
</View>
);
}
const styles = StyleSheet.create({
wrapper: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
});
export default App;