Skip to content

Instantly share code, notes, and snippets.

@cpsubrian
Created July 5, 2016 19:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cpsubrian/5f3acf2b9ed1212ee33703a6bee63737 to your computer and use it in GitHub Desktop.
Save cpsubrian/5f3acf2b9ed1212ee33703a6bee63737 to your computer and use it in GitHub Desktop.
Auto-IP Detection React-Native 0.28.0

Setup Auto-IP Dev for React Native 0.28.0

  • Edit AppDelegate.m
  • Edit Libraries > RCTWebSockert.. > RCTWebSocketExecutor.m
  • Add a Run Script in the Target > Build Phases area
// ...
NSURL *jsCodeLocation;
/**
* This Replaces the regular OPTION1/OPTION2 business with the hard-coded IPs.
* @see https://gist.github.com/cpsubrian/5f3acf2b9ed1212ee33703a6bee63737
* @see http://moduscreate.com/automated-ip-configuration-for-react-native-development/
* @see https://github.com/facebook/react-native/pull/4681/files
*/
#define AUTO_IP
// uncomment the following line to disable auto IP detection and use the old 0.16
// manual IP setting method:
//#undef AUTO_IP
// BE SURE TO SEE THE AUTO_IP #define in Libraries/RTCWebSocket.xcodeproj/RCTWebSocketExecutor.m
#ifdef AUTO_IP
#if DEBUG
#if TARGET_OS_SIMULATOR
#warning "DEBUG SIMULATOR"
jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.bundle?platform=ios&dev=true"];
#else
#warning "DEBUG DEVICE"
NSString *serverIP = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"SERVER_IP"];
NSString *jsCodeUrlString = [NSString stringWithFormat:@"http://%@:8081/index.ios.bundle?platform=ios&dev=true", serverIP];
NSString *jsBundleUrlString = [jsCodeUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
jsCodeLocation = [NSURL URLWithString:jsBundleUrlString];
#endif
#else
#warning "PRODUCTION DEVICE"
jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
#endif
#else
#warning "ORIGINAL 0.16 MANUAL STYLE"
/**
* 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 "Bundle React Native code and images" build step.
*/
// jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
#endif
/**/
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
// ...
if (!_url) {
NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
NSString *serverIP = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"SERVER_IP"];
NSInteger port = [standardDefaults integerForKey:@"websocket-executor-port"] ?: 8081;
NSString *URLString = [NSString stringWithFormat:@"http://%@:%zd/debugger-proxy?role=client", serverIP, port];
_url = [RCTConvert NSURL:URLString];
}
INFOPLIST="${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
echo "writing to $INFOPLIST"
PLISTCMD="Add :SERVER_IP string $(ifconfig | grep inet\ | tail -1 | cut -d " " -f 2)"
echo -n "$INFOPLIST" | xargs -0 /usr/libexec/PlistBuddy -c "$PLISTCMD" || true
PLISTCMD="Set :SERVER_IP $(ifconfig | grep inet\ | tail -1 | cut -d " " -f 2)"
echo -n "$INFOPLIST" | xargs -0 /usr/libexec/PlistBuddy -c "$PLISTCMD" || true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment