Skip to content

Instantly share code, notes, and snippets.

@skevy
Last active April 19, 2016 20:22
Show Gist options
  • Save skevy/690fa92fca8dfc00ff6a to your computer and use it in GitHub Desktop.
Save skevy/690fa92fca8dfc00ff6a to your computer and use it in GitHub Desktop.
Branch Integration with React Native

This set of files demonstrate a very basic integration with Branch.IO on React Native.

This is not necessarily production ready...haven't cleaned up the code or anything.

I'm not currently supporting this, but I'll try to answer comments!

import { NativeModules } from 'react-native';
import RCTDeviceEventEmitter from 'RCTDeviceEventEmitter';
const BranchLinkingManager = NativeModules.BranchLinkingManager;
const BRANCH_DEEP_LINK_NOTIF_EVENT = 'openBranchDeepLink';
const _notifHandlers = new Map();
export class BranchLinkingIOS {
static addEventListener(type: string, handler: Function) {
const listener = RCTDeviceEventEmitter.addListener(
BRANCH_DEEP_LINK_NOTIF_EVENT,
handler
);
_notifHandlers.set(handler, listener);
}
static removeEventListener(type: string, handler: Function) {
const listener = _notifHandlers.get(handler);
if (!listener) {
return;
}
listener.remove();
_notifHandlers.delete(handler);
}
static popLastBranchParams(cb) {
BranchLinkingManager.getLatestParams((err, params) => {
cb(err, params);
});
}
}
//
// BranchLinkingManager.h
//
// Created by Adam Miskiewicz on 8/6/15.
//
#import "RCTBridgeModule.h"
@interface BranchLinkingManager : NSObject <RCTBridgeModule>
+ (void)openBranchDeepLinkWithParams:(NSDictionary*)params;
@end
//
// BranchLinkingManager.m
//
// Created by Adam Miskiewicz on 8/6/15.
//
#import "BranchLinkingManager.h"
#import "RCTBridge.h"
#import "RCTEventDispatcher.h"
#import "RCTUtils.h"
NSString *const BranchDeepLinkNotification = @"BranchDeepLinkNotification";
@interface BranchLinkKeeper : NSObject
@property (nonatomic, strong) NSDictionary *lastParams;
+ (BranchLinkKeeper *)shared;
@end
@implementation BranchLinkKeeper
+ (BranchLinkKeeper *)shared
{
static BranchLinkKeeper *sharedBranchLinkKeeper = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedBranchLinkKeeper = [[self alloc] init];
});
return sharedBranchLinkKeeper;
}
@end
@implementation BranchLinkingManager
@synthesize bridge = _bridge;
RCT_EXPORT_MODULE()
- (instancetype)init
{
if ((self = [super init])) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleDeepLinkNotification:)
name:BranchDeepLinkNotification
object:nil];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
+ (void)openBranchDeepLinkWithParams:(NSDictionary *)params
{
if (params) {
NSDictionary *payload = @{@"params": params};
[BranchLinkKeeper shared].lastParams = params;
[[NSNotificationCenter defaultCenter] postNotificationName:BranchDeepLinkNotification
object:self
userInfo:payload];
}
}
- (void)handleDeepLinkNotification:(NSNotification *)notification
{
[_bridge.eventDispatcher sendDeviceEventWithName:@"openBranchDeepLink"
body:[notification userInfo]];
}
RCT_EXPORT_METHOD(getLatestParams:(RCTResponseSenderBlock)callback)
{
callback(@[RCTNullIfNil(nil), RCTNullIfNil([BranchLinkKeeper shared].lastParams)]);
}
@end
import {NativeModules} from 'react-native';
const {BranchSharingManager} = NativeModules;
export class BranchSharingIOS {
static showShareActionSheetWithOptions(options: Object, failureCallback: Function, successCallback:Function) {
BranchSharingManager.showShareActionSheetWithOptions(options, failureCallback, successCallback);
}
}
//
// BranchSharingManager.h
//
// Created by Adam Miskiewicz on 8/6/15.
//
#import "RCTBridgeModule.h"
@interface BranchSharingManager : NSObject <RCTBridgeModule>
@end
//
// BranchSharingManager.m
//
// Created by Adam Miskiewicz on 8/6/15.
//
#import "Branch.h"
#import "BranchSharingManager.h"
#import "RCTLog.h"
#import "RCTUtils.h"
@implementation BranchSharingManager
{
NSMutableDictionary *_callbacks;
}
RCT_EXPORT_MODULE()
- (instancetype)init
{
if ((self = [super init])) {
_callbacks = [[NSMutableDictionary alloc] init];
}
return self;
}
- (dispatch_queue_t)methodQueue
{
return dispatch_get_main_queue();
}
RCT_EXPORT_METHOD(showShareActionSheetWithOptions:(NSDictionary *)options
failureCallback:(RCTResponseSenderBlock)failureCallback
successCallback:(RCTResponseSenderBlock)successCallback)
{
NSMutableArray *items = [NSMutableArray array];
id message = options[@"message"];
id url = options[@"url"];
if ([message isKindOfClass:[NSString class]]) {
[items addObject:message];
}
if ([url isKindOfClass:[NSString class]]) {
[items addObject:[NSURL URLWithString:url]];
}
if ([items count] == 0) {
failureCallback(@[@"No `url` or `message` to share"]);
return;
}
id branchData = options[@"branchData"];
if ([branchData isKindOfClass:[NSDictionary class]]) {
id params = branchData[@"params"];
id feature = branchData[@"feature"];
id stage = branchData[@"stage"];
//id tags = branchData[@"tags"];
UIActivityItemProvider *itemProvider = [Branch getBranchActivityItemWithParams:params feature:feature stage:stage tags:nil];
[items addObject:itemProvider];
}
UIActivityViewController *share = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
UIViewController *ctrl = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0
if (![UIActivityViewController instancesRespondToSelector:@selector(setCompletionWithItemsHandler:)]) {
// Legacy iOS 7 implementation
share.completionHandler = ^(NSString *activityType, BOOL completed) {
successCallback(@[@(completed), RCTNullIfNil(activityType)]);
};
} else
#endif
{
// iOS 8 version
share.completionWithItemsHandler = ^(NSString *activityType, BOOL completed, __unused NSArray *returnedItems, NSError *activityError) {
if (activityError) {
failureCallback(@[RCTNullIfNil(activityError.localizedDescription)]);
} else {
successCallback(@[@(completed), RCTNullIfNil(activityType)]);
}
};
}
[ctrl presentViewController:share animated:YES completion:nil];
}
@end
@tomauty
Copy link

tomauty commented Apr 19, 2016

anyone who arrives at this — branch just released a RN SDK https://github.com/BranchMetrics/React-Native-Deep-Linking-SDK

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment