我只是做與清潔的Xcode項目啓動整個過程的錯誤。通常我簡單地創建RN應用程序,彈出然後翻譯成cocoapods ios部分。 它主要基於RN文檔:http://facebook.github.io/react-native/docs/0.51/integration-with-existing-apps.html
所以環境:macOS Sierra,Xcode 9.2,RN 0.51。0
項目名稱:MyApp的
準備
- 創建新的Xcode項目,從 '單一視圖應用' 模板,產品名稱爲 「MyApp的」 語言Objective-C的
- 運行,看它工作
cd MyApp
,mkdir ios
,mv MyApp* ios
(移動所有iOS相關的文件到iOS子文件夾)
周
安裝NPM的依賴
在根文件夾中創建package.json
的你的項目(非常基本的)
{
"name": "MyApp",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start"
},
"dependencies": {
"react": "16.0.0",
"react-native": "0.51.0"
},
"devDependencies": {
"babel-jest": "22.0.4",
"babel-preset-react-native": "4.0.0"
}
}
運行npm install
Seup的CocoaPods
cd ios
pod init
(生成Podfile)
- 聲明反應在MyApp的在Podfile依賴靶向
pod 'React', :path => '../node_modules/react-native', :subspecs => [
'Core',
'CxxBridge',
'RCTAnimation',
'RCTBlob',
'RCTText',
'RCTNetwork',
'RCTWebSocket',
'RCTImage',
'RCTLinkingIOS',
'DevSupport',
]
pod 'yoga', :path => '../node_modules/react-native/ReactCommon/yoga'
pod 'DoubleConversion', :podspec => '../node_modules/react-native/third-party-podspecs/DoubleConversion.podspec'
pod 'GLog', :podspec => '../node_modules/react-native/third-party-podspecs/GLog.podspec'
pod 'Folly', :podspec => '../node_modules/react-native/third-party-podspecs/Folly.podspec'
可以添加/刪除陣營subspecs以包括/刪除RN的功能,這是很難處理,因爲RN組件沒有完全獨立的。
pod install
(莢整合到項目中,將創建MyApp.xcworkspace,它應該被用來編譯應用程序)
open MyApp.xcworkspace
,建立&運行,應用程序仍然應該工作
嵌入RN根查看
替換你在這個片段中AppDelegate.m:
#import "AppDelegate.h"
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
#if RCT_DEV
#import <React/RCTDevLoadingView.h>
#endif
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
RCTBundleURLProvider* provider = [RCTBundleURLProvider sharedSettings];
NSURL* jsCodeLocation = [provider jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
RCTBridge *bridge = [[RCTBridge alloc] initWithBundleURL:jsCodeLocation moduleProvider:nil launchOptions:launchOptions];
#if RCT_DEV
[bridge moduleForClass:[RCTDevLoadingView class]];
#endif
RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge moduleName:@"MyApp" initialProperties:@{}];
rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIViewController *rootViewController = [UIViewController new];
rootViewController.view = rootView;
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];
return YES;
}
@end
- 添加ATS例外的Info.plist(或MyApp的將是無法連接到以http打包服務器)
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>localhost</key>
<dict>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
- 編譯&在模擬器中運行,你應該看到紅色的屏幕,消息「沒有包的URL存在。「(這是因爲沒有打包服務器運行&沒有編譯jsbundle存在)
JavaScript部分
與此代碼(這是由RN模板)創建MyApp/index.js
:
import { AppRegistry } from 'react-native';
import App from './App';
AppRegistry.registerComponent('MyApp',() => App);
創建MyApp/App.js
:
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
export default class App extends React.Component {
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome to React Native!
</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
});
- 從根項目文件夾開始打包
npm start
在Xcode(MyApp
)
- 運行的應用程序,你應該看到加載指示燈,然後RN渲染屏幕「歡迎反應原住民!」
打包
- 您還應該添加packager構建步驟,將編譯後的js嵌入到應用程序包
main.jsbundle
中,以便可以在沒有包裝器開發服務器的情況下運行它。
添加腳本步驟MyApp的目標構建階段與此內容:
export NODE_BINARY=node
../node_modules/react-native/scripts/react-native-xcode.sh
這對我的作品的步驟。