2017-04-25 57 views
5

我已經通過一些帖子挖掘了一些試圖設置原生ios庫使用cocoapods的原生項目,但是我不可避免地在我的AppDelegate.m文件中#import <React/RCTBundleURLProvider.h>聲明中丟失了一個文件。在ios中使用React Native(0.43.4)的cocoapods的正確方法是什麼?

什麼是反應本地使用可可豆莢的正確方法?在這篇文章的時候,我現在使用的版本是0.43.4,我使用的是Xcode 8.2.1。

這是我的過程中,好奇的,我可能是想錯了:

1)在終端中,我創建一個使用一個新的項目react-native init TestProject

2)我在該項目中的IOS目錄運行pod init

3)我在podFile添加依賴關係和運行pod install

4)安裝過程成功,但我看到我的目標override the 'OTHER_LDFLAGS警告「一nd建議我在Xcode的鏈接器標誌中使用$(inherit)

5)因此,基於this SO post我添加$(inherited)項目> TestProject> BuildSettings>連接>其它鏈接器標記這是其他空。我也檢查並看到$(inherited)已經出現在目標> TestProject>構建設置>鏈接>其他鏈接器標記以及預處理器宏。

6)在這一點上我看到反應/ RCTBundleURLProvider.h文件不是基於this SO post我嘗試刪除node modules目錄,並早在終端運行npm installAppDelegate.m

7)發現上的錯誤#import <React/RCTBundleURLProvider.h>聲明並在完成時'react-native upgrade'。當它詢問我是否要替換AppDelegate.mproject.pbxproj文件時,我說是。

8)早在Xcode的清潔我的構建,但仍然有從第6步導入<React/RCTBundleURLProvider.h>

回答

2

我只是做與清潔的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 MyAppmkdir iosmv 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 

這對我的作品的步驟。

相關問題