From fd5c7c095f8e52e5aed7cb4093044d594bb7dfdc Mon Sep 17 00:00:00 2001 From: Nathan Azaria Date: Mon, 13 Jun 2016 15:06:20 +0100 Subject: [PATCH 1/5] Implemented automatic IP detection on iOS --- React/Base/RCTBundleURLProvider.m | 12 +++++++-- .../generator-ios/templates/app/AppDelegate.m | 27 ++----------------- packager/react-native-xcode.sh | 9 +++++++ 3 files changed, 21 insertions(+), 27 deletions(-) diff --git a/React/Base/RCTBundleURLProvider.m b/React/Base/RCTBundleURLProvider.m index 2f3bc3979df13f..f140c85540816a 100644 --- a/React/Base/RCTBundleURLProvider.m +++ b/React/Base/RCTBundleURLProvider.m @@ -19,9 +19,18 @@ static NSString *const kRCTEnableMinificationKey = @"RCT_enableMinification"; static NSString *const kDefaultPort = @"8081"; +static NSString *ipGuess; @implementation RCTBundleURLProvider ++ (void)initialize +{ + NSString *ipPath = [[NSBundle mainBundle] pathForResource:@"ip" ofType:@"txt"]; + NSString *ip = [NSString stringWithContentsOfFile:ipPath encoding:NSUTF8StringEncoding error:nil]; + ip = [ip stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\n"]]; + ipGuess = ip; +} + - (NSDictionary *)defaults { static NSDictionary *defaults; @@ -75,8 +84,7 @@ - (BOOL)isPackagerRunning:(NSString *)host - (NSString *)guessPackagerHost { - NSString *host = @"localhost"; - //TODO: Implement automatic IP address detection + NSString *host = ipGuess ? 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 \ From 0793bb3ac523a9d5f719ef02e0f031f9227fddff Mon Sep 17 00:00:00 2001 From: Nathan Azaria Date: Mon, 13 Jun 2016 15:46:07 +0100 Subject: [PATCH 2/5] Changed ipGuess assignment --- React/Base/RCTBundleURLProvider.m | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/React/Base/RCTBundleURLProvider.m b/React/Base/RCTBundleURLProvider.m index f140c85540816a..dba3ffcc47bffd 100644 --- a/React/Base/RCTBundleURLProvider.m +++ b/React/Base/RCTBundleURLProvider.m @@ -27,8 +27,7 @@ + (void)initialize { NSString *ipPath = [[NSBundle mainBundle] pathForResource:@"ip" ofType:@"txt"]; NSString *ip = [NSString stringWithContentsOfFile:ipPath encoding:NSUTF8StringEncoding error:nil]; - ip = [ip stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\n"]]; - ipGuess = ip; + ipGuess = [ip stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\n"]]; } - (NSDictionary *)defaults From 9d4902fd4356b5ebb7f635d2cc4a3b72637a6537 Mon Sep 17 00:00:00 2001 From: Nathan Azaria Date: Mon, 13 Jun 2016 15:58:18 +0100 Subject: [PATCH 3/5] Used newlineCharacterSet --- React/Base/RCTBundleURLProvider.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/React/Base/RCTBundleURLProvider.m b/React/Base/RCTBundleURLProvider.m index dba3ffcc47bffd..3232b825bb8110 100644 --- a/React/Base/RCTBundleURLProvider.m +++ b/React/Base/RCTBundleURLProvider.m @@ -27,7 +27,7 @@ + (void)initialize { NSString *ipPath = [[NSBundle mainBundle] pathForResource:@"ip" ofType:@"txt"]; NSString *ip = [NSString stringWithContentsOfFile:ipPath encoding:NSUTF8StringEncoding error:nil]; - ipGuess = [ip stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\n"]]; + ipGuess = [ip stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]]; } - (NSDictionary *)defaults From ff19d6da8afd87366f14d0a9d461e2e7cd174a92 Mon Sep 17 00:00:00 2001 From: Nathan Azaria Date: Mon, 13 Jun 2016 17:50:57 +0100 Subject: [PATCH 4/5] Made requested changes --- React/Base/RCTBundleURLProvider.m | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/React/Base/RCTBundleURLProvider.m b/React/Base/RCTBundleURLProvider.m index 3232b825bb8110..22f2cd32c900c9 100644 --- a/React/Base/RCTBundleURLProvider.m +++ b/React/Base/RCTBundleURLProvider.m @@ -25,9 +25,11 @@ @implementation RCTBundleURLProvider + (void)initialize { +#if RCT_DEV 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 @@ -83,7 +85,7 @@ - (BOOL)isPackagerRunning:(NSString *)host - (NSString *)guessPackagerHost { - NSString *host = ipGuess ? ipGuess : @"localhost"; + NSString *host = ipGuess ?: @"localhost"; if ([self isPackagerRunning:host]) { return host; } From a12038aa49fa9f8296dcf44ce12d08f2a0920b50 Mon Sep 17 00:00:00 2001 From: Nathan Azaria Date: Mon, 13 Jun 2016 19:41:11 +0100 Subject: [PATCH 5/5] Moved RCT_DEV check outside the method --- React/Base/RCTBundleURLProvider.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/React/Base/RCTBundleURLProvider.m b/React/Base/RCTBundleURLProvider.m index 22f2cd32c900c9..fe00e1414b3a35 100644 --- a/React/Base/RCTBundleURLProvider.m +++ b/React/Base/RCTBundleURLProvider.m @@ -23,14 +23,14 @@ @implementation RCTBundleURLProvider +#if RCT_DEV + (void)initialize { -#if RCT_DEV NSString *ipPath = [[NSBundle mainBundle] pathForResource:@"ip" ofType:@"txt"]; NSString *ip = [NSString stringWithContentsOfFile:ipPath encoding:NSUTF8StringEncoding error:nil]; ipGuess = [ip stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]]; -#endif } +#endif - (NSDictionary *)defaults {