Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running react-native app on iOS device using offline bundle

Tags:

react-native

The official React Native documentation to run app on iOS device using offline bundle says

Open ios/YourApp/AppDelegate.m

Uncomment the line, jsCodeLocation = [[NSBundle mainBundle] ...

Using latest react-native 0.30.0, this line is no longer present in the default AppDelegate.m file. Excerpt from the default AppDelegate.m file

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

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

If I try to use the jsCodeLocation as it used to be in previous versions, it is returned as null

jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];

I am not sure why is the URL for main.jsbundle returned as null. Is it not created by the packager anymore. If it should be created using the latest version also, how can I confirm it?

If you have run a react-native 0.30.0 app on an iOS device using offline bundle, please share the instructions to do so.

like image 335
Varun Gupta Avatar asked Aug 05 '16 02:08

Varun Gupta


People also ask

How do you run React Native on iOS physical device?

Plug in your device via USB​ If this is your first time running an app on your iOS device, you may need to register your device for development. Open the Product menu from Xcode's menubar, then go to Destination. Look for and select your device from the list. Xcode will then register your device for development.

Can React Native app run on iOS?

React Native supports building apps for iOS, Android, and web from a single code base. It's a more affordable technology than Swift, yet it allows creating high-quality apps.


2 Answers

EDIT: The whole bundle ip detection should be automatic on the latest release of react-native. If you select your device on xcode do build and run and run the app once. It should save an offline bundle on the phone so that if it doesn't find a packager server running, it will use the offline bundle.

source: https://github.com/facebook/react-native/commit/8c29a52c54392ce52148e7d3aa9f835537453aa4


If you change to a release scheme while building your app, that will compile your app with the offline bundle.

Product > Scheme > Edit Scheme > set build configuration to Release.

Not sure why they didn't update the documentation for the Appdelegate.m method. Looks like the line is still there on the sources. [Edit: there's a pull request for this]

like image 98
nabn Avatar answered Oct 08 '22 02:10

nabn


When I open the global proxy in ShadowsocksX, automatic IP detection won't return http://localhost:8081/index.ios.bundle in the debug mode. That's why the URL for main.jsbundle returned as null.

How to solve?

Option 1:

replace

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

with

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

This method could disable automatic IP detection.

Option 2:

Switch your proxy mode if you open the global proxy mode.

like image 22
parryworld Avatar answered Oct 08 '22 01:10

parryworld