From 7decc5cff1de221ad729ab16f7b18901dc2f889a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivari=20T=C3=B6lp?= Date: Wed, 17 Mar 2021 01:29:40 +0200 Subject: [PATCH] feat(ios): Add support for `limitsNavigationsToAppBoundDomains` (#1662) * Add support for iOS-specific prop `limitsNavigationsToAppBoundDomains` * Add check for `limitsNavigationsToAppBoundDomains` property Turns out that @available is simply bugged right now in Xcode and will pretty much always return `true`. Adding a check for the property actually existing as well will avoid iOS <14 crashing horribly. * Improve documentation Co-authored-by: Thibault Malbranche --- apple/RNCWebView.h | 4 ++++ apple/RNCWebView.m | 10 ++++++++++ apple/RNCWebViewManager.m | 4 ++++ docs/Reference.md | 20 ++++++++++++++++++++ src/WebViewTypes.ts | 12 ++++++++++++ 5 files changed, 50 insertions(+) diff --git a/apple/RNCWebView.h b/apple/RNCWebView.h index 7eea051..805031e 100644 --- a/apple/RNCWebView.h +++ b/apple/RNCWebView.h @@ -76,6 +76,10 @@ @property (nonatomic, assign) WKContentMode contentMode; #endif +#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 140000 /* iOS 14 */ +@property (nonatomic, assign) BOOL limitsNavigationsToAppBoundDomains; +#endif + + (void)setClientAuthenticationCredential:(nullable NSURLCredential*)credential; + (void)setCustomCertificatesForHost:(nullable NSDictionary *)certificates; - (void)postMessage:(NSString *_Nullable)message; diff --git a/apple/RNCWebView.m b/apple/RNCWebView.m index 0cd79a6..9658d03 100644 --- a/apple/RNCWebView.m +++ b/apple/RNCWebView.m @@ -245,6 +245,16 @@ static NSDictionary* customCertificatesForHost; } #endif +#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 140000 /* iOS 14 */ + if (@available(iOS 14.0, *)) { + if ([wkWebViewConfig respondsToSelector:@selector(limitsNavigationsToAppBoundDomains)]) { + if (_limitsNavigationsToAppBoundDomains) { + wkWebViewConfig.limitsNavigationsToAppBoundDomains = YES; + } + } + } +#endif + // Shim the HTML5 history API: [wkWebViewConfig.userContentController addScriptMessageHandler:[[RNCWeakScriptMessageDelegate alloc] initWithDelegate:self] name:HistoryShimName]; diff --git a/apple/RNCWebViewManager.m b/apple/RNCWebViewManager.m index fd522f1..270d3e4 100644 --- a/apple/RNCWebViewManager.m +++ b/apple/RNCWebViewManager.m @@ -89,6 +89,10 @@ RCT_EXPORT_VIEW_PROPERTY(automaticallyAdjustsScrollIndicatorInsets, BOOL) RCT_EXPORT_VIEW_PROPERTY(contentMode, WKContentMode) #endif +#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 140000 /* iOS 14 */ +RCT_EXPORT_VIEW_PROPERTY(limitsNavigationsToAppBoundDomains, BOOL) +#endif + /** * Expose methods to enable messaging the webview. */ diff --git a/docs/Reference.md b/docs/Reference.md index 7896a02..7fd4eba 100644 --- a/docs/Reference.md +++ b/docs/Reference.md @@ -73,6 +73,7 @@ This document lays out the current public properties and methods for the React N - [`pullToRefreshEnabled`](Reference.md#pullToRefreshEnabled) - [`ignoreSilentHardwareSwitch`](Reference.md#ignoreSilentHardwareSwitch) - [`onFileDownload`](Reference.md#onFileDownload) +- [`limitsNavigationsToAppBoundDomains`](Reference.md#limitsNavigationsToAppBoundDomains) - [`autoManageStatusBarEnabled`](Reference.md#autoManageStatusBarEnabled) - [`setSupportMultipleWindows`](Reference.md#setSupportMultipleWindows) @@ -1314,6 +1315,25 @@ Example: --- +### `limitsNavigationsToAppBoundDomains`[⬆](#props-index) + +If true indicates to WebKit that a WKWebView will only navigate to app-bound domains. Only applicable for iOS 14 or greater. + +Once set, any attempt to navigate away from an app-bound domain will fail with the error “App-bound domain failure.” +Applications can specify up to 10 “app-bound” domains using a new Info.plist key `WKAppBoundDomains`. For more information see [App-Bound Domains](https://webkit.org/blog/10882/app-bound-domains/). + +| Type | Required | Platform | +| ------- | -------- | -------- | +| boolean | No | iOS | + +Example: + +```jsx + +``` + +--- + ### `autoManageStatusBarEnabled` If set to `true`, the status bar will be automatically hidden/shown by WebView, specifically when full screen video is being watched. If `false`, WebView will not manage the status bar at all. The default value is `true`. diff --git a/src/WebViewTypes.ts b/src/WebViewTypes.ts index 574acf5..020db16 100644 --- a/src/WebViewTypes.ts +++ b/src/WebViewTypes.ts @@ -332,6 +332,7 @@ export interface IOSNativeWebViewProps extends CommonNativeWebViewProps { injectedJavaScriptForMainFrameOnly?: boolean; injectedJavaScriptBeforeContentLoadedForMainFrameOnly?: boolean; onFileDownload?: (event: FileDownloadEvent) => void; + limitsNavigationsToAppBoundDomains?: boolean; } export interface MacOSNativeWebViewProps extends CommonNativeWebViewProps { @@ -614,6 +615,17 @@ export interface IOSWebViewProps extends WebViewSharedProps { * If not provided, the default is to let the webview try to render the file. */ onFileDownload?: (event: FileDownloadEvent) => void; + + /** + * A Boolean value which, when set to `true`, indicates to WebKit that a WKWebView + * will only navigate to app-bound domains. Once set, any attempt to navigate away + * from an app-bound domain will fail with the error “App-bound domain failure.” + * + * Applications can specify up to 10 “app-bound” domains using a new + * Info.plist key `WKAppBoundDomains`. + * @platform ios + */ + limitsNavigationsToAppBoundDomains?: boolean; } export interface MacOSWebViewProps extends WebViewSharedProps {