React Native集成到IOS原生项目
这里默认电脑上已经安装了cocoapods和React-Native,如果没有RN开发环境,可以点击这里按照步骤配置。 0、新建项目首先,先使用xcode新建一个项目,然后在项目的根目录下新建一个文件夹,用于存放RN的组件库还有其他一些文件。这里假设新建一个名为 1、新建RN配置文件准备妥当以后,我们在 内容为: {
"name": "RNApp","version": "0.0.1","private": true,"scripts": { "start": "node node_modules/react-native/local-cli/cli.js start" },"dependencies": { "react": "16.0.0-alpha.6","react-native": "0.44.0","react-native-deprecated-custom-components": "^0.1.0","react-native-tab-navigator": "^0.3.3" } }
上述代码里的配置信息已经足够现在集成项目使用,如果需要了解其它配置信息的话,可以参考下方:
文章结尾放了一些 2、安装RN编辑好 当出现下图,就说明安装成功,并且目录里多了一个 3、设置podfile文件安装完毕以后,我们切换到xcode项目根目录中,然后再终端输入 4、使用pod管理依赖组件生成 pod 'Yoga',:path => './RNComponent/node_modules/react-native/ReactCommon/yoga' pod 'React',:path => ‘./RNComponent/node_modules/react-native/',:subspecs => [ 'Core','ART','RCTActionSheet','RCTAdSupport','RCTGeolocation','RCTImage','RCTNetwork','RCTPushNotification','RCTSettings','RCTText','RCTVibration','RCTWebSocket','RCTLinkingIOS'] 这里把所有组件全部做了依赖管理,所以在开发中,就需要根据实际需求来做对应的依赖。如果路径没有指定正确,就会报错,具体点击这里参考。 编辑好以后,直接在终端输入 此时目录结构为: 5、新建RN项目js文件入口接下来需要在 /** * Sample React Native App * https://github.com/facebook/react-native * @flow */
import React,{ Component } from 'react';
import {
AppRegistry,//注册
StyleSheet,//样式
Text,//文本组件
View,//视图组件
Image
} from 'react-native';
export default class RNApp extends Component {
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
nethanhan
</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('RNApp',() => RNApp);
6、集成测试完成所有工作以后,我们打开xcode项目,然后创建一个按钮,用于跳转到另外一个控制器,然后这个控制器直接把RN界面当成自己的view来展示。如图: RN模块的类是 然后在项目的plist文件中添加允许http连接声明,代码如下: <key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>localhost</key>
<dict>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
接下来在终端中切换到刚才的 最后运行xcode项目,点击按钮,会跳转到RN界面的控制器里,就会出现RN的界面(第一次运行会慢一些),如图: OK! 至此,我们已经成功的把RN集成到IOS原生项目中。 如果有什么疑问,随时欢迎评论! 7、package.json扩展我们在上方 {
"name": "module-name","version": "10.3.1","description": "An example module to illustrate the usage of a package.json","author": "Your Name <you.name@example.org>","contributors": [{ "name": "Foo Bar","email": "foo.bar@example.com" }],"bin": { "module-name": "./bin/module-name" },"scripts": { "test": "vows --spec --isolate","start": "node index.js","predeploy": "echo im about to deploy","postdeploy": "echo ive deployed","prepublish": "coffee --bare --compile --output lib/foo src/foo/*.coffee" },"main": "lib/foo.js","repository": { "type": "git","url": "https://github.com/nodejitsu/browsenpm.org" },"bugs": { "url": "https://github.com/nodejitsu/browsenpm.org/issues" },"keywords": [ "nodejitsu","example","browsenpm" ],"dependencies": { "primus": "*","async": "~0.8.0","express": "4.2.x","winston": "git://github.com/flatiron/winston#master","bigpipe": "bigpipe/pagelet","plates": "https://github.com/flatiron/plates/tarball/master" },"devDependencies": { "vows": "^0.7.0","assume": "<1.0.0 || >=2.3.1 <2.4.5 || >=2.5.2 <3.0.0","pre-commit": "*" },"preferGlobal": true,"publishConfig": { "registry": "https://your-private-hosted-npm.registry.nodejitsu.com" },"subdomain": "foobar","analyze": true,"license": "MIT" } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |