Skip to content

Instantly share code, notes, and snippets.

@mikesurowiec
Created February 25, 2017 04:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikesurowiec/fef0afabd0d3abfcdca80f8392d421cd to your computer and use it in GitHub Desktop.
Save mikesurowiec/fef0afabd0d3abfcdca80f8392d421cd to your computer and use it in GitHub Desktop.
React Native Swift & Promises
import {
NativeModules,
} from 'react-native';
const { AudioManager } = NativeModules;
async function setPlaying (shouldPlay: boolean) => {
const isPlayingNativeResult = await AudioManager.setPlaying(shouldPlay);
// do something with the native result
}
#import "RCTBridgeModule.h"
// This registers your module with React Native, so it can be exposed to JS
@interface RCT_EXTERN_MODULE(AudioManager, NSObject)
RCT_EXTERN_METHOD(setPlaying:(BOOL) value
resolver:(RCTPromiseResolveBlock *)resolve
rejecter:(RCTPromiseRejectBlock *)reject
)
// ... other methods
@end
import Foundation
@objc(AudioManager)
class AudioManager: RCTEventEmitter {
// ...
@objc func setPlaying(_ shouldPlay: Bool,
resolver resolve: RCTPromiseResolveBlock,
rejecter reject: RCTPromiseRejectBlock) -> Void {
let isPlaying = setPlaying(shouldPlay)
resolve(isPlaying)
}
// ...
}
#import "RCTBridgeModule.h"
#import "RCTEventDispatcher.h"
#import "RCTEventEmitter.h"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment