IOS 11 中的 WebKit 和 Safari 更改WebKit and Safari changes in iOS 11

iOS 11 引入了新版本的 Safari web 浏览器– Safari 11.0 –其中包括对 WebKit 和 SafariServices 的更改。iOS 11 introduces a new version of the Safari web browser – Safari 11.0 – which includes changes to WebKit and SafariServices. 本指南将探讨这些更改。This guide explores these changes.


在 iOS 9 中引入 SFSafariViewController 作为显示 web 内容或从应用对用户进行身份验证的选项。SFSafariViewController was introduced in iOS 9 as an option for displaying web content or authenticating users from your app. 有关其功能的详细信息,请参阅Web 视图指南。More information on its features can be found in the Web Views guide.

iOS 11 引入了对 Safari 视图控制器的样式更新,使用户能够在应用程序和 web 之间获得更流畅的体验。iOS 11 has introduced style updates to the Safari View Controller, giving your users a more seamless experience between an app and the web. 例如,删除地址栏现在为 Safari 视图控制器提供应用内浏览器的外观,而不是小型浏览器。For example, the removal of the address bar now gives the Safari View Controller the feel of an in-app browser, rather than a mini-browser. 还可以通过设置 "preferredBarTintColor" 和 "PreferredControlTintColor" 属性,自定义颜色方案,使其适合应用的配色方案:You can also customize the color scheme to fit in with the color scheme of your app by setting the preferredBarTintColor and PreferredControlTintColor properties:

sfViewController.PreferredControlTintColor = UIColor.White;
sfViewController.PreferredBarTintColor = UIColor.Purple;

下面的代码段以紫色和白色呈现条形,如下图所示:The following code snippet renders the bars in purple and white, as displayed in the following image:

以紫色和白色呈现的 SFSafariViewController 栏

还可以通过将 "DismissButtonStyle" 属性设置为 "Done"、"Close" 或 "Cancel" 来更改 Safari 视图控制器中显示的 "解除" 按钮:The Dismiss button presented in the Safari View Controller can also be changed by setting the DismissButtonStyle property to either Done, Close, or Cancel:

sfViewController.DismissButtonStyle = SFSafariViewControllerDismissButtonStyle.Close;


显示 SFSafariViewController 时,可以更改此值。This value can be changed while SFSafariViewController is presented.

根据在 Safari 视图控制器内显示的内容,可能需要确保在用户滚动时菜单栏不会折叠。Depending on the content that is displayed inside a Safari View Controller, it might be necessary to ensure that the menu bars don't collapse as the user scrolls. 为此,可将新的 BarCollapsedEnabled 属性设置为 falseThis is enabled by setting the new BarCollapsedEnabled property to false:

var config = new SFSafariViewControllerConfiguration();
config.BarCollapsingEnabled = false;

var sfViewController = new SFSafariViewController(url, config);


Apple 还在 iOS 11 中更新了 Safari 视图控制器中的隐私。Apple has also made updates to privacy in the Safari View Controller in iOS 11. 现在,浏览 cookie 和本地存储等数据仅存在于每个应用程序的基础上,而不是在 Safari 视图控制器的所有实例中存在。Now, browsing data such as cookies and local storage only exist on a per-app basis, rather than across all instances of Safari view controller. 这会使用户浏览活动在应用中保持私有。This keeps user browsing activity private within your app.

其他功能(如对 Url 的拖放支持以及对 的支持也已添加到 iOS 11 SFSafariViewControllerAdditional features such as drag and drop support for URLs and support for have also been added to SFSafariViewController in iOS 11. 可在Apple SFSafariViewController 文档中找到有关这些新功能的详细信息。You can find more information about these new features in Apple's SFSafariViewController documentation.


WKWebView 在 iOS 8 中作为 WebKit 的一部分引入,作为向用户显示 web 内容的一种方式。WKWebView was introduced as part of WebKit in iOS 8 as a means of displaying web content to your user. SFSafariViewController相比,它的自定义性要多得多,因此您可以创建自己的导航界面和用户界面。It's much more customizable than SFSafariViewController, allowing you to create your own navigation and user interface.

Apple 在 iOS 11 中引入了三项 WKWebView 的主要改进:Apple has introduced three main improvements for WKWebView with iOS 11:

  • 管理 cookie 的功能The ability to manage cookies
  • 内容筛选Content filtering
  • 自定义资源加载。Custom resource loading.

Cookie 管理通过新的WKHttpCookieStore类完成,此类允许你添加和删除 cookie,获取存储在 WKWebView 中的所有 cookie,并观察 cookie 存储区中的更改。Cookie management is done through the new WKHttpCookieStore class, which allows you to add and delete cookies, to get all the cookies stored in a WKWebView, and to observe the cookie store for changes.

内容筛选使你能够管理你的用户将看到的内容类型,使你可以确保它是安全的,并且在必要的情况下,仅供一组选择的用户使用。Content filtering allows you to manage the type of content that your user will see, allowing you to make sure it's secure, family friendly, and, if necessary, only available to a select group of users. 这是通过新的WKContentRuleList类实现的,通过提供 JSON 中的触发器和操作对。This is implemented through the new WKContentRuleList class, by providing pairs of triggers and actions in JSON. 有关这些触发器和操作的详细信息,请参阅 Apple 的内容阻止规则指南。More information on these triggers and actions can be found in Apple's Content Blocking Rules guide.

iOS 11 现在允许你自定义 WKWebView,并为 web 内容自定义资源加载。iOS 11 now allows you to customize WKWebView with custom resource loading for your web content. 这是通过 IWKUrlSchemeHandler 接口实现的,它使你能够处理不是本机到 Web 工具包的 URL 方案。This is implemented through the IWKUrlSchemeHandler interface, which lets you handle URL Schemes that are not native to Web Kit. 此接口具有必须实现的启动和停止方法:This interface has a start and stop method that must be implemented:

public class MyHandler : NSObject, IWKUrlSchemeHandler {

    public void StartUrlSchemeTask(WKWebView webView, IWKUrlSchemeTask urlSchemeTask){
        // Implement a IWKUrlSchemeTask here
        var response = new NSUrlResponse(urlSchemeTask.Request.Url, "text/html", ContentLength, null);

    public void StopUrlSchemeTask(WKWebView webView, IWKUrlSchemeTask urlSchemeTask){
        throw new NotImplementedException();


实现该处理程序后,使用它来设置 WKWebViewConfiguration上的 SetUrlSchemeHandler 属性。Once the handler has been implemented, use it to set the SetUrlSchemeHandler property on the WKWebViewConfiguration. 然后,加载使用自定义方案的某些内容的 URL:Then, load the URL of something that uses the custom scheme:

var config = new WKWebViewConfiguration();
config.SetUrlSchemeHandler(new MyHandler(), "xamarin-asset");

webView = new WKWebView (View.Frame, config);
webView.LoadRequest (new NSUrlRequest("xamarin-asset://"));