From 8c29a52c54392ce52148e7d3aa9f835537453aa4 Mon Sep 17 00:00:00 2001 From: Nathan Azaria Date: Mon, 13 Jun 2016 15:52:19 -0700 Subject: [PATCH] Implemented automatic IP detection for iOS Summary: Implemented automatic IP detection for iOS, based on #6345 and #6362. As the previous pull requests did, this works by writing the IP address of the host to a file. Closes https://github.com/facebook/react-native/pull/8091 Differential Revision: D3427657 Pulled By: javache fbshipit-source-id: 3f534c9b32c4d6fb9615fc2e2c3c3aef421454c5 --- React/Base/RCTBundleURLProvider.m | 13 +++++++-- .../generator-ios/templates/app/AppDelegate.m | 27 ++----------------- packager/react-native-xcode.sh | 9 +++++++ 3 files changed, 22 insertions(+), 27 deletions(-) diff --git a/React/Base/RCTBundleURLProvider.m b/React/Base/RCTBundleURLProvider.m index 2f3bc3979df13f..fe00e1414b3a35 100644 --- a/React/Base/RCTBundleURLProvider.m +++ b/React/Base/RCTBundleURLProvider.m @@ -19,9 +19,19 @@ static NSString *const kRCTEnableMinificationKey = @"RCT_enableMinification"; static NSString *const kDefaultPort = @"8081"; +static NSString *ipGuess; @implementation RCTBundleURLProvider +#if RCT_DEV ++ (void)initialize +{ + NSString *ipPath = [[NSBundle mainBundle] pathForResource:@"ip" ofType:@"txt"]; + NSString *ip = [NSString stringWithContentsOfFile:ipPath encoding:NSUTF8StringEncoding error:nil]; + ipGuess = [ip stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]]; +} +#endif + - (NSDictionary *)defaults { static NSDictionary *defaults; @@ -75,8 +85,7 @@ - (BOOL)isPackagerRunning:(NSString *)host - (NSString *)guessPackagerHost { - NSString *host = @"localhost"; - //TODO: Implement automatic IP address detection + NSString *host = ipGuess ?: @"localhost"; if ([self isPackagerRunning:host]) { return host; } diff --git a/local-cli/generator-ios/templates/app/AppDelegate.m b/local-cli/generator-ios/templates/app/AppDelegate.m index 3b4879f8601256..edaccd8aaf7efb 100644 --- a/local-cli/generator-ios/templates/app/AppDelegate.m +++ b/local-cli/generator-ios/templates/app/AppDelegate.m @@ -9,6 +9,7 @@ #import "AppDelegate.h" +#import "RCTBundleURLProvider.h" #import "RCTRootView.h" @implementation AppDelegate @@ -17,31 +18,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( { NSURL *jsCodeLocation; - /** - * Loading JavaScript code - uncomment the one you want. - * - * OPTION 1 - * Load from development server. Start the server from the repository root: - * - * $ npm start - * - * To run on device, change `localhost` to the IP address of your computer - * (you can get this by typing `ifconfig` into the terminal and selecting the - * `inet` value under `en0:`) and make sure your computer and iOS device are - * on the same Wi-Fi network. - */ - - jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.bundle?platform=ios&dev=true"]; - - /** - * OPTION 2 - * Load from pre-bundled file on disk. The static bundle is automatically - * generated by the "Bundle React Native code and images" build step when - * running the project on an actual device or running the project on the - * simulator in the "Release" build configuration. - */ - -// jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"]; + jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil]; RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation moduleName:@"<%= name %>" diff --git a/packager/react-native-xcode.sh b/packager/react-native-xcode.sh index ad27792eb7bd02..eeedfa2818a5b9 100755 --- a/packager/react-native-xcode.sh +++ b/packager/react-native-xcode.sh @@ -68,6 +68,15 @@ type $NODE_BINARY >/dev/null 2>&1 || nodejs_not_found set -x DEST=$CONFIGURATION_BUILD_DIR/$UNLOCALIZED_RESOURCES_FOLDER_PATH +if [[ "$CONFIGURATION" = "Debug" && "$PLATFORM_NAME" != "iphonesimulator" ]]; then + PLISTBUDDY='/usr/libexec/PlistBuddy' + PLIST=$TARGET_BUILD_DIR/$INFOPLIST_PATH + IP=$(ipconfig getifaddr en0) + $PLISTBUDDY -c "Add NSAppTransportSecurity:NSExceptionDomains:localhost:NSTemporaryExceptionAllowsInsecureHTTPLoads bool true" $PLIST + $PLISTBUDDY -c "Add NSAppTransportSecurity:NSExceptionDomains:$IP.xip.io:NSTemporaryExceptionAllowsInsecureHTTPLoads bool true" $PLIST + echo "$IP.xip.io" > "$DEST/ip.txt" +fi + $NODE_BINARY "$REACT_NATIVE_DIR/local-cli/cli.js" bundle \ --entry-file index.ios.js \ --platform ios \