diff --git a/.changes/app-bound-domains.md b/.changes/app-bound-domains.md new file mode 100644 index 000000000000..e74e635f4790 --- /dev/null +++ b/.changes/app-bound-domains.md @@ -0,0 +1,7 @@ +--- +"tauri": patch:feat +"tauri-runtime": patch:feat +"tauri-runtime-wry": patch:feat +--- + +Add `WebviewBuilder::limit_navigations_to_app_bound_domains` and `WebviewWindowBuilder::limit_navigations_to_app_bound_domains` only on iOS. diff --git a/crates/tauri-runtime-wry/src/lib.rs b/crates/tauri-runtime-wry/src/lib.rs index af7ddf5e77c3..79a787bed597 100644 --- a/crates/tauri-runtime-wry/src/lib.rs +++ b/crates/tauri-runtime-wry/src/lib.rs @@ -4688,6 +4688,15 @@ You may have it installed on another user account, but it is not available for t webview_builder.with_allow_link_preview(webview_attributes.allow_link_preview); } + #[cfg(target_os = "ios")] + { + if webview_attributes.limit_navigations_to_app_bound_domains { + webview_builder = webview_builder.with_limit_navigations_to_app_bound_domains( + webview_attributes.limit_navigations_to_app_bound_domains, + ) + } + } + #[cfg(target_os = "ios")] { if let Some(input_accessory_view_builder) = webview_attributes.input_accessory_view_builder { diff --git a/crates/tauri-runtime/src/webview.rs b/crates/tauri-runtime/src/webview.rs index 330356b39b8b..142a21cca7cc 100644 --- a/crates/tauri-runtime/src/webview.rs +++ b/crates/tauri-runtime/src/webview.rs @@ -255,6 +255,8 @@ pub struct WebviewAttributes { /// This relies on [`objc2_ui_kit`] which does not provide a stable API yet, so it can receive breaking changes in minor releases. #[cfg(target_os = "ios")] pub input_accessory_view_builder: Option, + #[cfg(target_os = "ios")] + pub limit_navigations_to_app_bound_domains: bool, } #[cfg(target_os = "ios")] @@ -360,6 +362,8 @@ impl WebviewAttributes { allow_link_preview: true, #[cfg(target_os = "ios")] input_accessory_view_builder: None, + #[cfg(target_os = "ios")] + limit_navigations_to_app_bound_domains: false, } } @@ -598,6 +602,39 @@ impl WebviewAttributes { self.allow_link_preview = allow_link_preview; self } + /// Whether to limit navigations to App-Bound Domains. This is necessary + /// to enable Service Workers on iOS according to [StackOverflow](https://stackoverflow.com/questions/49673399/service-workers-unavailable-in-wkwebview-in-ios-11-3/64155509#64155509). + /// + /// Note: If you pass in `true` make sure to add the following to Info.plist + /// in the iOS project: + /// ```xml + /// + /// + /// WKAppBoundDomains + /// + /// localhost + /// + /// + /// + /// ``` + /// You should also add any additional domains which your app requests assets from. + /// Assets served through custom protocols like Tauri's IPC are added to the + /// list automatically. Available on iOS only. + /// + /// Default is false. + /// + /// See https://webkit.org/blog/10882/app-bound-domains/ and + /// https://developer.apple.com/documentation/webkit/wkwebviewconfiguration/limitsnavigationstoappbounddomains + /// + /// ## Platform-specific + /// + /// - **Linux / Windows / Android / MacOS:** Unsupported. + #[must_use] + #[cfg(target_os = "ios")] + pub fn limit_navigations_to_app_bound_domains(mut self, limit_navigations: bool) -> Self { + self.limit_navigations_to_app_bound_domains = limit_navigations; + self + } /// Change the default background throttling behavior. /// diff --git a/crates/tauri/src/webview/mod.rs b/crates/tauri/src/webview/mod.rs index a8e29d7cee0e..bd683a9c122d 100644 --- a/crates/tauri/src/webview/mod.rs +++ b/crates/tauri/src/webview/mod.rs @@ -992,6 +992,42 @@ fn main() { .allow_link_preview(allow_link_preview); self } + /// Whether to limit navigations to App-Bound Domains. This is necessary + /// to enable Service Workers on iOS according to [StackOverflow](https://stackoverflow.com/questions/49673399/service-workers-unavailable-in-wkwebview-in-ios-11-3/64155509#64155509). + /// + /// Note: If you pass in `true` make sure to add the following to Info.plist + /// in the iOS project: + /// ```xml + /// + /// + /// WKAppBoundDomains + /// + /// localhost + /// + /// + /// + /// ``` + /// You should also add any additional domains which your app requests assets from. + /// Assets served through custom protocols like Tauri's IPC are added to the + /// list automatically. Available on iOS only. + /// + /// Default is false. + /// + /// See https://webkit.org/blog/10882/app-bound-domains/ and + /// https://developer.apple.com/documentation/webkit/wkwebviewconfiguration/limitsnavigationstoappbounddomains + /// + /// ## Platform-specific + /// + /// - **Linux / Windows / Android / MacOS:** Unsupported. + #[cfg(target_os = "ios")] + #[must_use] + pub fn limit_navigations_to_app_bound_domains(mut self, limit_navigations: bool) -> Self { + // self. + self.webview_attributes = self + .webview_attributes + .limit_navigations_to_app_bound_domains(limit_navigations); + self + } /// Allows overriding the the keyboard accessory view on iOS. /// Returning `None` effectively removes the view. diff --git a/crates/tauri/src/webview/webview_window.rs b/crates/tauri/src/webview/webview_window.rs index b8ba7eebc2a3..6a1f1e4600ef 100644 --- a/crates/tauri/src/webview/webview_window.rs +++ b/crates/tauri/src/webview/webview_window.rs @@ -1177,6 +1177,41 @@ impl> WebviewWindowBuilder<'_, R, M> { .with_input_accessory_view_builder(builder); self } + + /// Whether to limit navigations to App-Bound Domains. This is necessary + /// to enable Service Workers on iOS according to [StackOverflow](https://stackoverflow.com/questions/49673399/service-workers-unavailable-in-wkwebview-in-ios-11-3/64155509#64155509). + /// + /// Note: If you pass in `true` make sure to add the following to Info.plist + /// in the iOS project: + /// ```xml + /// + /// + /// WKAppBoundDomains + /// + /// localhost + /// + /// + /// + /// ``` + /// You should also add any additional domains which your app requests assets from. + /// Assets served through custom protocols like Tauri's IPC are added to the + /// list automatically. Available on iOS only. + /// + /// Default is false. + /// + /// See https://webkit.org/blog/10882/app-bound-domains/ and + /// https://developer.apple.com/documentation/webkit/wkwebviewconfiguration/limitsnavigationstoappbounddomains + /// + /// ## Platform-specific + /// + /// - **Linux / Windows / Android / MacOS:** Unsupported. + #[cfg(target_os = "ios")] + pub fn limit_navigations_to_app_bound_domains(mut self, limit_navigations: bool) -> Self { + self.webview_builder = self + .webview_builder + .limit_navigations_to_app_bound_domains(limit_navigations); + self + } } /// A type that wraps a [`Window`] together with a [`Webview`].