Skip to content

duncan60/RN-integration-with-existing-app

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RN-integration-with-existing-app

嘗試將 React Native 與現有 Java/Objective-C Native APP 做配合溝通的簡單範例。

Build Setup

# install react-native dependencies, cd rn/
yarn

# run react-native
yarn run start


# Android Studio open Android project and run Simulators

# ios install pods, Xcode open project and run Simulators

Android 要點:

如何引入 RN Modules 及加入 Activity

Android & RN 溝通與事件增聽

  • 建立繼承 ReactContextBaseJavaModule 的 class,並加入方法:
# /android/app/src/main/java/com/example/duncan_du/android/ReactEventManager.java
public class ReactEventManager extends ReactContextBaseJavaModule {

    public ReactEventManager(ReactApplicationContext reactContext) {
        super(reactContext);
    }

    @Override
    public String getName() {
        return "ReactEventManager";
    }

    @ReactMethod
    public void showNativeAlert(String message) {
        Toast.makeText(getReactApplicationContext(), message, Toast.LENGTH_LONG).show();
        // send event to RN
        this.getReactApplicationContext()
                .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                .emit("eventToRN","connecting RN.....");
    }
}
  • 建立繼承 ReactPackage 的 class,並將剛建立的 ReactEventManager 加入:
# /android/app/src/main/java/com/example/duncan_du/android/AnExampleReactPackage.java
public class AnExampleReactPackage implements ReactPackage {

    @Override
    public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
        return Collections.emptyList();
    }

    @Override
    public List<NativeModule> createNativeModules(
            ReactApplicationContext reactContext) {
        List<NativeModule> modules = new ArrayList<>();

        modules.add(new ReactEventManager(reactContext));

        return modules;
    }
}
  • MainApplication 中加入 ReactPackage 的引用:
# /android/app/src/main/java/com/example/duncan_du/android/MainApplication.java
@Override
        protected List<ReactPackage> getPackages() {
            return Arrays.<ReactPackage>asList(
                    new MainReactPackage(),
                    new AnExampleReactPackage()
            );
        }

iOS 要點

如何引入 RN Modules

加入到 ViewController

# /ios/ios/ViewController.m

- (IBAction)addRNViewPressed:(id)sender {
    NSURL *jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.bundle?platform=ios"];

    RCTRootView *rootView =
    [[RCTRootView alloc] initWithBundleURL: jsCodeLocation
                                moduleName: @"RNExistingApp"
                            initialProperties:
        @{
        @"message" : @"Hello, I'm come from iOS"
        }
                                launchOptions: nil];
    UIViewController *vc = [[UIViewController alloc] init];
    vc.view = rootView;
    [self presentViewController:vc animated:YES completion:nil];
}

iOS & RN 溝通與事件增聽

  • 建立 .h & .m 檔案,加入發法與曾聽事件
# /ios/ios/ReactEventManager.h

#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>

@interface ReactEventManager : RCTEventEmitter <RCTBridgeModule>

@end


# /ios/ios/ReactEventManager.m

#import "ReactEventManager.h"
#import <React/RCTLog.h>

@implementation ReactEventManager

RCT_EXPORT_MODULE();

- (NSArray<NSString *> *)supportedEvents
{
    return @[@"eventToRN"];
}

RCT_EXPORT_METHOD(showNativeAlert:(NSString *)message)
{
    RCTLogInfo(@"from RN Message %@", message);

    [self sendEventWithName:@"eventToRN" body:@"test RN integration with existing app"];

    UIViewController *presentingController = RCTPresentedViewController();

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Alert" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];

    [alertController addAction:ok];

    [presentingController presentViewController:alertController animated:YES completion: nil];
}


@end

RN Component

  • 使用 React-Native 的 NativeModules & NativeEventEmitter 和 Native 做溝通:
# rn/App.js

import {
  NativeModules,
  NativeEventEmitter,
} from 'react-native';
const { ReactEventManager } = NativeModules;

# onPress Method
ReactEventManager.show('From React Native: Awesome!');

# Event AddListener
componentDidMount() {
    const ManagerEvent = new NativeEventEmitter(ReactEventManager);
    this._subscription = ManagerEvent.addListener('eventToRN', (info) => {
      this.setState({ nativeEventInfo: info });
    });
  }
componentWillUnmount() {
  this._subscription.remove();
}

學習資源

Android

iOS

其他

About

React Native integration with existing android/ ios app

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published