当用户强行或长按链接时,我试图使用LinkPresentation API在UIContextualMenu内显示网站的预览,这类似于偷窥和弹出操作链接的方式。但是,LP API异步地从网站加载元数据,当我强制触摸链接时,上下文菜单显示的预览控制器为空白。以下是UIContextMenuConfiguration的委托方法:
public func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
guard let url = shouldShowContextualMenu(location: location) else { return nil }
return UIContextMenuConfiguration(identifier: nil, previewProvider: { () -> UIViewController? in
self.getMetadataForUrl(url: url ) { linkView in
self.previewController = LinkPreviewViewController(linkView: linkView)
}
return self.previewController
}, actionProvider: nil)
}