Skip to content

Commit 43e78ff

Browse files
zphrspewsheen
andauthored
Feat: add WebViewBuilder::with_limit_navigations_to_app_bound_domains (#1588)
* Added `with_limit_navigations_to_app_bound_domains` to WebViewBuilder * Update .changes/app-bound-domains.md --------- Co-authored-by: Jason Tsai <[email protected]>
1 parent 6a89f46 commit 43e78ff

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

.changes/app-bound-domains.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"wry": "patch"
3+
---
4+
5+
Add `WebViewBuilder::with_limit_navigations_to_app_bound_domains` only on iOS.
6+
Function is a no-op if iOS version is less than iOS 14.

src/lib.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,6 +1360,8 @@ pub(crate) struct PlatformSpecificWebViewAttributes {
13601360
allow_link_preview: bool,
13611361
#[cfg(target_os = "ios")]
13621362
input_accessory_view_builder: Option<Box<InputAccessoryViewBuilder>>,
1363+
#[cfg(target_os = "ios")]
1364+
limit_navigations_to_app_bound_domains: bool,
13631365
}
13641366

13651367
#[cfg(any(target_os = "macos", target_os = "ios"))]
@@ -1372,6 +1374,8 @@ impl Default for PlatformSpecificWebViewAttributes {
13721374
allow_link_preview: true,
13731375
#[cfg(target_os = "ios")]
13741376
input_accessory_view_builder: None,
1377+
#[cfg(target_os = "ios")]
1378+
limit_navigations_to_app_bound_domains: false,
13751379
}
13761380
}
13771381
}
@@ -1432,6 +1436,30 @@ pub trait WebViewBuilderExtIos {
14321436
self,
14331437
builder: F,
14341438
) -> Self;
1439+
/// Whether to limit navigations to App-Bound Domains. This is necessary
1440+
/// to enable Service Workers on iOS.
1441+
///
1442+
/// Note: If you set limit_navigations to true
1443+
/// make sure to add the following to Info.plist in the iOS project:
1444+
/// ```xml
1445+
/// <plist>
1446+
/// <dict>
1447+
/// <key>WKAppBoundDomains</key>
1448+
/// <array>
1449+
/// <string>localhost</string>
1450+
/// </array>
1451+
/// </dict>
1452+
/// </plist>
1453+
/// ```
1454+
/// You should also add any additional domains which your app requests assets from.
1455+
/// Assets served through custom protocols like Tauri's IPC are added to the
1456+
/// list automatically. Available on iOS only.
1457+
///
1458+
/// Default is false.
1459+
///
1460+
/// See https://webkit.org/blog/10882/app-bound-domains/ and
1461+
/// https://developer.apple.com/documentation/webkit/wkwebviewconfiguration/limitsnavigationstoappbounddomains
1462+
fn with_limit_navigations_to_app_bound_domains(self, limit_navigations: bool) -> Self;
14351463
}
14361464

14371465
#[cfg(target_os = "ios")]
@@ -1448,6 +1476,12 @@ impl WebViewBuilderExtIos for WebViewBuilder<'_> {
14481476
.replace(Box::new(builder));
14491477
self
14501478
}
1479+
fn with_limit_navigations_to_app_bound_domains(mut self, limit_navigations: bool) -> Self {
1480+
self
1481+
.platform_specific
1482+
.limit_navigations_to_app_bound_domains = limit_navigations;
1483+
self
1484+
}
14511485
}
14521486

14531487
#[cfg(windows)]

src/wkwebview/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ use once_cell::sync::Lazy;
6363

6464
#[cfg(target_os = "ios")]
6565
use crate::wkwebview::ios::WKWebView::WKWebView;
66+
#[cfg(target_os = "ios")]
67+
use crate::wkwebview::util::operating_system_version;
68+
6669
#[cfg(target_os = "macos")]
6770
use objc2_web_kit::WKWebView;
6871

@@ -283,6 +286,12 @@ impl InnerWebView {
283286
let _preference = config.preferences();
284287
let _yes = NSNumber::numberWithBool(true);
285288

289+
#[cfg(target_os = "ios")]
290+
{
291+
if pl_attrs.limit_navigations_to_app_bound_domains && operating_system_version().0 >= 14 {
292+
config.setLimitsNavigationsToAppBoundDomains(true);
293+
}
294+
}
286295
#[cfg(feature = "mac-proxy")]
287296
if let Some(proxy_config) = attributes.proxy_config {
288297
let proxy_config = match proxy_config {

0 commit comments

Comments
 (0)