From bd4e61e186c20c4e1cd0a1af68888bb50024ecff Mon Sep 17 00:00:00 2001 From: Alekos Filini Date: Tue, 15 Oct 2024 19:05:04 +0200 Subject: [PATCH] Update react-native example for iOS --- .../LibportalReactNativeExample/Info.plist | 2 + .../LibportalReactNativeExample.entitlements | 10 +++ .../example/src/App.tsx | 88 ++++++++++++++----- 3 files changed, 79 insertions(+), 21 deletions(-) create mode 100644 sdk/libportal-react-native/example/ios/LibportalReactNativeExample/LibportalReactNativeExample.entitlements diff --git a/sdk/libportal-react-native/example/ios/LibportalReactNativeExample/Info.plist b/sdk/libportal-react-native/example/ios/LibportalReactNativeExample/Info.plist index 49a4b68..78af4ff 100644 --- a/sdk/libportal-react-native/example/ios/LibportalReactNativeExample/Info.plist +++ b/sdk/libportal-react-native/example/ios/LibportalReactNativeExample/Info.plist @@ -48,5 +48,7 @@ UIViewControllerBasedStatusBarAppearance + NFCReaderUsageDescription + Read NFC tags diff --git a/sdk/libportal-react-native/example/ios/LibportalReactNativeExample/LibportalReactNativeExample.entitlements b/sdk/libportal-react-native/example/ios/LibportalReactNativeExample/LibportalReactNativeExample.entitlements new file mode 100644 index 0000000..2bb4dee --- /dev/null +++ b/sdk/libportal-react-native/example/ios/LibportalReactNativeExample/LibportalReactNativeExample.entitlements @@ -0,0 +1,10 @@ + + + + + com.apple.developer.nfc.readersession.formats + + TAG + + + diff --git a/sdk/libportal-react-native/example/src/App.tsx b/sdk/libportal-react-native/example/src/App.tsx index 53fe9ad..fceebf5 100644 --- a/sdk/libportal-react-native/example/src/App.tsx +++ b/sdk/libportal-react-native/example/src/App.tsx @@ -1,9 +1,10 @@ import React, { useState } from 'react'; -import {View, Text, TouchableOpacity, StyleSheet} from 'react-native'; +import { Platform, View, Button, Text, TouchableOpacity, StyleSheet } from 'react-native'; import NfcManager, { NfcTech } from 'react-native-nfc-manager'; import { PortalSdk, type NfcOut, type CardStatus } from 'libportal-react-native'; const sdk = new PortalSdk(true); +let paused = false; function livenessCheck(): Promise { return new Promise((_resolve, reject) => { @@ -22,30 +23,56 @@ function livenessCheck(): Promise { async function manageTag() { await sdk.newTag(); - const check = livenessCheck(); + const check = Platform.select({ + ios: () => new Promise(() => {}), + android: () => livenessCheck(), + })(); while (true) { const msg = await Promise.race([sdk.poll(), check]); // console.trace('>', msg.data); - const result = await NfcManager.nfcAHandler.transceive(msg.data); - // console.trace('<', result); - await sdk.incomingData(msg.msgIndex, result); + if (!paused) { + const result = await NfcManager.nfcAHandler.transceive(msg.data); + // console.trace('<', result); + await sdk.incomingData(msg.msgIndex, result); + } + } +} + +async function getOneTag() { + console.info('Looking for a Portal...'); + paused = false; + + let restartInterval = null; + try { + await NfcManager.requestTechnology(NfcTech.NfcA, {}); + + if (Platform.OS === 'ios') { + restartInterval = setInterval(() => { + paused = true; + NfcManager.restartTechnologyRequestIOS().then((_) => { + paused = false; + }); + }, 17500); + } + + await manageTag(); + } catch (ex) { + if (Platform.OS === 'ios') { + NfcManager.invalidateSessionWithErrorIOS(ex); + } + console.warn('Oops!', ex); + } + + if (restartInterval) { + clearInterval(restartInterval); } + NfcManager.cancelTechnologyRequest({ delayMsAndroid: 0 }); } async function listenForTags() { while (true) { - console.info('Looking for a Portal...'); - - try { - await NfcManager.registerTagEvent(); - await NfcManager.requestTechnology(NfcTech.NfcA, {}); - await manageTag(); - } catch (ex) { - console.warn('Oops!', ex); - } finally { - NfcManager.cancelTechnologyRequest({ delayMsAndroid: 0 }); - } + await getOneTag(); } } @@ -53,7 +80,11 @@ NfcManager.isSupported() .then((value) => { if (value) { NfcManager.start(); - return listenForTags(); + + if (Platform.OS === 'android') { + return listenForTags(); + } + } else { throw "NFC not supported"; } @@ -62,14 +93,29 @@ NfcManager.isSupported() function App() { const [status, setStatus] = useState(null); async function getStatus() { - setStatus(await sdk.getStatus()) + if (Platform.OS === 'ios') { + getOneTag(); + } + + setStatus(await sdk.getStatus()); + + if (Platform.OS === 'ios') { + NfcManager.cancelTechnologyRequest({ delayMsAndroid: 0 }); + } + } + + function resetStatus() { + setStatus(null); } return ( - + + { JSON.stringify(status) } @@ -84,4 +130,4 @@ const styles = StyleSheet.create({ }, }); -export default App; \ No newline at end of file +export default App;