https://github.com/evanbacon/expo-swift-example
Example of writing Swift modules in Expo
https://github.com/evanbacon/expo-swift-example
cross-platform expo ios native-modules react-native swift
Last synced: 11 months ago
JSON representation
Example of writing Swift modules in Expo
- Host: GitHub
- URL: https://github.com/evanbacon/expo-swift-example
- Owner: EvanBacon
- Created: 2018-08-22T20:09:28.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-08-22T20:20:01.000Z (over 7 years ago)
- Last Synced: 2025-03-19T07:32:32.490Z (11 months ago)
- Topics: cross-platform, expo, ios, native-modules, react-native, swift
- Language: Ruby
- Size: 3.51 MB
- Stars: 12
- Watchers: 3
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# expo-swift-example
Example of writing Swift modules in Expo
## How To
First detach your expo project with:
```sh
exp detach
```
Then open your iOS project, and add a swift file. You will also need a `.m` file of the same name.
* `DemoModule.swift`
* `DemoModule.m`
You should have been prompted to add a bridging header.
To check to make sure your header is linked:
* Click on the project Icon (Blue icon on the top left)
* Click the **Build Settings** tab
* Go to the section **Swift Compiler - General**
* The value for **Objective-C Bridging Header** should be `[project]/[project]-Bridging-Header.h`.
* Ex: `expo-swift-example/expo-swift-example-Bridging-Header.h`
In your bridging header, import React Native libraries:
```objc
#import
#import
```
Now in your swift file:
```swift
@objc(DemoModule)
class DemoModule: RCTEventEmitter {
@objc func someMethod(_ input: String) -> Void {}
override func supportedEvents() -> [String]! {
return []
}
@objc override func constantsToExport() -> [AnyHashable : Any]! {
return []
}
}
```
In your `.m` file:
```objc
#import
#import
@interface RCT_EXTERN_MODULE(DemoModule, NSObject)
RCT_EXTERN_METHOD(someMethod:(NSString *)input);
@end
```
And in JS:
```js
import { NativeModules } from 'react-native';
NativeModules.DemoModule.someMethod(input);
```