1:在项目跟目录下创建一个ReactComponent文件夹。目录结构如下:

2: 在ReactComponent文件夹下新建一个 package.json 文件

{
"name": "yy", // 项目名称
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest"
},
"dependencies": {
"react": "16.8.3",
"react-native": "0.59.9"
},
"devDependencies": {
"@babel/core": "7.4.5",
"@babel/runtime": "7.4.5",
"babel-jest": "24.8.0",
"jest": "24.8.0",
"metro-react-native-babel-preset": "0.54.1",
"react-test-renderer": "16.8.3"
},
"jest": {
"preset": "react-native"
}
}

3:到ReactComponent目录下,初始化package.json文件,会生成需要的 node_modules 目录结构如下

// 到对应的目录中
cd xxx/ReactComponent
// 初始化 package.json
npm install

4:在ReactComponent文件夹下生成 index.ios.js 文件

/*
*
* Sample React Native App
* jff
*
*/ import React, {Component} from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
} from 'react-native'; class NativeRnApp extends Component {
render(){
return (
<View style={styles.container}>
<Text style={styles.welcome}> Welcome react-native </Text>
<Text style={styles.instructions}> To get started, edit index.ios.js </Text>
<Text style={styles.instructions}> Press Cmd+R to reload,{'\n'} Cmd+D or shake for dev menu </Text>
</View>
);
}
} const styles = StyleSheet.create({
container:{
flex:1,
justifyContent:'center',
alignItems:'center',
backgroundColor:'#F5FCFF',
},
welcome:{
fontSize:20,
textAlign:'center',
margin:10,
},
instructions:{
textAlign:'center',
marginBottom:10,
color:'#333333'
}
})
// 注册组件
AppRegistry.registerComponent('rn', ()=>NativeRnApp);

5:常见Podfile 文件导入yoga,react框架,Podfile文件内容

# 如果导入react-navigation 和 react-native-gesture-handler 需要在Podfile中导入RNGestureHandler 否则会报 null is not an object (evaluating '_rngeasturhandlemodule.default.direction'
# RCTLinkingIOS 用于链接原生module 否则会报 Native Moudle is NULL
# RCTAnimation 导入动画,否则会有警告
# DevSupport 用于调试支持
platform :ios, '9.0' target 'iOSRN' do
    pod 'yoga', :path => './ReactComponent/node_modules/react-native/ReactCommon/yoga'
    pod 'React', :path => './ReactComponent/node_modules/react-native', :subspecs => [
        'Core',
        'DevSupport',
        'CxxBridge',
        'RCTActionSheet',
        'RCTGeolocation',
        'RCTImage',
        'RCTNetwork',
        'RCTPushNotification',
        'RCTSettings',
        'RCTText',
        'RCTVibration',
        'RCTWebSocket',
        'RCTLinkingIOS',
        'RCTAnimation'
    ]
    pod 'RNGestureHandler', :path => './ReactComponent/node_modules/react-native-gesture-handler'
    pod 'DoubleConversion', :podspec => './ReactComponent/node_modules/react-native/third-party-podspecs/DoubleConversion.podspec'
    pod 'glog', :podspec => './ReactComponent/node_modules/react-native/third-party-podspecs/glog.podspec'
    pod 'Folly', :podspec => './ReactComponent/node_modules/react-native/third-party-podspecs/Folly.podspec'
end

在第一次 pod install 的时候,要打开VPN不然下载不下来 ‘boost-for-react-native’会报错:RPC failed; curl 56 LibreSSL SSL_read: SSL_ERROR_SYSCALL, errno 60 fatal:

如果报‘ios folly/portability.h' file not found’ 需要导入上面的‘:podspec’对应的依赖库,同时为了正常的链接好需要有 'CxxBridge'

6: 在pod install 成功之后,打开 xx.xcworkspace, 之后 file -> Workspace Settings -> 更改“new Build System(Default)”为“legacy Build System”,如下

在info.plist文件中添加"App Transport Security Settings"

<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>

7:生成需要使用的RN文件。让ReactViewController继承自UIVIewController

//
// ReactViewController.h
// yy
//
// Created by jisa on 2019/6/24.
// Copyright © 2019 jff. All rights reserved.
// #import <UIKit/UIKit.h> NS_ASSUME_NONNULL_BEGIN @interface ReactViewController : UIViewController @end NS_ASSUME_NONNULL_END
//
// ReactViewController.m
// yy
//
// Created by jisa on 2019/6/24.
// Copyright © 2019 jff. All rights reserved.
// #import "ReactViewController.h"
// 导入需要的组件
#import <React/RCTRootView.h> @interface ReactViewController () @end @implementation ReactViewController - (void)viewDidLoad {
[super viewDidLoad]; // Do any additional setup after loading the view. local
NSString * strUrl = @"http://localhost:8081/index.ios.bundle?platform=ios&dev=true";
NSURL * jsCodeLocation = [NSURL URLWithString:strUrl]; RCTRootView * rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
moduleName:@"rn"
initialProperties:nil
launchOptions:nil];
self.view = rootView;
}
@end

在进行真机调试是要把localhost换成电脑上的IP地址,同事要保证电脑和手机处于同一网段下。

在iOS项目中嵌入RN代码的更多相关文章

  1. 在Flex (Flash)中嵌入HTML 代码或页面—Flex IFrame

    在flex组件中嵌入html代码,可以利用flex iframe.这个在很多时候会用到的,有时候flex必须得这样做,如果你不这样做还真不行-- flex而且可以和html进行JavaScript交互 ...

  2. iOS-C文件添加到iOS项目中,运行报错

    iOS-C文件添加到iOS项目中,运行报错 问题: 往项目中添加一个空的c文件, 编译运行; 出现2,30个编译错误. 原因: 由于在项目中添加了Pch文件,在文件中所有代码还没有开始运行之前, pc ...

  3. iOS项目中常见的文件

    html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,bi ...

  4. 在Xamarin.iOS项目中使用预设数据库

    在Xamarin.iOS项目中使用预设数据库 当开发者准备好一个预设数据库文件后,就可以将这个数据库文件添加到创建的项目中了.本节将分别在Xamarin.iOS和Xamarin.Android项目中使 ...

  5. 在Flex (Flash)中嵌入HTML 代码或页面—Flex IFrame

    在flex组件中嵌入html代码,可以利用flex iframe.这个在很多时候会用到的,有时候flex必须得这样做,如果你不这样做还真不行…… flex而且可以和html进行JavaScript交互 ...

  6. 给iOS项目中添加图片,并通过UIImageView引用和显示该UIImage图片

    [问题] 关于iOS/iPhone中的文件选择对话框,用于用户去选择图片等文件 过程中,问题转换为,需要给当前iOS项目中,添加一个图片. 类似于Windows开发中的资源文件,其中图片文件属于资源的 ...

  7. linux内核分析作业4:使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用

    系统调用:库函数封装了系统调用,通过库函数和系统调用打交道 用户态:低级别执行状态,代码的掌控范围会受到限制. 内核态:高执行级别,代码可移植性特权指令,访问任意物理地址 为什么划分级别:如果全部特权 ...

  8. ios项目中引用其他项目复习

    ios项目中引用其他开源项目,今天再次复习了,记个备注. 1. 将开源项目的.xcodeproj拖入项目frameworks 2. Build Phases下 Links Binary With Li ...

  9. JSP中嵌入java代码方式以及指令

    JSP中嵌入java代码的三种方式: (1)声明变量或方法 :  <%! 声明; %> :慎重使用,因为此方法定义的是全局变量 (2)java片段(scriptlet):  <% j ...

随机推荐

  1. 箭头函数的 this

    在非严格模式下,匿名函数和定时器中的 this 由于没有默认的宿主对象,因此指向 window: 而在严格模式下,匿名函数和定时器中的 this 由于没有默认的宿主对象,因此为 undefined. ...

  2. mySql学习笔记:比sql server书写要简单

    在学mySql.总的感觉,mySql与Sql Server差不多,语法都很象,但mySql也许是吸取了SQL SERVER的一些经验,SQL语句书写起来更加简单. 比如说,设置主键.索引,SQL SE ...

  3. 【bzoj1002】 [FJOI2007]轮状病毒DP

    递推+环状特殊处理+高精度   #include<algorithm> #include<iostream> #include<cstdlib> #include& ...

  4. elasticsearch 布尔过滤器 游标查询 Scroll

    组合过滤器 | Elasticsearch: 权威指南 | Elastic https://www.elastic.co/guide/cn/elasticsearch/guide/current/co ...

  5. bzoj1143: [CTSC2008]祭祀river && bzoj27182718: [Violet 4]毕业旅行

    其实我至今不懂为啥强联通缩点判入度会错... 然后这个求的和之前那道组合数学一样,就是最长反链=最小链覆盖=最大独立集. #include<cstdio> #include<iost ...

  6. 不仅开源,而且对企业应用完全免费!ExtAspNet弃用GPL v2,拥抱Apache License 2.0(转)

    不仅开源,而且对企业应用完全免费!ExtAspNet弃用GPL v2,拥抱Apache License 2.0(转) 提出问题 ExtAspNet开源以来,一直坚持开源免费的原则,但是其GPL v2的 ...

  7. hdu 4587(枚举+割顶)

    TWO NODES Time Limit: 24000/12000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total ...

  8. ubuntu系统快捷键设置

    1.打开'系统设置' 2.点击键盘 3.选择快捷键,查看和修改对应的快捷键.

  9. 当Table中td内容为空时,让它显示边框的办法

    1 在 table的css里面加 border-collapse:collapse; 在 td 的css里面加      empty-cells:show; border-collapse设置或检索表 ...

  10. 数据库登陆失败原因: 未与信任 SQL Server 连接相关联

    解决方案:用户 'sa' 登录失败.原因: 未与信任 SQL Server 连接相关联. 问题简述: 用户 'sa' 登录失败.原因: 未与信任 SQL Server 连接相关联. 说明: 执行当前 ...