Skip to content

Commit

Permalink
fix(iOS): Replace UIKit with RCTUIKit on MacOS for RCT_NEW_ARCH_ENABL…
Browse files Browse the repository at this point in the history
…ED (#3296)

* Replace UIKit with RCTUIKit on MacOS for RCT_NEW_ARCH_ENABLED

* Exclude iOS specific destroy handler.

* Fix `Non-constant-expression cannot be narrowed from type 'BOOL' (aka 'signed char') to 'bool' in initializer` when building for Release

* Minor fix (typo)
  • Loading branch information
iSuslov committed Feb 13, 2024
1 parent 7af398c commit cb9fb9c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 27 deletions.
6 changes: 6 additions & 0 deletions apple/RNCWebView.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
#import <React/RCTViewComponentView.h>
#import <React/RCTConversions.h>
#import <WebKit/WKDataDetectorTypes.h>

#if !TARGET_OS_OSX
#import <UIKit/UIKit.h>
#else
#import <React/RCTUIKit.h>
#endif // !TARGET_OS_OSX

#import <react/renderer/components/RNCWebViewSpec/Props.h>

#ifndef NativeComponentExampleComponentView_h
Expand Down
63 changes: 36 additions & 27 deletions apple/RNCWebView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ + (ComponentDescriptorProvider)componentDescriptorProvider
return concreteComponentDescriptorProvider<RNCWebViewComponentDescriptor>();
}

#if !TARGET_OS_OSX
// Reproduce the idea from here: https://github.com/facebook/react-native/blob/8bd3edec88148d0ab1f225d2119435681fbbba33/React/Fabric/Mounting/ComponentViews/InputAccessory/RCTInputAccessoryComponentView.mm#L142
- (void)prepareForRecycle {
[super prepareForRecycle];
[_view destroyWebView];
}
#endif // !TARGET_OS_OSX

- (instancetype)initWithFrame:(CGRect)frame
{
Expand All @@ -74,10 +76,10 @@ - (instancetype)initWithFrame:(CGRect)frame
.lockIdentifier = [[dictionary valueForKey:@"lockIdentifier"] doubleValue],
.title = std::string([[dictionary valueForKey:@"title"] UTF8String]),
.navigationType = stringToOnShouldStartLoadWithRequestNavigationTypeEnum(std::string([[dictionary valueForKey:@"navigationType"] UTF8String])),
.canGoBack = [[dictionary valueForKey:@"canGoBack"] boolValue],
.canGoForward = [[dictionary valueForKey:@"canGoBack"] boolValue],
.isTopFrame = [[dictionary valueForKey:@"isTopFrame"] boolValue],
.loading = [[dictionary valueForKey:@"loading"] boolValue],
.canGoBack = static_cast<bool>([[dictionary valueForKey:@"canGoBack"] boolValue]),
.canGoForward = static_cast<bool>([[dictionary valueForKey:@"canGoBack"] boolValue]),
.isTopFrame = static_cast<bool>([[dictionary valueForKey:@"isTopFrame"] boolValue]),
.loading = static_cast<bool>([[dictionary valueForKey:@"loading"] boolValue]),
.mainDocumentURL = std::string([[dictionary valueForKey:@"mainDocumentURL"] UTF8String])
};
webViewEventEmitter->onShouldStartLoadWithRequest(data);
Expand All @@ -91,9 +93,9 @@ - (instancetype)initWithFrame:(CGRect)frame
.lockIdentifier = [[dictionary valueForKey:@"lockIdentifier"] doubleValue],
.title = std::string([[dictionary valueForKey:@"title"] UTF8String]),
.navigationType = stringToOnLoadingStartNavigationTypeEnum(std::string([[dictionary valueForKey:@"navigationType"] UTF8String])),
.canGoBack = [[dictionary valueForKey:@"canGoBack"] boolValue],
.canGoForward = [[dictionary valueForKey:@"canGoBack"] boolValue],
.loading = [[dictionary valueForKey:@"loading"] boolValue],
.canGoBack = static_cast<bool>([[dictionary valueForKey:@"canGoBack"] boolValue]),
.canGoForward = static_cast<bool>([[dictionary valueForKey:@"canGoBack"] boolValue]),
.loading = static_cast<bool>([[dictionary valueForKey:@"loading"] boolValue]),
.mainDocumentURL = std::string([[dictionary valueForKey:@"mainDocumentURL"] UTF8String], [[dictionary valueForKey:@"mainDocumentURL"] lengthOfBytesUsingEncoding:NSUTF8StringEncoding])
};
webViewEventEmitter->onLoadingStart(data);
Expand All @@ -108,9 +110,9 @@ - (instancetype)initWithFrame:(CGRect)frame
.title = std::string([[dictionary valueForKey:@"title"] UTF8String]),
.code = [[dictionary valueForKey:@"code"] intValue],
.description = std::string([[dictionary valueForKey:@"description"] UTF8String]),
.canGoBack = [[dictionary valueForKey:@"canGoBack"] boolValue],
.canGoForward = [[dictionary valueForKey:@"canGoBack"] boolValue],
.loading = [[dictionary valueForKey:@"loading"] boolValue],
.canGoBack = static_cast<bool>([[dictionary valueForKey:@"canGoBack"] boolValue]),
.canGoForward = static_cast<bool>([[dictionary valueForKey:@"canGoBack"] boolValue]),
.loading = static_cast<bool>([[dictionary valueForKey:@"loading"] boolValue]),
.domain = std::string([[dictionary valueForKey:@"domain"] UTF8String])
};
webViewEventEmitter->onLoadingError(data);
Expand All @@ -123,9 +125,9 @@ - (instancetype)initWithFrame:(CGRect)frame
.url = std::string([[dictionary valueForKey:@"url"] UTF8String]),
.lockIdentifier = [[dictionary valueForKey:@"lockIdentifier"] doubleValue],
.title = std::string([[dictionary valueForKey:@"title"] UTF8String]),
.canGoBack = [[dictionary valueForKey:@"canGoBack"] boolValue],
.canGoForward = [[dictionary valueForKey:@"canGoBack"] boolValue],
.loading = [[dictionary valueForKey:@"loading"] boolValue],
.canGoBack = static_cast<bool>([[dictionary valueForKey:@"canGoBack"] boolValue]),
.canGoForward = static_cast<bool>([[dictionary valueForKey:@"canGoBack"] boolValue]),
.loading = static_cast<bool>([[dictionary valueForKey:@"loading"] boolValue]),
.data = std::string([[dictionary valueForKey:@"data"] UTF8String])
};
webViewEventEmitter->onMessage(data);
Expand All @@ -139,9 +141,9 @@ - (instancetype)initWithFrame:(CGRect)frame
.lockIdentifier = [[dictionary valueForKey:@"lockIdentifier"] doubleValue],
.title = std::string([[dictionary valueForKey:@"title"] UTF8String]),
.navigationType = stringToOnLoadingFinishNavigationTypeEnum(std::string([[dictionary valueForKey:@"navigationType"] UTF8String], [[dictionary valueForKey:@"navigationType"] lengthOfBytesUsingEncoding:NSUTF8StringEncoding])),
.canGoBack = [[dictionary valueForKey:@"canGoBack"] boolValue],
.canGoForward = [[dictionary valueForKey:@"canGoBack"] boolValue],
.loading = [[dictionary valueForKey:@"loading"] boolValue],
.canGoBack = static_cast<bool>([[dictionary valueForKey:@"canGoBack"] boolValue]),
.canGoForward = static_cast<bool>([[dictionary valueForKey:@"canGoBack"] boolValue]),
.loading = static_cast<bool>([[dictionary valueForKey:@"loading"] boolValue]),
.mainDocumentURL = std::string([[dictionary valueForKey:@"mainDocumentURL"] UTF8String], [[dictionary valueForKey:@"mainDocumentURL"] lengthOfBytesUsingEncoding:NSUTF8StringEncoding])
};
webViewEventEmitter->onLoadingFinish(data);
Expand All @@ -154,9 +156,9 @@ - (instancetype)initWithFrame:(CGRect)frame
.url = std::string([[dictionary valueForKey:@"url"] UTF8String]),
.lockIdentifier = [[dictionary valueForKey:@"lockIdentifier"] doubleValue],
.title = std::string([[dictionary valueForKey:@"title"] UTF8String]),
.canGoBack = [[dictionary valueForKey:@"canGoBack"] boolValue],
.canGoForward = [[dictionary valueForKey:@"canGoBack"] boolValue],
.loading = [[dictionary valueForKey:@"loading"] boolValue],
.canGoBack = static_cast<bool>([[dictionary valueForKey:@"canGoBack"] boolValue]),
.canGoForward = static_cast<bool>([[dictionary valueForKey:@"canGoBack"] boolValue]),
.loading = static_cast<bool>([[dictionary valueForKey:@"loading"] boolValue]),
.progress = [[dictionary valueForKey:@"progress"] doubleValue]
};
webViewEventEmitter->onLoadingProgress(data);
Expand All @@ -169,9 +171,9 @@ - (instancetype)initWithFrame:(CGRect)frame
.url = std::string([[dictionary valueForKey:@"url"] UTF8String]),
.lockIdentifier = [[dictionary valueForKey:@"lockIdentifier"] doubleValue],
.title = std::string([[dictionary valueForKey:@"title"] UTF8String]),
.canGoBack = [[dictionary valueForKey:@"canGoBack"] boolValue],
.canGoForward = [[dictionary valueForKey:@"canGoBack"] boolValue],
.loading = [[dictionary valueForKey:@"loading"] boolValue]
.canGoBack = static_cast<bool>([[dictionary valueForKey:@"canGoBack"] boolValue]),
.canGoForward = static_cast<bool>([[dictionary valueForKey:@"canGoBack"] boolValue]),
.loading = static_cast<bool>([[dictionary valueForKey:@"loading"] boolValue])
};
webViewEventEmitter->onContentProcessDidTerminate(data);
}
Expand Down Expand Up @@ -229,9 +231,9 @@ - (instancetype)initWithFrame:(CGRect)frame
.title = std::string([[dictionary valueForKey:@"title"] UTF8String]),
.statusCode = [[dictionary valueForKey:@"statusCode"] intValue],
.description = std::string([[dictionary valueForKey:@"description"] UTF8String]),
.canGoBack = [[dictionary valueForKey:@"canGoBack"] boolValue],
.canGoForward = [[dictionary valueForKey:@"canGoBack"] boolValue],
.loading = [[dictionary valueForKey:@"loading"] boolValue]
.canGoBack = static_cast<bool>([[dictionary valueForKey:@"canGoBack"] boolValue]),
.canGoForward = static_cast<bool>([[dictionary valueForKey:@"canGoBack"] boolValue]),
.loading = static_cast<bool>([[dictionary valueForKey:@"loading"] boolValue])
};
webViewEventEmitter->onHttpError(data);
}
Expand Down Expand Up @@ -284,9 +286,10 @@ - (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &
REMAP_WEBVIEW_PROP(cacheEnabled)
REMAP_WEBVIEW_PROP(allowsLinkPreview)
REMAP_WEBVIEW_STRING_PROP(allowingReadAccessToURL)

REMAP_WEBVIEW_PROP(messagingEnabled)
#if !TARGET_OS_OSX
REMAP_WEBVIEW_PROP(fraudulentWebsiteWarningEnabled)
#endif // !TARGET_OS_OSX
REMAP_WEBVIEW_PROP(enableApplePay)
REMAP_WEBVIEW_PROP(pullToRefreshEnabled)
REMAP_WEBVIEW_PROP(bounces)
Expand All @@ -310,7 +313,8 @@ - (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 140500 /* iOS 14.5 */
REMAP_WEBVIEW_PROP(textInteractionEnabled)
#endif


#if !TARGET_OS_OSX
if (oldViewProps.dataDetectorTypes != newViewProps.dataDetectorTypes) {
WKDataDetectorTypes dataDetectorTypes = WKDataDetectorTypeNone;
if (dataDetectorTypes & RNCWebViewDataDetectorTypes::Address) {
Expand All @@ -334,6 +338,8 @@ - (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &
}
[_view setDataDetectorTypes:dataDetectorTypes];
}
#endif // !TARGET_OS_OSX

if (oldViewProps.contentInset.top != newViewProps.contentInset.top || oldViewProps.contentInset.left != newViewProps.contentInset.left || oldViewProps.contentInset.right != newViewProps.contentInset.right || oldViewProps.contentInset.bottom != newViewProps.contentInset.bottom) {
UIEdgeInsets edgesInsets = {
.top = newViewProps.contentInset.top,
Expand All @@ -350,6 +356,8 @@ - (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &
@"password": RCTNSStringFromString(newViewProps.basicAuthCredential.password)
}];
}

#if !TARGET_OS_OSX
if (oldViewProps.contentInsetAdjustmentBehavior != newViewProps.contentInsetAdjustmentBehavior) {
if (newViewProps.contentInsetAdjustmentBehavior == RNCWebViewContentInsetAdjustmentBehavior::Never) {
[_view setContentInsetAdjustmentBehavior: UIScrollViewContentInsetAdjustmentNever];
Expand All @@ -361,6 +369,7 @@ - (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &
[_view setContentInsetAdjustmentBehavior: UIScrollViewContentInsetAdjustmentAlways];
}
}
#endif // !TARGET_OS_OSX

if (oldViewProps.menuItems != newViewProps.menuItems) {
NSMutableArray *newMenuItems = [NSMutableArray array];
Expand Down

0 comments on commit cb9fb9c

Please sign in to comment.