如何在真实设备上打包和部署React-Native iOS应用程序

4

我找不到最新版本React-Native的文档,无法了解如何将应用程序捆绑并部署到实际设备上。

在我的iPhone上打开远程调试器时,我遇到了红屏错误,但是如果没有启用远程调试器,则应用程序会加载。

我没有更改AppDelegate.m中的任何网址,根Bundle仍然是@"index.ios"


错误信息是什么? - Tushar Khatiwada
2个回答

2

感谢Monochrome的回答,对我很有帮助!

从第4步开始我遇到了问题,我的AppDelegate.m文件看起来和你所解释的不太一样。

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

  RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                  moduleName:@"Kompas"
                                                  initialProperties:nil
                                                  launchOptions:launchOptions];

1

我自己也是RN开发的初学者,第一次尝试在真机上运行应用时遇到了同样的问题。在这里,我想与大家分享我是如何最终解决这个问题的:

  1. So firstly, make sure that your iPhone (or iPad) is connected to your Mac via the cable;

  2. Then check if both your Mac and iPhone are connected to the same Wi-Fi network;

  3. Then comes the tricky part. If you are using RN ver < 0.30, you have to configure your Info.plist file properly. To be clear, you have to disable such thing as App Transport Security (ATS). This is a security feature in iOS 9 which will block any unauthorized network activity that may happen on your smartphone, to keep it simple. The sad part is that ATS considers launching a RN app to be that kind of activity and blocks it. So you have to open your Info.plist file and add this code, officially proved by the Facebook team:

    <key>NSAppTransportSecurity</key>
    <dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
    </dict>
    

    You might wonder why you don't see it in the Docs now – well, as far as I know, this is because in RN ver 0.30 they made ATS disabled by default (you can read about it in the bottom of the page)

  4. Now you need to open your AppDelegate.m file and find this line:

    jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.bundle?platform=ios&dev=true"];

    Uncomment it and then change it to look like that: jsCodeLocation = [NSURL URLWithString:@"http://Your.Mac's_Ip.Address.Here:8081/index.ios.bundle?platform=ios&dev=true"];

    Like that: jsCodeLocation = [NSURL URLWithString:@"http://255.255.255.255:8081/index.ios.bundle?platform=ios&dev=true"];

  5. Then go to your simulator choosing list (idk how it is called, actually, just take a look at my screenshot :D ) and choose your connected device.

    Xcode screenshot

然后点击“运行”,你就可以开始了!请记住,有两种选项可以在设备上运行应用程序。第一种选项(我上面已经描述过)允许您在编辑器中编写代码,点击保存按钮,然后将重新加载应用程序到手机上,并且您可以在模拟器中执行所有操作,但是当您断开iPhone与Mac的连接时,您将无法在本地设备上运行应用程序。为了使其在本地工作而不需要连接Wi-Fi或Mac,请取消注释OPTION 2下面的行(不需要更改其中任何内容)。所有先前的步骤都保持不变。希望这有所帮助!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接