React Native 嵌入到iOS原生应用
如果你正准备从头开始制作一个新的应用,那么React Native会是个非常好的选择。但如果你只想给现有的原生应用中添加一两个视图或是业务流程,React Native也同样不在话下。只需简单几步,你就可以给原有应用加上新的基于React Native的特性、画面和视图等。 把React Native组件植入到iOS应用中有如下几个主要步骤:
1. 创建一个原生应用这个就不多说。 2. 添加依赖包
2.1 package.json我这里的做法是在项目的根目录下创建一个专门存放 然后在这个文件夹下创建一个 在 {
"name": "NativeRN","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" } }
解释一下:
本文无法在这里列出所有 2.2 安装依赖包使用npm(node包管理器,Node package manager)来安装React和React Native模块。这些模块会被安装到项目根目录下的node_modules/目录中。 在包含有package.json文件的目录(一般也就是项目根目录,我这里因为创建了RNComponent文件夹,所以是在这个文件夹目录下执行这个命令)中运行下列命令来安装: $ npm install
运行完成后会出现 3. React Native框架React Native框架整体是作为node模块安装到项目中的。下一步我们需要在CocoaPods的Podfile中指定我们所需要使用的组件。 3.1 Subspecs在你开始把 可用的 3.2 Podfile在 创建podfile在这里不在多说,相信只要用过cocoapods的朋友都知道。 podfile创建完成之后,在文件里添加一下内容: # target的名字一般与你的项目名字相同
target 'NativeRN' do
# 'node_modules'目录一般位于根目录中
# 但是如果你的结构不同,那你就要根据实际路径修改下面的`:path`
pod 'React',:path => './RNComponent/node_modules/react-native',:subspecs => [
'Core','RCTText','RCTNetwork','RCTWebSocket',# 这个模块是用于调试功能的
# 在这里继续添加你所需要的模块
]
# 如果你的RN版本 >= 0.42.0,请加入下面这行
pod "Yoga",:path => "./RNComponent/node_modules/react-native/ReactCommon/yoga"
end
然后执行下面的??命令,开始安装React Native的pod包。 $ pod install
4. 代码集成4.1 index.ios.js首先创建一个空的index.ios.js文件。一般来说我们把它放置在项目根目录下。就像??: index.ios.js是React Native应用在iOS上的入口文件。而且它是不可或缺的!它可以是个很简单的文件,简单到可以只包含一行require/import导入语句。 # 在项目根目录执行以下命令创建文件:
$ touch index.ios.js
4.2 添加你自己的React Native代码import React,{ Component } from 'react';
import {
AppRegistry,StyleSheet,Text,View
} from 'react-native';
export default class NativeRN 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('NativeRN',() => NativeRN);
4.3 集成到原生项目中我这里先创建了一个ViewController,??这样: 然后导入 - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString * strUrl = @"http://localhost:8081/index.ios.bundle?platform=ios&dev=true";
NSURL * jsCodeLocation = [NSURL URLWithString:strUrl];
RCTRootView * rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
moduleName:@"NativeRN"
initialProperties:nil
launchOptions:nil];
self.view = rootView;
}
4.4 配置info.plist还需要在info.plist文件中配置一下: <key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>localhost</key>
<dict>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
配置后的效果: 5. 运行项目在运行项目前,先在react native文件夹目录下,启动开发服务器。也就是在本文中的RNComponent目录下,启动命令行: $ npm start
运行项目,看到效果: 博客地址 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |