1. 程式人生 > >[轉]react native 真機除錯appdelegate.m找不到ip地址的處理

[轉]react native 真機除錯appdelegate.m找不到ip地址的處理

現象

對著書本及網上文章,如果要進行真機除錯,得修改appdelegate.m裡的ip,但高版本的react native已經做了大變動。
React Native iOS在0.29.0版本中BundleURL載入方法做了重大改變,新增了RCTBundleURLProvider單例類專門處理BundleURL,使用NSUserDefaults儲存配置資訊。
預設載入方式
在Debug模式下,執行react-native-xcode.sh編譯指令碼會自動獲取當前網絡卡en0的IP地址,並打入App包中一個配置檔案ip.txt,App執行時會讀取ip檔案,自動生成Developer Server URL,通過這種載入方式,我們不再需要手動去把”localhost”改成Mac的IP了,每次編譯都會讀取當前最新的IP。

jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@”index.ios” fallbackResource:nil];

解決方法

手動設定IP
RCTBundleURLProvider在介面中暴露了jsLocation屬性,可以通過setJsLocation手動設定IP。

NSURL *jsCodeLocation;

[[RCTBundleURLProvider sharedSettings] setDefaults];

if DEBUG

[[RCTBundleURLProvider sharedSettings] setJsLocation:@”192.168.3.13”];

endif

jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@”index.ios” fallbackResource:nil];

另需要在Info設定NSAppTransportSecurity的NSAllowsArbitraryLoads為true即可。

總之
RCTBundleURLProvider類做了一些訊息和屬性的封裝,可以通過判斷是否DEBUG環境然後做不同的設定。