Created
February 25, 2017 04:39
-
-
Save mikesurowiec/fef0afabd0d3abfcdca80f8392d421cd to your computer and use it in GitHub Desktop.
React Native Swift & Promises
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
// ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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