4
4
5
5
#import < jschelpers/JSCWrapper.h>
6
6
#import < UIKit/UIKit.h>
7
+ #import < React/RCTLog.h>
7
8
8
9
#import " RCTDefines.h"
9
10
#import " RCTInspectorPackagerConnection.h"
12
13
13
14
static NSString *const kDebuggerMsgDisable = @" { \" id\" :1,\" method\" :\" Debugger.disable\" }" ;
14
15
15
- static NSString *getDebugServerHost (NSURL *bundleURL)
16
+ static NSString *getServerHost (NSURL *bundleURL, NSNumber *port )
16
17
{
17
18
NSString *host = [bundleURL host ];
18
19
if (!host) {
19
20
host = @" localhost" ;
20
21
}
21
22
22
- // Inspector Proxy is run on a separate port (from packager).
23
- NSNumber *port = @8082 ;
24
-
25
23
// this is consistent with the Android implementation, where http:// is the
26
24
// hardcoded implicit scheme for the debug server. Note, packagerURL
27
25
// technically looks like it could handle schemes/protocols other than HTTP,
32
30
33
31
static NSURL *getInspectorDeviceUrl (NSURL *bundleURL)
34
32
{
33
+ NSNumber *inspectorProxyPort = @8082 ;
35
34
NSString *escapedDeviceName = [[[UIDevice currentDevice ] name ] stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
36
35
NSString *escapedAppName = [[[NSBundle mainBundle ] bundleIdentifier ] stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
37
36
return [NSURL URLWithString: [NSString stringWithFormat: @" http://%@ /inspector/device?name=%@ &app=%@ " ,
38
- getDebugServerHost (bundleURL),
37
+ getServerHost (bundleURL, inspectorProxyPort ),
39
38
escapedDeviceName,
40
39
escapedAppName]];
41
40
}
42
41
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
+ }
43
53
44
54
@implementation RCTInspectorDevServerHelper
45
55
@@ -54,6 +64,41 @@ static void sendEventToAllConnections(NSString *event)
54
64
}
55
65
}
56
66
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
+
57
102
+ (void )disableDebugger
58
103
{
59
104
sendEventToAllConnections (kDebuggerMsgDisable );
0 commit comments