对于react native(一下简称RCT)很多人应该都听说过,但真的集成使用,还是没有过的,近期在为已有项目集成RCT遇上了很多坑,发现网上挺多资料,但是看着乱的很,于是自己整理了一下:
在集成之前,你的电脑必须安装:
step 1.在项目中加入Cocoapods
Cocoapods是iOS中目前公认的最便捷,合理的第三方框架管理工具,具体的网上很多相关的文章,一搜就可以了。
项目工程根目录下
pod init
pod install
step 2.安装react-native
先创建一个文件夹用来存放react-native
mkdir react
进入创建的react文件中
cd react
创建package.json
文件 记得删除注释
{
"name": "你的项目名",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start"
},
"dependencies": { // 这里填入依赖项
"react": "16.0.0", // 其实 react也是作为一个第三方库存在的
"react-native": "0.50" // 这个也是 - - 依次可以加入很多第三方库,然后使用npm install初始化,其实这里和cocoapods很相似
}
}
在react文件夹中安装react-native
npm install
创建index.ios.js
文件
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View
} from 'react-native';
export default class ModuleA extends Component {
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome to 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',
color: '#333333',
marginBottom: 5,
},
});
AppRegistry.registerComponent('ModuleA', () => ModuleA);
step 3.使用Cocoapods集成react-native
在Podfile
中添加React
pod 'React', :path => './react/node_modules/react-native', :subspecs => [
'Core',
'DevSupport',
'RCTText',
'RCTNetwork',
'RCTWebSocket',
'BatchedBridge',
]
pod 'yoga', :path => './react/node_modules/react-native/ReactCommon/yoga'
# 根据需求添加项目中需要的模块
注意:./react
/node_modules/react-native这个react是和你开始创建的存放RCT的目录是相同的。记得要添加所有你需要的依赖!举例来说, 元素如果不添加RCTText依赖就不能运行。
更新pod
pod update --no-repo-update
到这里,配置工作就基本完成了,接下来跑起来试一下
step 4.run run run
先进入开始安装RCT的目录中,开启服务
react-native start
在iOS 9以上的系统中,除非明确指明,否则应用无法通过http协议连接到localhost主机。 我们建议你在Info.plist文件中将localhost列为App Transport Security的例外。 如果不这样做,在尝试通过http连接到服务器时,会遭遇这个错误 - Could not connect to development server.
打开工程中的 Info.list 文件,添加下面配置即可:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>localhost</key>
<dict>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
做完配置,就可以开始写代码了
NSString * strUrl = @"http://localhost:8081/index.ios.bundle?platform=ios&dev=true";
NSURL * jsCodeLocation = [NSURL URLWithString:strUrl];
RCTRootView * rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation moduleName:@"ModuleA" initialProperties:nil launchOptions:nil];
[self.view addSubview:rootView];rootView.frame = CGRectMake(0, 20, 300, 300);
网友评论
我修改路径也起不到作用,请问应该怎么去解决这个问题?谢谢
其实创建rn项目分为两种做法:1.用reactnative来创建xcode的project;(使用终端) 2.用xcode创建好project,用cocoapods安装reactnative。
我现在用的二种方法,想模仿第一种成功的project里的代码。但是出现上面提到的错误。
pod 'React', '~> 0.13.0-rc'
pod 'React/Core'
pod 'React/ART'
pod 'React/RCTActionSheet'
pod 'React/RCTAdSupport'
pod 'React/RCTGeolocation'
pod 'React/RCTImage'
pod 'React/RCTNetwork'
pod 'React/RCTPushNotification'
pod 'React/RCTSettings'
pod 'React/RCTText'
pod 'React/RCTVibration'
pod 'React/RCTWebSocket'
pod 'React/RCTLinkingIOS'
Analyzing dependencies
Fetching podspec for `React` from `./ReactComponent/node_modules/react-native`
[!] Unable to satisfy the following requirements:
- `React/Core (from `./ReactComponent/node_modules/react-native`)` required by `Podfile`
Specs satisfying the `React/Core (from `./ReactComponent/node_modules/react-native`)` dependency were found, but they required a higher minimum deployment target.
[!] Your Podfile has had smart quotes sanitised. To avoid issues in the future, you should not use TextEdit for editing it. If you are not using TextEdit, you should turn off smart quotes in your editor of choice.
localhost:kxdh zhuyuhui$ pod install
Analyzing dependencies
Fetching podspec for `React` from `./node_modules/react-native`
[!] Unable to satisfy the following requirements:
- `Yoga (= 0.42.0.React)` required by `React/Core (0.42.0)`
None of your spec sources contain a spec satisfying the dependency: `Yoga (= 0.42.0.React)`.
You have either:
* out-of-date source repos which you can update with `pod repo update`.
* mistyped the name or version.
* not added the source repo that hosts the Podspec to your Podfile.
Note: as of CocoaPods 1.0, `pod repo update` does not happen on `pod install` by default.