Xamarin 中的 Web 视图Web Views in Xamarin.iOS

在 iOS 的生存期内,应用开发人员可以通过多种方式在其应用中合并 web 视图功能。Over the lifetime of iOS Apple has released a number of ways for app developers to incorporate web view functionality in their apps. 大多数用户都利用其 iOS 设备上的内置 Safari web 浏览器,因此,从其他应用程序获得的 web 视图功能与此体验一致。Most users utilize the built-in Safari web browser on their iOS device, and therefore expect that web-view functionality from other apps is consistent with this experience. 它们期望使用相同的笔势,性能应为 par,并具有相同的功能。They expect the same gestures to work, the performance to be on par, and the functionality the same.

在本文中,我们将探讨 Apple 提供的三个 Web 视图(UIWebViewWKWebviewSFSafariViewController,它们之间的相似性和差异,以及如何使用它们。In this article, we will explore each of the three Web Views provided by Apple: UIWebView, WKWebview, and SFSafariViewController, their similarities and differences, and how they can be used.

iOS 11 引入了对 WKWebViewSFSafariViewController的新更改。iOS 11 introduced new changes to both WKWebView and SFSafariViewController. 有关这些内容的详细信息,请参阅iOS 11 指南中的 Web 更改For more information on these, see the Web changes in iOS 11 guide guide.

UIWebViewUIWebView

UIWebView 是 Apple 在您的应用程序中提供 web 内容的传统方法。UIWebView is Apple's legacy way of providing web content in your app. 它已在 iOS 2.0 中发布,并已在8.0 后弃用。It was released in iOS 2.0, and has been deprecated as of 8.0.

如果计划支持早于8.0 的 iOS 版本,则必须使用 UIWebViewIf you plan to support iOS versions earlier than 8.0, you will have to use UIWebView. 由于 UIWebView 不如替代方法优化性能,因此,建议您检查用户的 iOS 版本。Due to the fact that UIWebView is less optimized for performance than the alternatives, it is recommended that you should check the user's iOS version. 如果8.0 或更高版本,使用下面的其中一个选项将创建更好的用户体验。If it 8.0 or above, using either of the options explain below will create a better user experience.

若要将 UIWebView 添加到你的 Xamarin iOS 应用,请使用以下代码:To add a UIWebView to your Xamarin.iOS app, use the following code:

webView = new UIWebView (View.Bounds);
View.AddSubview(webView);

var url = "https://xamarin.com"; // NOTE: https secure request
webView.LoadRequest(new NSUrlRequest(new NSUrl(url)));

这会生成以下 web 视图:This produces the following web view:

有关使用 UIWebView的详细信息,请参阅以下食谱:For more information on using UIWebView, refer to the following recipes:

WKWebViewWKWebView

iOS 8 中引入了 WKWebView,应用程序开发人员可以实现类似于 mobile Safari 的 web 浏览界面。WKWebView was introduced in iOS 8 allowing app developers to implement a web browsing interface similar to that of mobile Safari. 这在某种程度上是因为 WKWebView 使用 Nitro Javascript 引擎,这是移动 Safari 使用的同一个引擎。This is due, in part, to the fact that WKWebView uses the Nitro Javascript engine, the same engine used by mobile Safari. 应始终在 UIWebView 上使用 WKWebView,这是因为提高了性能、内置用户友好的手势,以及网页与应用之间的轻松交互。WKWebView should always be used over UIWebView were possible due to the increased performance,built in user-friendly gestures, and the ease of interaction between the web page and your app.

可以通过与 UIWebView 几乎完全相同的方式将 WKWebView 添加到应用程序中,但作为开发人员,你可以更好地控制 UI/UX 和功能。WKWebView can be added to your app in an almost identical way to UIWebView, however as the developer you have much more control over the UI/UX and functionality. 创建和显示 web 视图对象将显示请求的页面,但你可以控制视图的显示方式、用户可以导航的方式,以及用户如何退出视图。Creating and displaying the web view object will display the requested page, however you can control how the view is presented, how the user can navigate, and how the user exits the view.

下面的代码可用于在 Xamarin iOS 应用程序中启动 WKWebViewThe code below can be used to launch a WKWebView in your Xamarin.iOS app:

    WKWebView webView = new WKWebView(View.Frame, new WKWebViewConfiguration());
    View.AddSubview(webView);

    var url = new NSUrl("https://xamarin.com");
    var request = new NSUrlRequest(url);
    webView.LoadRequest(request);

这会生成以下 web 视图:This produces the following web view:

请注意,WKWebView 位于 WebKit 命名空间中,因此必须将此 using 指令添加到类的顶部。It is important to note that WKWebView is in the WebKit namespace, so you will have to add this using directive to the top of your class.

还可在 Xamarin 应用程序中使用 WKWebView,因此,如果要创建跨平台 Mac/iOS 应用,则可能需要考虑使用该应用。WKWebView can also be used within Xamarin.Mac apps, and you therefore may want to consider using it if you are creating a cross-platform Mac/iOS app.

处理 Javascript 警报食谱还提供有关将 WKWebView 与 JavaScript 一起使用的信息The Handle JavaScript Alerts recipe also provides information on using WKWebView with Javascript

SFSafariViewControllerSFSafariViewController

SFSafariViewController 是提供应用程序提供的 web 内容的最新方式,可在 iOS 9 及更高版本中使用。SFSafariViewController is the latest way to provide web content from your app and is available in iOS 9 and later. UIWebViewWKWebView不同,SFSafariViewController 是一个视图控制器,因此不能与其他视图一起使用。Unlike UIWebView or WKWebView, SFSafariViewController is a View Controller and so cannot be used with other views. 你应以新的视图控制器形式显示 SFSafariViewController,其方式与显示任何视图控制器的方式相同。You should present SFSafariViewController as a new View Controller, in the same way you would present any View Controller.

SFSafariViewController 本质上是可以嵌入到应用中的 "小型 safari"。SFSafariViewController is essentially a 'mini safari' that can be embedded into your app. 与 WKWebView 一样,它使用相同的 Nitro Javascript 引擎,还提供了一系列附加的 Safari 功能,如自动填充、读者,以及在移动 Safari 中共享 cookie 和数据的功能。Like WKWebView it uses the same Nitro Javascript Engine, but also provides a range of additional Safari features such as AutoFill, Reader, and the ability to share cookies and data with mobile Safari. 您的应用程序不能访问用户与 SFSafariViewController 之间的交互。Interaction between the user and the SFSafariViewController is not accessible to your app. 您的应用程序将不能访问任何默认的 Safari 功能。Your app will not have access to any of the default Safari features.

默认情况下,它还实现 "完成" 按钮,使用户能够轻松返回到你的应用程序、前进和后退导航按钮,使用户能够在网页堆栈中导航。It also, by default, implements a Done button, allowing to user to easily return to your app, and forward and back navigation buttons, allowing your user to navigate through a stack of web pages. 此外,它还为用户提供了一个地址栏,使他们可以放心地使用它们。In addition, it also provides the user with an address bar giving them the peace of mind that they are on the expected web page. 地址栏不允许用户更改 url。The address bar does not allow the user to change the url.

这些实现无法更改,因此,如果您的应用程序想要在不进行任何自定义的情况下显示网页,则最好将其用作默认浏览器 SFSafariViewControllerThese implementations cannot be changed, so SFSafariViewController is ideal to use as the default browser if your app wants to present a webpage without any customization.

下面的代码可用于在 Xamarin iOS 应用程序中启动 SFSafariViewControllerThe code below can be used to launch a SFSafariViewController in your Xamarin.iOS app:

var sfViewController = new SFSafariViewController(url);

PresentViewController(sfViewController, true, null);

这会生成以下 web 视图:This produces the following web view:

SafariSafari

还可以通过使用以下代码,从应用内打开移动 Safari 应用:It is also possible to open the mobile Safari app from within your app, by using the code below:

var url = new NSUrl("https://xamarin.com");

UIApplication.SharedApplication.OpenUrl(url);

这会生成以下 web 视图:This produces the following web view:

通常应始终避免将用户从应用导航到 Safari。Navigating users away from your app to Safari should generally always be avoided. 大多数用户不需要在应用程序外导航,因此,如果离开应用,用户可能永远都不会返回它,实质上是终止了订婚。Most users will not expect navigation outside of your application, so if you navigate away from your app, users may never return it, essentially killing engagement.

iOS 9 改进使用户可以通过 Safari 页面左上角提供的 "后退" 按钮轻松返回到应用。iOS 9 improvements allow the user to easily return to your app through a back button that is provided in the top left corner of the Safari page.

应用传输安全性App Transport Security

应用传输安全性或ATS是由 Apple 在 iOS 9 中引入的,以确保所有 internet 通信都符合安全连接最佳实践。App Transport Security, or ATS was introduced by Apple in iOS 9 to ensure that all internet communications conform to secure connection best practices.

有关 ATS 的详细信息,包括如何在应用中实现它,请参阅应用传输安全指南。For more information on ATS, including how to implement it in your app, refer to the App Transport Security guide.