Skip to content

Automated IP Switching for Simulator/Devices #4245

@aphillipo

Description

@aphillipo

I have no idea where to add this but here is how to automate (mostly, under some conditions, your milage may vary - your network connection must be en0 etc.) changing the IP address of your machine when you want to run on the device.

  1. Click Your Project -> Build Phases -> Click the + -> New Run Script Phase
  2. Add the following code:
INFOPLIST="${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
echo "writing to $INFOPLIST"
PLISTCMD="Set :SERVER_IP $(ifconfig | grep inet\ | tail -1 | cut -d " " -f 2)"
echo -n "$INFOPLIST" | xargs -0 /usr/libexec/PlistBuddy -c "$PLISTCMD"

Next add the following to AppDelegate.m replacing the usual jsCodeLocation line:

NSString *jsBundleUrlString;

if (TARGET_IPHONE_SIMULATOR) {
  jsBundleUrlString = @"http://localhost:8081/index.ios.bundle";
} else {
  NSString *serverIP = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"SERVER_IP"];
  NSString *jsCodeUrlString = [NSString stringWithFormat:@"http://%@:8081/index.ios.bundle", serverIP];
  jsBundleUrlString = [jsCodeUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}

jsCodeLocation = [NSURL URLWithString:jsBundleUrlString];

And that's it, you should now be able to switch between the simulator and a device without any thought about where you are... Let me know if this works for you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions