Skip to content
This repository has been archived by the owner on Nov 11, 2024. It is now read-only.

Commit

Permalink
Feature: dev menu option to change packager location during runtime (…
Browse files Browse the repository at this point in the history
…#21970)

Summary:
Add an option to dev menu to change packager location on the fly on iOS (similar to Dev Settings in Android)
Pull Request resolved: facebook/react-native#21970

Differential Revision: D14162776

Pulled By: cpojer

fbshipit-source-id: 3cca4f4fbd8c599bd5342ba1ae64905a03270d48
  • Loading branch information
chubakueno authored and aleclarson committed Nov 30, 2019
1 parent 1a096aa commit 4bbd040
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
13 changes: 9 additions & 4 deletions React/Base/RCTBundleURLProvider.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,22 @@ - (void)resetToDefaults
[self settingsUpdated];
}

static NSURL *serverRootWithHost(NSString *host)
static NSURL *serverRootWithHostPort(NSString *hostPort)
{
if([hostPort rangeOfString:@":"].location != NSNotFound){
return [NSURL URLWithString:
[NSString stringWithFormat:@"http://%@/",
hostPort]];
}
return [NSURL URLWithString:
[NSString stringWithFormat:@"http://%@:%lu/",
host, (unsigned long)kRCTBundleURLProviderDefaultPort]];
hostPort, (unsigned long)kRCTBundleURLProviderDefaultPort]];
}

#if RCT_DEV
- (BOOL)isPackagerRunning:(NSString *)host
{
NSURL *url = [serverRootWithHost(host) URLByAppendingPathComponent:@"status"];
NSURL *url = [serverRootWithHostPort(host) URLByAppendingPathComponent:@"status"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLResponse *response;
NSError *error;
Expand Down Expand Up @@ -177,7 +182,7 @@ + (NSURL *)resourceURLForResourcePath:(NSString *)path
packagerHost:(NSString *)packagerHost
query:(NSString *)query
{
NSURLComponents *components = [NSURLComponents componentsWithURL:serverRootWithHost(packagerHost) resolvingAgainstBaseURL:NO];
NSURLComponents *components = [NSURLComponents componentsWithURL:serverRootWithHostPort(packagerHost) resolvingAgainstBaseURL:NO];
components.path = path;
if (query != nil) {
components.query = query;
Expand Down
56 changes: 56 additions & 0 deletions React/DevSupport/RCTDevMenu.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#import "RCTKeyCommands.h"
#import "RCTLog.h"
#import "RCTUtils.h"
#import "RCTDefines.h"
#import <React/RCTBundleURLProvider.h>

#define RCT_DEVMENU_TITLE @"React Native"

Expand Down Expand Up @@ -217,6 +219,12 @@ - (void)addItem:(RCTDevMenuItem *)item
[_extraMenuItems addObject:item];
}

- (void)setDefaultJSBundle {
[[RCTBundleURLProvider sharedSettings] resetToDefaults];
self->_bridge.bundleURL = [[RCTBundleURLProvider sharedSettings] jsBundleURLForFallbackResource:nil fallbackExtension:nil];
[self->_bridge reload];
}

- (NSArray<RCTDevMenuItem *> *)_menuItemsToPresent
{
NSMutableArray<RCTDevMenuItem *> *items = [NSMutableArray new];
Expand Down Expand Up @@ -295,6 +303,54 @@ - (void)addItem:(RCTDevMenuItem *)item
}]];
}

[items addObject:[RCTDevMenuItem buttonItemWithTitleBlock:^NSString *{
return @"Change packager location";
} handler:^{
UIAlertController * alertController = [UIAlertController alertControllerWithTitle: @"Change packager location"
message: @"Input packager IP, port and entrypoint"
preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"0.0.0.0";
}];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"8081";
}];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"index";
}];
[alertController addAction:[UIAlertAction actionWithTitle:@"Use bundled JS" style:UIAlertActionStyleDefault handler:^(__unused UIAlertAction *action) {
[self setDefaultJSBundle];
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"Use packager location" style:UIAlertActionStyleDefault handler:^(__unused UIAlertAction *action) {
NSArray * textfields = alertController.textFields;
UITextField * ipTextField = textfields[0];
UITextField * portTextField = textfields[1];
UITextField * bundleRootTextField = textfields[2];
NSString * bundleRoot = bundleRootTextField.text;
if(bundleRoot.length==0){
bundleRoot = @"index";
}
if(ipTextField.text.length == 0 && portTextField.text.length == 0) {
[self setDefaultJSBundle];
return;
}
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterDecimalStyle;
NSNumber *portNumber = [formatter numberFromString:portTextField.text];
if (portNumber == nil) {
portNumber = [NSNumber numberWithInt: RCT_METRO_PORT];
}
[RCTBundleURLProvider sharedSettings].jsLocation = [NSString stringWithFormat:@"%@:%d",
ipTextField.text, portNumber.intValue];
self->_bridge.bundleURL = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:bundleRoot fallbackResource:nil];
[self->_bridge reload];
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(__unused UIAlertAction *action) {
return;
}]];
[RCTPresentedViewController() presentViewController:alertController animated:YES completion:NULL];
}]];

[items addObject:[RCTDevMenuItem buttonItemWithTitleBlock:^NSString *{
return @"Toggle Inspector";
} handler:^{
Expand Down

0 comments on commit 4bbd040

Please sign in to comment.