Skip to content

Commit f21fa4e

Browse files
andymatuschakfacebook-github-bot
authored andcommitted
Enabling RCTWebSocket on UIKitForMac (macOS Catalyst) (#27469)
Summary: In #25427, radex added initial support for running React Native projects on macOS via Catalyst. However, `RCTWebSocket` was disabled for that target because of some compilation issues. This meant that running projects via a connection to the packager wasn't possible: no live reload, and projects must be run in "Release" mode. It also meant making manual changes to Xcode projects deploying to macOS and scattering a number of conditional checks throughout the codebase. In this change, I've implemented support for `RCTWebSocket` on the macOS target and re-enabled the affected features. Live reload and the inspector now work for macOS targets. Manual modifications of Xcode build settings are no longer necessary for react-native projects running on macOS. ![Screen Shot 2019-12-10 at 8 36 38 AM](https://user-images.githubusercontent.com/2771/70549905-ce7b0800-1b29-11ea-85c6-07bf09811ae2.png) ### Limitations There's no binding which displays the developer menu (since there's no shake event on macOS). We'll probably want to add one, perhaps to the menu bar. I've chosen not to commit the modifications to RNTester which enable macOS support, since that would imply more "official" support for this target than I suspect you all would like to convey. I'm happy to add those chunks if it would be helpful. ## Changelog [iOS] [Added] - Added web socket support for macOS (Catalyst), enabling debug builds and live reload Pull Request resolved: #27469 Test Plan: * Open RNTester/RNTester.xcodeproj with Xcode 11.2.1, run it like a normal iOS app -- make sure it compiles and runs correctly (no regression) * Select "My Mac" as device target, and run. You may need to configure a valid development team to make signing work. * RNTester should run fine with no additional configuration. Modify a file in RNTester, note that live reload is now working. * Test the developer inspector. To display the developer menu, you'll need to manually show it; here's an example diff which does that: ``` diff --git a/RNTester/js/RNTesterApp.ios.js b/RNTester/js/RNTesterApp.ios.js index 8245a68d12..a447ad3b1b 100644 --- a/RNTester/js/RNTesterApp.ios.js +++ b/RNTester/js/RNTesterApp.ios.js @@ -19,6 +19,8 @@ const React = require('react'); const SnapshotViewIOS = require('./examples/Snapshot/SnapshotViewIOS.ios'); const URIActionMap = require('./utils/URIActionMap'); +import NativeDevMenu from '../../Libraries/NativeModules/specs/NativeDevMenu'; + const { AppRegistry, AsyncStorage, @@ -143,6 +145,7 @@ class RNTesterApp extends React.Component<Props, RNTesterNavigationState> { UNSAFE_componentWillMount() { BackHandler.addEventListener('hardwareBackPress', this._handleBack); + NativeDevMenu.show(); } componentDidMount() { ``` Reviewed By: sammy-SC Differential Revision: D18945861 Pulled By: hramos fbshipit-source-id: edcf02c5803742c89a845a3e5d72bc7dacae839f
1 parent 0a525b6 commit f21fa4e

13 files changed

+18
-23
lines changed

Libraries/PushNotificationIOS/RCTPushNotificationManager.mm

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
static NSString *const kErrorUnableToRequestPermissions = @"E_UNABLE_TO_REQUEST_PERMISSIONS";
2828

29-
#if !TARGET_OS_TV && !TARGET_OS_UIKITFORMAC
29+
#if !TARGET_OS_TV
3030
@implementation RCTConvert (NSCalendarUnit)
3131

3232
RCT_ENUM_CONVERTER(NSCalendarUnit,

Libraries/WebSocket/RCTSRWebSocket.m

+6-11
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,9 @@
1414
// limitations under the License.
1515
//
1616

17-
#if !TARGET_OS_UIKITFORMAC
18-
1917
#import <React/RCTSRWebSocket.h>
2018

2119
#import <Availability.h>
22-
#import <Endian.h>
2320

2421
#import <Security/SecRandom.h>
2522

@@ -580,7 +577,7 @@ - (void)closeWithCode:(NSInteger)code reason:(NSString *)reason
580577
NSMutableData *mutablePayload = [[NSMutableData alloc] initWithLength:sizeof(uint16_t) + maxMsgSize];
581578
NSData *payload = mutablePayload;
582579

583-
((uint16_t *)mutablePayload.mutableBytes)[0] = EndianU16_BtoN(code);
580+
((uint16_t *)mutablePayload.mutableBytes)[0] = NSSwapBigShortToHost(code);
584581

585582
if (reason) {
586583
NSRange remainingRange = {0};
@@ -749,7 +746,7 @@ - (void)handleCloseWithData:(NSData *)data
749746
return;
750747
} else if (dataSize >= 2) {
751748
[data getBytes:&closeCode length:sizeof(closeCode)];
752-
_closeCode = EndianU16_BtoN(closeCode);
749+
_closeCode = NSSwapBigShortToHost(closeCode);
753750
if (!closeCodeIsValid(_closeCode)) {
754751
[self _closeWithProtocolError:[NSString stringWithFormat:@"Cannot have close code of %d", _closeCode]];
755752
return;
@@ -972,12 +969,12 @@ - (void)_readFrameContinue
972969

973970
if (header.payload_length == 126) {
974971
assert(mapped_size >= sizeof(uint16_t));
975-
uint16_t newLen = EndianU16_BtoN(*(uint16_t *)(mapped_buffer));
972+
uint16_t newLen = NSSwapBigShortToHost(*(uint16_t *)(mapped_buffer));
976973
header.payload_length = newLen;
977974
offset += sizeof(uint16_t);
978975
} else if (header.payload_length == 127) {
979976
assert(mapped_size >= sizeof(uint64_t));
980-
header.payload_length = EndianU64_BtoN(*(uint64_t *)(mapped_buffer));
977+
header.payload_length = NSSwapBigLongLongToHost(*(uint64_t *)(mapped_buffer));
981978
offset += sizeof(uint64_t);
982979
} else {
983980
assert(header.payload_length < 126 && header.payload_length >= 0);
@@ -1264,11 +1261,11 @@ - (void)_sendFrameWithOpcode:(RCTSROpCode)opcode data:(NSData *)data
12641261
frame_buffer[1] |= payloadLength;
12651262
} else if (payloadLength <= UINT16_MAX) {
12661263
frame_buffer[1] |= 126;
1267-
*((uint16_t *)(frame_buffer + frame_buffer_size)) = EndianU16_BtoN((uint16_t)payloadLength);
1264+
*((uint16_t *)(frame_buffer + frame_buffer_size)) = NSSwapBigShortToHost((uint16_t)payloadLength);
12681265
frame_buffer_size += sizeof(uint16_t);
12691266
} else {
12701267
frame_buffer[1] |= 127;
1271-
*((uint64_t *)(frame_buffer + frame_buffer_size)) = EndianU64_BtoN((uint64_t)payloadLength);
1268+
*((uint64_t *)(frame_buffer + frame_buffer_size)) = NSSwapBigLongLongToHost((uint64_t)payloadLength);
12721269
frame_buffer_size += sizeof(uint64_t);
12731270
}
12741271

@@ -1637,5 +1634,3 @@ - (NSRunLoop *)runLoop
16371634
}
16381635

16391636
@end
1640-
1641-
#endif

React/Base/RCTBridge.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ - (void)reload
305305
*/
306306
- (void)reloadWithReason:(NSString *)reason
307307
{
308-
#if RCT_ENABLE_INSPECTOR && !TARGET_OS_UIKITFORMAC
308+
#if RCT_ENABLE_INSPECTOR
309309
// Disable debugger to resume the JsVM & avoid thread locks while reloading
310310
[RCTInspectorDevServerHelper disableDebugger];
311311
#endif

React/Base/RCTDefines.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
#endif
6464

6565
#ifndef ENABLE_PACKAGER_CONNECTION
66-
#if RCT_DEV && (__has_include("RCTPackagerConnection.h") || __has_include(<React/RCTPackagerConnection.h>)) && !TARGET_OS_UIKITFORMAC
66+
#if RCT_DEV && (__has_include("RCTPackagerConnection.h") || __has_include(<React/RCTPackagerConnection.h>))
6767
#define ENABLE_PACKAGER_CONNECTION 1
6868
#else
6969
#define ENABLE_PACKAGER_CONNECTION 0

React/CoreModules/RCTDevMenu.mm

+1-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ - (void)setDefaultJSBundle
262262
if (devSettings.isNuclideDebuggingAvailable && !devSettings.isDebuggingRemotely) {
263263
[items addObject:[RCTDevMenuItem buttonItemWithTitle:@"Debug with Nuclide"
264264
handler:^{
265-
#if RCT_ENABLE_INSPECTOR && !TARGET_OS_UIKITFORMAC
265+
#if RCT_ENABLE_INSPECTOR
266266
[RCTInspectorDevServerHelper
267267
attachDebugger:@"ReactNative"
268268
withBundleURL:bridge.bundleURL

React/CoreModules/RCTDevSettings.mm

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ - (void)setBridge:(RCTBridge *)bridge
176176
forMethod:@"reload"];
177177
#endif
178178

179-
#if RCT_ENABLE_INSPECTOR && !TARGET_OS_UIKITFORMAC
179+
#if RCT_ENABLE_INSPECTOR
180180
// we need this dispatch back to the main thread because even though this
181181
// is executed on the main thread, at this point the bridge is not yet
182182
// finished with its initialisation. But it does finish by the time it

React/DevSupport/RCTInspectorDevServerHelper.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#import <React/RCTDefines.h>
1212
#import <React/RCTInspectorPackagerConnection.h>
1313

14-
#if RCT_DEV && !TARGET_OS_UIKITFORMAC
14+
#if RCT_DEV
1515

1616
@interface RCTInspectorDevServerHelper : NSObject
1717

React/DevSupport/RCTInspectorDevServerHelper.mm

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#import <React/RCTInspectorDevServerHelper.h>
99

10-
#if RCT_DEV && !TARGET_OS_UIKITFORMAC
10+
#if RCT_DEV
1111

1212
#import <React/RCTLog.h>
1313
#import <UIKit/UIKit.h>

React/DevSupport/RCTPackagerConnection.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
#import <React/RCTDefines.h>
1111

12-
#if RCT_DEV && !TARGET_OS_UIKITFORMAC
12+
#if RCT_DEV
1313

1414
NS_ASSUME_NONNULL_BEGIN
1515

React/DevSupport/RCTPackagerConnection.mm

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#import <React/RCTReconnectingWebSocket.h>
2222
#import <React/RCTUtils.h>
2323

24-
#if RCT_DEV && !TARGET_OS_UIKITFORMAC
24+
#if RCT_DEV
2525

2626
#import <React/RCTSRWebSocket.h>
2727

React/Inspector/RCTInspector.mm

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#import <React/RCTInspector.h>
99

10-
#if RCT_DEV && !TARGET_OS_UIKITFORMAC
10+
#if RCT_DEV
1111

1212
#include <jsinspector/InspectorInterfaces.h>
1313

React/Inspector/RCTInspectorPackagerConnection.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#import <Foundation/Foundation.h>
99
#import <React/RCTDefines.h>
1010

11-
#if RCT_DEV && !TARGET_OS_UIKITFORMAC
11+
#if RCT_DEV
1212

1313
@interface RCTBundleStatus : NSObject
1414
@property (atomic, assign) BOOL isLastBundleDownloadSuccess;

React/Inspector/RCTInspectorPackagerConnection.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#import <React/RCTInspectorPackagerConnection.h>
99

10-
#if RCT_DEV && !TARGET_OS_UIKITFORMAC
10+
#if RCT_DEV
1111

1212
#import <React/RCTDefines.h>
1313
#import <React/RCTInspector.h>

0 commit comments

Comments
 (0)