Create the iOS bridge

Create the native files for your module by opening your workspace file on Xcode, select your project, and right-click to add a new file. Select Cocoa Touch Class, give the name, and create it.

Step 1 - BridgeClassDemo.h

Create the bridge by copying the code below into your BridgeClassDemo.h file

BridgeClassDemo.h
//
//  BridgeClassDemo.h
//  RaxelPulseReactNativeDemo
//
//  Created by Sergey Emelyanov on 19/04/2020.
//  Copyright © 2020 Data motion Pte.Ltd. All rights reserved.
//  https://telematicssdk.com

#import "React/RCTBridgeModule.h"

// Instead of BridgeClassDemo put the name of your module
@interface BridgeClassDemo : NSObject <RCTBridgeModule>
@end

Step 2 - DemoClass.m

Add the code below into your DemoClass.m file:

DemoClass.m
//
//  BridgeClassDemo.m
//  RaxelPulseReactNativeDemo
//
//  Created by Sergey Emelyanov on 19/04/2020.
//  Copyright © 2020 Data motion Pte.Ltd. All rights reserved.
//  https://telematicssdk.com

#import <React/RCTLog.h>
#import <RaxelPulse/RaxelPulse.h>
#import <AdSupport/AdSupport.h>
#import "BridgeClassDemo.h" // Here put the name of your module

@implementation BridgeClassDemo // Here put the name of your module

// This RCT (React) "macro" exposes the current module to JavaScript

RCT_EXPORT_MODULE(BridgeClassDemo);

RCT_EXPORT_METHOD(start:(NSString *)token) {
  dispatch_async(dispatch_get_main_queue(), ^{
    [RPEntry instance].virtualDeviceToken = @"DeviceToken";
    [RPEntry instance].disableTracking = NO;
  });
}

RCT_EXPORT_METHOD(enableSDK) {
  dispatch_async(dispatch_get_main_queue(), ^{
      [[RPEntry instance] setEnableSdk: true];
  });
}

RCT_EXPORT_METHOD(disableSDK) {
  dispatch_async(dispatch_get_main_queue(), ^{
      [[RPEntry instance] setDisableWithUpload];
  });
}

/** You can add any required methods by yourself 
* using this code as a template

RCT_EXPORT_METHOD(yourmetod) {
  dispatch_async(dispatch_get_main_queue(), ^{
      //Your native command
  });
}

*/

@end

Step 3 - AppDelegate

Handle library methods for AppDelegate as it is in the example below:

#import "AppDelegate.h"
#import <React/RCTBridge.h>
#import <React/RCTRootView.h>
#import <RaxelPulse/RaxelPulse.h>
#import <React/RCTLog.h>
#import <AdSupport/AdSupport.h>

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  [[RPCPermissionsWizard returnInstance] launchWithFinish:^(BOOL showWizzard) {
    [RPEntry initializeWithRequestingPermissions:YES];
  }];
  RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
  RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
    moduleName:@"RCdemo_mobile"
    initialProperties:nil];
  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  UIViewController *rootViewController = [UIViewController new];
  rootViewController.view = rootView;
  self.window.rootViewController = rootViewController;
  [self.window makeKeyAndVisible];
  return YES;
}
  
- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(nonnull NSString *)identifier completionHandler:(nonnull void (^)(void))completionHandler {
  [RPEntry application:application handleEventsForBackgroundURLSession:identifier completionHandler:completionHandler];
}
  
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
  [RPEntry applicationDidReceiveMemoryWarning:application];
}
  
- (void)applicationWillTerminate:(UIApplication *)application {
  [RPEntry applicationWillTerminate:application];
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
  [RPEntry applicationDidEnterBackground:application];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
  [RPEntry applicationDidBecomeActive:application];
}

- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  [RPEntry application:application performFetchWithCompletionHandler:^{
    completionHandler(UIBackgroundFetchResultNewData);
  }];
}

@end

And that’s it for the native side.

Step 4 - React Native side

We can now call it anywhere on the React Native side:

import { NativeModules } from 'react-native';
var BridgeClassDemo = NativeModules.BridgeClassDemo;
//Let's show it
BridgeClassDemo.start(@"DeviceToken")
BridgeClassDemo.enableSDK()
BridgeClassDemo.disableSDK()
//BridgeClassDemo.yourmetod()

Last updated