Skip to content

Instantly share code, notes, and snippets.

@danielmcgrath
Last active June 11, 2016 18:41
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 danielmcgrath/d9bc19a3133d294cbb7a to your computer and use it in GitHub Desktop.
Save danielmcgrath/d9bc19a3133d294cbb7a to your computer and use it in GitHub Desktop.
Somewhat outdated example of creating an iOS React Native bridge for the Button SDK (https://www.usebutton.com/developers).

Based on the React Native docs for bridging native modules.

Note: This was written prior to the latest Button SDK release, and will require some tweaks to the context object. See the migration guide for details.

'use strict';
var React = require('react-native');
var {
AppRegistry,
StyleSheet,
Text,
View,
} = React;
var Button = React.NativeModules.ButtonManager;
var DropinButton = require('./dropinbutton.ios');
Button.configure('YOUR_APP_ID');
var ButtonReactNative = React.createClass({
getButtonContext: function() {
return {
subject_location: {
latitude: 40.7127,
longitude: -74.0059
}
}
},
render: function() {
return (
<View style={styles.container}>
<Text style={styles.text}>Welcome to my audience app!</Text>
<DropinButton buttonId="YOUR_BUTTON_ID"
context={this.getButtonContext()}
style={styles.button} />
</View>
);
}
});
var styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
button: {
width: 180,
height: 40
},
text: {
height: 100
}
});
AppRegistry.registerComponent('ButtonReactNative', () => ButtonReactNative);
var React = require('react-native');
var { requireNativeComponent } = React;
class DropinButton extends React.Component {
render() {
return <RCTDropinButton ref="btn" {...this.props} />;
}
}
DropinButton.propTypes = {
buttonId: React.PropTypes.string.isRequired,
context: React.PropTypes.object,
onDisplay: React.PropTypes.func
};
var RCTDropinButton = requireNativeComponent('RCTDropinButton', null);
module.exports = DropinButton;
#import "RCTBridgeModule.h"
@interface ButtonManager : NSObject <RCTBridgeModule>
@end
#import "RCTButtonManager.h"
#import <Button/Button.h>
@implementation ButtonManager
RCT_EXPORT_MODULE()
RCT_EXPORT_METHOD(configure:(NSString *)appId) {
[[Button sharedButton] configureWithApplicationId:appId completion:NULL];
}
RCT_EXPORT_METHOD(setDebugLoggingEnabled:(BOOL)enabled) {
[[Button sharedButton] setDebugLoggingEnabled:enabled];
}
@end
#import "RCTBridgeModule.h"
@interface RCTDropinButton : NSObject <RCTBridgeModule>
@end
#import "RCTDropinButton.h"
@implementation RCTDropinButton
RCT_EXPORT_MODULE()
@end
#import <RCTViewManager.h>
#import <RCTUIManager.h>
#import <RCTSparseArray.h>
#import <Button/Button.h>
@interface RCTDropinButtonManager : RCTViewManager
@end
@implementation RCTDropinButtonManager
RCT_EXPORT_MODULE()
RCT_EXPORT_VIEW_PROPERTY(buttonId, NSString)
RCT_EXPORT_VIEW_PROPERTY(context, NSDictionary)
- (UIView *)viewWithProps:(NSDictionary *)props {
BTNDropinButton *button = [[BTNDropinButton alloc] initWithButtonId:[props valueForKey:@"buttonId"]];
[button prepareForDisplayWithContext:[props valueForKey:@"context"] completion:NULL];
return button;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment