语言: Objective-C


iOS网页浏览中链接预览的自定义iOS 9预览操作
Custom iOS 9 preview actions for link previews in iOS webviews (中文)


在iOS 9中,UIWebView和WKWebView可以支持链接上的3D触摸,通过在Web视图上将allowsLinkPreview设置为YES,使用SFSafariViewController预览链接的URL。但是,目前没有办法向SFSafariViewController提供一组自定义操作,因此,UIPreviewActionItem列表始终为“打开”,“添加到读取列表”和“复制”。

此项目是一个概念验证,提供自定义UIPreviewActionItems以使用SFSafariViewController链接预览。这种方法有点复杂,因为用于预览链接的钩子在Web视图中,并且开发人员永远不会被提供钩子来提供他们自己的数据。解决方案是在SFSafariViewController上创建一个类别,覆盖 - (NSArray <id <UIPreviewActionItem >> *)previewActionItems实例方法,并从SFSafariViewController的委托中提取URL。 SFSafariViewControllerDelegate是DDURLPreviewAction的一个实例,它响应@selector(url)。代码以不使用私有API的方式编写,如果内部工作在将来的版本中发生更改,则会优雅地失败。



本文使用googletrans自动翻译,仅供参考, 原文来自


In iOS 9, UIWebView and WKWebView can support 3D touches on links to preview the linked URL with SFSafariViewController by setting the allowsLinkPreview to YES on the web view. However, there isn't currently a way to provide a set of custom actions to SFSafariViewController and, as such, the UIPreviewActionItem list is always "Open", "Add to Reading List", and "Copy".

This project is a proof-of-concept to provide custom UIPreviewActionItems to link previews using SFSafariViewController. The approach is somewhat convoluted because the hooks for previewing the link are within the web views, and the developer is never given hooks to provide their own data. The solution is to create a category on SFSafariViewController, override - (NSArray<id<UIPreviewActionItem>> *)previewActionItems instance method, and pull the URL from the SFSafariViewController's delegate. The SFSafariViewControllerDelegate is an instance of DDURLPreviewAction, which responds to @selector(url). The code is written in such a way to not use private APIs, and fail gracefully should the internal workings change in a future release.

Here's a screenshot of the result: