44
55#import < jschelpers/JSCWrapper.h>
66#import < UIKit/UIKit.h>
7+ #import < React/RCTLog.h>
78
89#import " RCTDefines.h"
910#import " RCTInspectorPackagerConnection.h"
1213
1314static NSString *const kDebuggerMsgDisable = @" { \" id\" :1,\" method\" :\" Debugger.disable\" }" ;
1415
15- static NSString *getDebugServerHost (NSURL *bundleURL)
16+ static NSString *getServerHost (NSURL *bundleURL, NSNumber *port )
1617{
1718 NSString *host = [bundleURL host ];
1819 if (!host) {
1920 host = @" localhost" ;
2021 }
2122
22- // Inspector Proxy is run on a separate port (from packager).
23- NSNumber *port = @8082 ;
24-
2523 // this is consistent with the Android implementation, where http:// is the
2624 // hardcoded implicit scheme for the debug server. Note, packagerURL
2725 // technically looks like it could handle schemes/protocols other than HTTP,
3230
3331static NSURL *getInspectorDeviceUrl (NSURL *bundleURL)
3432{
33+ NSNumber *inspectorProxyPort = @8082 ;
3534 NSString *escapedDeviceName = [[[UIDevice currentDevice ] name ] stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
3635 NSString *escapedAppName = [[[NSBundle mainBundle ] bundleIdentifier ] stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
3736 return [NSURL URLWithString: [NSString stringWithFormat: @" http://%@ /inspector/device?name=%@ &app=%@ " ,
38- getDebugServerHost (bundleURL),
37+ getServerHost (bundleURL, inspectorProxyPort ),
3938 escapedDeviceName,
4039 escapedAppName]];
4140}
4241
42+ static NSURL *getAttachDeviceUrl (NSURL *bundleURL, NSString *title)
43+ {
44+ NSNumber *metroBundlerPort = @8081 ;
45+ NSString *escapedDeviceName = [[[UIDevice currentDevice ] name ] stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
46+ NSString *escapedAppName = [[[NSBundle mainBundle ] bundleIdentifier ] stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
47+ return [NSURL URLWithString: [NSString stringWithFormat: @" http://%@ /attach-debugger-nuclide?title=%@ &device=%@ &app=%@ " ,
48+ getServerHost (bundleURL, metroBundlerPort),
49+ title,
50+ escapedDeviceName,
51+ escapedAppName]];
52+ }
4353
4454@implementation RCTInspectorDevServerHelper
4555
@@ -54,6 +64,41 @@ static void sendEventToAllConnections(NSString *event)
5464 }
5565}
5666
67+ static void displayErrorAlert (UIViewController *view, NSString *message) {
68+ UIAlertController *alert =
69+ [UIAlertController alertControllerWithTitle: nil
70+ message: message
71+ preferredStyle: UIAlertControllerStyleAlert];
72+ [view presentViewController: alert animated: YES completion: nil ];
73+ dispatch_after (
74+ dispatch_time (DISPATCH_TIME_NOW, NSEC_PER_SEC * 2.5 ),
75+ dispatch_get_main_queue (),
76+ ^{
77+ [alert dismissViewControllerAnimated: YES completion: nil ];
78+ });
79+ }
80+
81+ + (void )attachDebugger : (NSString *)owner
82+ withBundleURL : (NSURL *)bundleURL
83+ withView : (UIViewController *)view
84+ {
85+ NSURL *url = getAttachDeviceUrl (bundleURL, owner);
86+
87+ NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: url];
88+ [request setHTTPMethod: @" GET" ];
89+
90+ __weak UIViewController *viewCapture = view;
91+ [[[NSURLSession sharedSession ] dataTaskWithRequest: request completionHandler:
92+ ^(NSData *_Nullable data,
93+ NSURLResponse *_Nullable response,
94+ NSError *_Nullable error) {
95+ UIViewController *viewCaptureStrong = viewCapture;
96+ if (error != nullptr && viewCaptureStrong != nullptr ) {
97+ displayErrorAlert (viewCaptureStrong, @" The request to attach Nuclide couldn't reach Metro Bundler!" );
98+ }
99+ }] resume ];
100+ }
101+
57102+ (void )disableDebugger
58103{
59104 sendEventToAllConnections (kDebuggerMsgDisable );
0 commit comments