From 151ed6d78bb386d7d1d4688fa0a57aa89b38387a Mon Sep 17 00:00:00 2001 From: Artur Gurgul Date: Sun, 3 Aug 2025 12:53:34 +0200 Subject: [PATCH] Example of calling swift code from RN --- App.tsx | 30 ++++++++++++++++-- .../RNPlayground-Bridging-Header.h | 1 + .../Application/Services/TestingService.swift | 31 +++++++++++++++++++ .../Services/TestingServiceModuleBridge.m | 17 ++++++++++ .../Application/Shared/SharedState.swift | 2 +- .../{ => Views}/BaseButton/BaseButton.swift | 2 +- .../BaseButton/BaseButtonManager.m | 0 .../BaseButton/BaseButtonManager.swift | 0 .../CustomButton/CustomButton.swift | 0 .../CustomButton/CustomButtonManager.m | 7 +++++ .../CustomButton/CustomButtonManager.swift | 0 .../Application/Views/ToolboxHeader.swift | 2 +- 12 files changed, 86 insertions(+), 6 deletions(-) create mode 100644 ios/Native/Application/Services/TestingService.swift create mode 100644 ios/Native/Application/Services/TestingServiceModuleBridge.m rename ios/Native/Application/{ => Views}/BaseButton/BaseButton.swift (76%) rename ios/Native/Application/{ => Views}/BaseButton/BaseButtonManager.m (100%) rename ios/Native/Application/{ => Views}/BaseButton/BaseButtonManager.swift (100%) rename ios/Native/Application/{ => Views}/CustomButton/CustomButton.swift (100%) rename ios/Native/Application/{ => Views}/CustomButton/CustomButtonManager.m (52%) rename ios/Native/Application/{ => Views}/CustomButton/CustomButtonManager.swift (100%) diff --git a/App.tsx b/App.tsx index 0175c62..765ecbd 100644 --- a/App.tsx +++ b/App.tsx @@ -1,12 +1,13 @@ import { NewAppScreen } from '@react-native/new-app-screen' import { useEffect, useState } from 'react' -import { StatusBar, Text, StyleSheet, useColorScheme, ScrollView } from 'react-native' +import { StatusBar, Text, StyleSheet, useColorScheme, ScrollView, Button, Alert } from 'react-native' import { NativeEventEmitter, NativeModules } from 'react-native' -const { Emitter } = NativeModules +const { Emitter, TestingServiceModule } = NativeModules import { requireNativeComponent } from 'react-native' import type { StyleProp, ViewStyle } from 'react-native' + type CustomButtonProps = { style?: StyleProp } @@ -15,9 +16,23 @@ const BaseButton = requireNativeComponent('BaseButton') export default function App() { const isDarkMode = useColorScheme() === 'dark' - const [message, setMessage] = useState(null) + const [message, setMessage] = useState(null) const [color, setColor] = useState("#FFF") + + const onPress = async () => { + if (!TestingServiceModule?.greet) { + Alert.alert('Module not found', 'MyNativeModule is not linked or not exported.') + return + } + try { + const result = await TestingServiceModule.greet('John') + setMessage(result); + } catch (e) { + //Alert.alert('Error', String(e?.message ?? e)) + } + } + useEffect(() => { const emitter = new NativeEventEmitter(Emitter); const subscription = emitter.addListener('onMessage', (event) => { @@ -28,6 +43,14 @@ export default function App() { setColor("#00F") } } + + TestingServiceModule.greet('John') + .then((message: string) => { + setMessage(message) + }) + .catch((error: string) => { + console.error(error); + }); }) return () => { @@ -41,6 +64,7 @@ export default function App() { {message ?? 'Waiting for message...'} +