博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS项目集成react native就这几步
阅读量:7005 次
发布时间:2019-06-27

本文共 2891 字,大约阅读时间需要 9 分钟。

七牛的图片在这里居然显示不了...

没事写个记录, 也算是温故而知新吧. 如下效果:

react-native-cli和npm安装不在本文范围.以下react native简称RN.

现在RN也越来越方便了,集成进原生项目也非常简单.就这下面几个步骤.

我们有一个iOS原生项目叫 Helloworld, 是使用Cocoapods做包管理, RN也是Cocoapods接入. 目录结构如下:

1.创建RN

  1. 在项目目录下创建reactnative文件夹, 在这个文件夹里创建两个文件index.ios.jspackage.json;

  2. package.json添加以下内容

    {    "name": "Helloworld",    "version": "0.0.1",    "private": true,    "scripts": {      "start": "node node_modules/react-native/local-cli/cli.js start"    },    "dependencies": {        "react": "15.3.1",        "react-native": "^0.33.0"    },    "devDependencies": {      "babel-plugin-transform-decorators-legacy": "^1.3.4"    }}
  3. index.ios.js添加以下内容

    import React, { Component } from 'react';import {  AppRegistry,  StyleSheet,  Text,  View,} from 'react-native';class Main extends Component {  render() {    return (      
    Hello World
    ); }}AppRegistry.registerComponent('Helloworld', () => Main);
  4. reactnative文件夹下用终端命令: npm install

2.接RN入项目

  1. 打开项目中的Podfile文件, 添加以下内容:

    pod 'React', :path => './reactnative/node_modules/react-native', :subspecs => ['Core','ART','RCTActionSheet','RCTAdSupport','RCTGeolocation','RCTImage','RCTNetwork','RCTPushNotification','RCTSettings','RCTText','RCTVibration','RCTWebSocket','RCTLinkingIOS',# Add any other subspecs you want to use in your project  ]

    注意 path => 后面的路径是否正确.

  2. 在项目下执行命令: pod install

  3. RN是以view的形式在项目中使用的, 所以再项目中新建一个控制器RNMainViewControllerRNView

    RNMainViewController.m

    #import "RNMainViewController.h"#import "ViewController.h"#import "RNView.h"@interface RNMainViewController ()@end@implementation RNMainViewController- (void)viewDidLoad {    [super viewDidLoad];        self.title = @"RN模块首页";        RNView * rnView = [[RNView alloc] initWithFrame:self.view.bounds];    self.view = rnView;}@end

    RNView.m

    #import "RNView.h"#import "RCTBundleURLProvider.h"#import "RCTRootView.h"@implementation RNView- (instancetype)initWithFrame:(CGRect)frame {        if (self = [super initWithFrame:frame]) {#if TARGET_IPHONE_SIMULATOR        [[RCTBundleURLProvider sharedSettings] setJsLocation:@"localhost"];#else        [[RCTBundleURLProvider sharedSettings] setDefaults];#endif        NSURL *jsCodeLocation;        jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];                RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation                                                            moduleName:@"Helloworld"                                                     initialProperties:nil                                                         launchOptions:nil];                [self addSubview:rootView];        rootView.frame = self.bounds;    }    return self;}@end
  4. 在项目info.plist加上 App Transport Security Settings:

  5. Build Phases中添加一个Run Script, 注意其中的路径正确:

  6. reactnative文件夹下用终端命令: npm start

  7. 运行项目,不出意外就大功告成了.

最后项目目录结构:

转载地址:http://ivytl.baihongyu.com/

你可能感兴趣的文章
一个完整和全面的云服务器安全方案是什么样子的?
查看>>
不小心删除win7账户怎么办?
查看>>
【硬创邦】跟hoowa学做智能路由(十二):网络音箱之声卡驱动
查看>>
生死狙击:阻止恶意SSL通信六要点
查看>>
再见Opera 离别便是永久
查看>>
关于物联网消费者和商户应知道的5件事
查看>>
用户体验再掀高潮,阿里云域名领跑用户体验
查看>>
《高性能Linux服务器构建实战》——3.3节Memcached的管理与性能监控
查看>>
服务器市场混乱,信息安全是关键
查看>>
CFCA入根苹果 信息安全产品国产化的又一重大突破!
查看>>
美德纷纷发生恶性断网事件 网络安全需要全球共治
查看>>
UBER如何使用大数据
查看>>
Google过去一年被要求删除十亿多条“盗版”搜索结果
查看>>
2016(第十一届)中国化学品储运会议
查看>>
《Java安全编码标准》一1.6 序列化
查看>>
为何思科和VMware的SDN产品总是无法很好地兼容?
查看>>
《机器人自动化:建模、仿真与控制》一一导读
查看>>
HPE和三星联手推进NFV 帮助运营商向云迈进
查看>>
宜昌进入全国智慧城市建设50强 率先被确定为试点
查看>>
《Cocos2D权威指南》——3.7 Cocos2D中的单例
查看>>