ReactNative集成到已有工程中-IOS

如下步驟爲手動添加的方式,使用rnpm的方式請參考官方文檔。html

本文是對官方文檔的一個補充,大部份內容來源於官網文檔。node

官方參考地址: http://facebook.github.io/react-native/docs/linking-libraries-ios.html#contentreact

中文參考地址:http://reactnative.cn/docs/linking-libraries-ios.html#contentios

第一步:添加.xcodeproj文件

把須要的.xcodeproj文件,拖到你的XCode工程下(一般拖到XCode的Libraries分組裏)git

clipboard.png

react-native須要集成的.xcodeproj文件清單以下:github

node_modules/react-native/React/React.xcodeproj
node_modules/react-native/Libraries/Image/RCTImage.xcodeproj
node_modules/react-native/Libraries/Network/RCTNetwork.xcodeproj
node_modules/react-native/Libraries/Text/RCTText.xcodeproj
node_modules/react-native/Libraries/Vibration/RCTVibration.xcodeproj
node_modules/react-native/Libraries/WebSocket/RCTWebSocket.xcodeproj
node_modules/react-native/Libraries/LinkingIOS/RCTLinking.xcodeproj
node_modules/react-native/Libraries/Settings/RCTSettings.xcodeproj

第二步添加.a文件

點擊你的主工程文件,選擇Build Phases,而後把剛纔所添加進去的.xcodeproj下的Products文件夾中的靜態庫文件(.a文件),拖到Link Binary With Libraries組內。npm

clipboard.png

第三步註冊頭文件路徑-不是全部依賴都須要,註冊React.xcodeproj便可

須要在原生代碼中使用這個庫,仍是隻須要經過JavaScript訪問?
若是你只須要經過JavaScript訪問這個庫,你就能夠跳過這步了。react-native

打開你的工程文件,選擇Build Settings,而後搜索Header Search Paths,而後添加庫所在的目錄(若是它還有像React這樣的子目錄須要包含,注意要選中recursive選項)xcode

clipboard.png

注意:React.xcodeproj是須要註冊的。下面的目錄要改爲本身的目錄。服務器

$(SRCROOT)/RN/node_modules/react-native/React

recursive

報錯: App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure

報錯緣由是須要開啓內網http的訪問權限。

  1. 在Info.plist中添加 NSAppTransportSecurity 類型 Dictionary ;

  2. 在 NSAppTransportSecurity 下添加 NSAllowsArbitraryLoads 類型Boolean ,值設爲 YES;

參考地址:

http://www.cnblogs.com/chglog/p/4746683.html

報錯:unrecognized selector sent to instance

解決方案:

Build Settings -> other linker flags -> -ObjC

參考地址1:https://github.com/facebook/react-native/issues/2396

參考地址2:http://stackoverflow.com/questions/32775481/rctbatchedbridge-perfstats-unrecognized-selector-sent-to-instance

報錯:RCTStatusBarManager module requires that the UIViewControllerBasedStatusBarAppearance key in the Info.plist is set to NO

錯誤信息說的很清楚了。

在Info.plist中添加View controller-based status bar appearance爲NO。

關閉自動啓動調試服務器

在React.xcodeproj的Build Phases中的第一個(有8081那個)Run Script。用 <<! 代碼代碼代碼 ! 註釋掉便可。

clipboard.png

相關文章
相關標籤/搜索