OC
//
// OpenNativeModule.m
// OC桥文件
//
// Created by 逗逗飞 on 2019/4/25.
// Copyright © 2019 Facebook. All rights reserved.
//
#import "AppDelegate.h"
#import "AwesomeProject-Swift.h" // 如果是swift的viewController 则需要引入这个
#import <React/RCTBridgeModule.h>
#import <React/RCTLog.h>
@interface OpenNativeModule : NSObject<RCTBridgeModule>
@end
@implementation OpenNativeModule
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(openNativeVC:(NSString *)name location:(NSString *)location date:(NSDate *)date) {
RCTLogInfo(@"Pretending to create an event %@ at %@", name, location);
dispatch_async(dispatch_get_main_queue(), ^{
AppDelegate *delegate = (AppDelegate *)([UIApplication sharedApplication].delegate);
UIViewController *rootVC = delegate.window.rootViewController;
UINavigationController *nav = [[UIStoryboard storyboardWithName:@"NativeStoryboard" bundle:nil] instantiateInitialViewController];
// NativeViewController *nativeVC = [[NativeViewController alloc] init];
// UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:nativeVC];
[rootVC presentViewController:nav animated:YES completion:nil];
});
}
@end
Swift
React-native 调用
import {NativeModules} from 'react-native';
var nativeModule = NativeModules.OpenNativeModule;
export default class ProfileScreen extends Component {
nativeModule.openNativeVC('Birthday Party', '4 Privet Drive, Surrey');
}
Native-RN-Native
- 1.修改Appledelegate.m
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "AppDelegate.h"
#import <React/RCTBridge.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
#import <UserNotifications/UserNotifications.h>
//友盟推送
#import <UMCommon/UMCommon.h>
#import <UMPush/UMessage.h>
//RN事件监听
#import "EventEmitterManager.h"
//存储deviceToken
#import "AwesomeProject-Swift.h"
#import <React/RCTLinkingManager.h>
// 原生跳转RN
@interface AppDelegate () <UNUserNotificationCenterDelegate>
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//rn 项目创建,默认的的
RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
moduleName:@"AwesomeProject"
initialProperties:nil];
rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIViewController *rootViewController = [UIViewController new];
rootViewController.view = rootView;
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];
// 如果需要原生跳转rn的话 则需要改成这样
// self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
// OrgOneViewController *rootViewController = [[OrgOneViewController alloc] init];
// UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:rootViewController];
// self.window.rootViewController = nav;
// [self.window makeKeyWindow];
//友盟推送设置
[UMConfigure setEncryptEnabled:YES];
[UMConfigure setLogEnabled:YES];
[UMConfigure initWithAppkey:[NSString stringWithUTF8String:"5bebdb52b465f59b1500024a"] channel:@"App Store"];
[UNUserNotificationCenter currentNotificationCenter].delegate = self;
UMessageRegisterEntity * entity = [[UMessageRegisterEntity alloc] init];
entity.types = UMessageAuthorizationOptionBadge | UMessageAuthorizationOptionAlert | UMessageAuthorizationOptionSound;
[UMessage registerForRemoteNotificationsWithLaunchOptions:launchOptions Entity:entity completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
//用户选择了接受Push消息
} else {
//用户拒绝Push消息
}
}];
[[UIApplication sharedApplication] registerForRemoteNotifications];
return YES;
}
- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
#if DEBUG
return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
#else
return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
#endif
}
#pragma mark - 获取token
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
[UMessage registerDeviceToken:deviceToken];
NSString *str = [[deviceToken description] stringByReplacingOccurrencesOfString:@"<" withString:@""];
str = [str stringByReplacingOccurrencesOfString:@">" withString:@""];
str = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
//拿到DeviceToken,存储到用户偏好设置
DeviceTokenManager *deviceTokenManager = [[DeviceTokenManager alloc] init];
[deviceTokenManager saveDeviceTokenWithDevice_token:str];
}
//iOS10以下使用这两个方法接收通知
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
[UMessage setAutoAlert:NO];
if([[[UIDevice currentDevice] systemVersion]intValue] < 10){
[UMessage didReceiveRemoteNotification:userInfo];
}
completionHandler(UIBackgroundFetchResultNewData);
}
#pragma mark - iOS10新增:处理前台收到通知的代理方法
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
NSDictionary * userInfo = notification.request.content.userInfo;
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[UMessage setAutoAlert:NO];
//应用处于前台时的远程推送接受
EventEmitterManager * emitterManager = [[EventEmitterManager alloc] init];
[emitterManager sendNoticeWithEventName:@"RemoteNotiUserInfo" Dict:userInfo];
//必须加这句代码
[UMessage didReceiveRemoteNotification:userInfo];
}else{
//应用处于前台时的本地推送接受
}
completionHandler(UNNotificationPresentationOptionSound|UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionAlert);
}
#pragma mark - iOS10新增:
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
NSDictionary * userInfo = response.notification.request.content.userInfo;
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
//应用处于后台时的远程推送接受
EventEmitterManager * emitterManager = [[EventEmitterManager alloc] init];
[emitterManager sendNoticeWithEventName:@"RemoteNotiUserInfo" Dict:userInfo];
//必须加这句代码
[UMessage didReceiveRemoteNotification:userInfo];
}else{
//应用处于后台时的本地推送接受
}
}
#pragma mark - 微信支付、分享
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
{
return [RCTLinkingManager application:application openURL:url options:options];
}
@end
//
// OrgViewController.swift
// AwesomeProject
//
// Created by 逗逗飞 on 2019/4/25.
// Copyright © 2019 Facebook. All rights reserved.
//
import UIKit
class OrgOneViewController: UIViewController {
// ui Obj
var btn : UIButton!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.yellow
navigationItem.title = "原生控制器"
btn = UIButton(frame: CGRect(x: 100, y: 100, width: 200, height: 80))
btn.setTitle("跳到RN", for: UIControl.State.normal)
btn.setTitleColor(UIColor.black, for: UIControl.State.normal)
btn.backgroundColor = UIColor.white
self.view.addSubview(btn)
btn.addTarget(self, action:#selector(btn_click), for: .touchUpInside)
}
@objc func btn_click() {
let rnViewController = RNOneViewController()
self.navigationController!.pushViewController(rnViewController, animated: true)
}
}
- rn的视图控制器
//
// RNOneViewController.swift
// RN控制器
//
// Created by 逗逗飞 on 2019/4/25.
// Copyright © 2019 Facebook. All rights reserved.
//
import UIKit
class RNOneViewController: UIViewController,RCTBridgeDelegate {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.red
let bridge = RCTBridge.init(delegate: self, launchOptions: nil)
let rootView = RCTRootView(bridge: bridge, moduleName: "AwesomeProject", initialProperties: nil)!
rootView.backgroundColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
self.view = rootView
}
// rNBridge
func sourceURL(for bridge: RCTBridge!) -> URL! {
#if DEBUG
return RCTBundleURLProvider.sharedSettings()!.jsBundleURL(forBundleRoot: "index", fallbackResource: nil)
#else
return Bundle.main.url(forResource: "main", withExtension: "jsbundle")
#endif
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
}
}
网友评论