iOS原生項目中集成React Native

親自操作了一遍,沒有大問題。

我在 pod install 的時候,一直 installling ,永遠安裝不完 ,最終發現,我把 翻牆軟件退出 去就好了


原文地址:http://blog.csdn.net/l863784757/article/details/50592341


1.本文的前提條件是,電腦上已經安裝了CocoaPods,React Native相關環境。


2.使用Xcode新建一個工程。EmbedRNMeituan


[圖1]


3.使用CocoaPods安裝React Native


在工程目錄下新建Podfile文件,並配置需要使用的第三方庫

[objc] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. pod 'React''0.13.0-rc'  
  2. pod "React/RCTText"  
  3. pod "React/RCTActionSheet"  
  4. pod "React/RCTGeolocation"  
  5. pod "React/RCTImage"  
  6. pod "React/RCTLinkingIOS"  
  7. pod "React/RCTNetwork"  
  8. pod "React/RCTSettings"  
  9. pod "React/RCTVibration"  
  10. pod "React/RCTWebSocket"  
  11. platform :ios, '7.0'  

注:如果你需要在React Native中使用<Text>,就需要添加   pod"React/RCTText”,否則不能用

然後安裝:  pod install

這一步會比較慢

安裝完成後,添加 Search Paths
輸入$(PODS_ROOT),選擇recursive


【圖2】


編譯一下,會報一個錯,提示路徑太長


Argument list too long: recursive header expansion failed at /Users/***/EmbedRNMeituan/Pods/React/node_modules/jest-cli/node_modules/istanbul/node_modules/escodegen/node_modules/estraverse.

這裏需要修改剛纔的設置,將 $(PODS_ROOT) 改成 $(PODS_ROOT)/React/React

再次編譯,成功。

4.在工程目錄下新建Components文件夾,和index.ios.js文件


【圖3】


並在index.ios.js文件裏粘貼一下代碼:

[javascript] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. 'use strict';  
  2.   
  3. var React = require('react-native');  
  4. var {  
  5.   AppRegistry,  
  6.   StyleSheet,  
  7.   Text,  
  8.   View,  
  9. } = React;  
  10.   
  11. var EmbedRNMeituan = React.createClass({  
  12.   render: function() {  
  13.     return (  
  14.       <View style={styles.container}>  
  15.         <Text style={styles.welcome}>  
  16.           Welcome to React Native!  
  17.         </Text>  
  18.         <Text style={styles.instructions}>  
  19.           To get started, edit index.android.js  
  20.         </Text>  
  21.         <Text style={styles.instructions}>  
  22.           Shake or press menu button for dev menu  
  23.         </Text>  
  24.       </View>  
  25.     );  
  26.   }  
  27. });  
  28.   
  29. var styles = StyleSheet.create({  
  30.   container: {  
  31.     flex: 1,  
  32.     justifyContent: 'center',  
  33.     alignItems: 'center',  
  34.     backgroundColor: '#F5FCFF',  
  35.   },  
  36.   welcome: {  
  37.     fontSize: 20,  
  38.     textAlign: 'center',  
  39.     margin: 10,  
  40.   },  
  41.   instructions: {  
  42.     textAlign: 'center',  
  43.     color: '#333333',  
  44.     marginBottom: 5,  
  45.   },  
  46. });  
  47.   
  48. AppRegistry.registerComponent('EmbedRNMeituan', () => EmbedRNMeituan);  

以上,React Native部分已經弄完。下面開始原生部分。


5.新建顯示React Native的UIView。

用來加載顯示React Native的容器是 RCTRootView,它是繼承自UIView。

在ViewController.m中

[objc] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. #import "RCTRootView.h"  
  2.   
  3. - (void)viewDidLoad {  
  4.     [super viewDidLoad];  
  5.     // Do any additional setup after loading the view, typically from a nib.  
  6.      
  7.     [self initRNView];  
  8. }  
  9.   
  10. -(void)initRNView {  
  11.     NSURL *jsCodeLocation;  
  12.     jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.bundle?platform=ios&dev=true"];     
  13.     RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation  
  14.                                                         moduleName:@"EmbedRNMeituan"  
  15.                                                  initialProperties:nil  
  16.                                                      launchOptions:nil];  
  17. //注意,這裏是 @"EmbedRNMeituan"  
  18.           rootView.frame = CGRectMake(064300300);  
  19.     [self.view addSubview:rootView];  
  20. }  


6.運行
 此時如果運行的話,會出現下面的情況


【圖4】


提示找不到服務器,以及數據傳輸的安全問題。


6.1允許http請求
打開info.plist文件,添加


【圖5】

App Transport Security Settings    -》 Dictionary
          Allow Arbitrary Loads          -》 YES

6.2啓動服務器
工程目錄下,終端輸入:

[html] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. (cd Pods/React; npm run start)  


6.3編寫腳本文件run.sh


[html] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. vi run.sh  

輸入

[html] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. #! /bin/bash  
  2. (cd Pods/React; npm run start)  


然後給run.sh添加可執行權限:chmod +x run.sh

之後開啓服務器時,只需要在終端輸入命令: ./run.sh

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章