gpt4 book ai didi

ios - 如何使用 turbolinks-ios 打开链接的 pdf

转载 作者:可可西里 更新时间:2023-11-01 00:58:36 27 4
gpt4 key购买 nike

我想知道如何使用 turbolinks-ios 打开链接的 pdf 文件iOS 中的框架。

目前,我正在经历 issue当 turbolinks 页面链接到 pdf 或其他文件时,链接将在 safari 而不是嵌入式 View 中打开。

背景

turbolinks-5图书馆连同turbolinks-ios framework提供一种将 Web 应用程序连接到相应移动应用程序的 native 导航 Controller 的方法。

screenshot

截图取自turbolinks README .

期望的行为

当单击引用 pdf 的链接时,应将单独的 View Controller 推送到当前导航 Controller ,以便用户可以阅读 pdf 并轻松导航回文档索引。

观察到的行为

链接的 pdf 在 safari 中打开,而不是在应用程序中打开。不幸的是,safari 再次要求进行身份验证。此外,用户必须离开应用程序。

最佳答案

拦截pdf链接的点击

对于 pdf 文件的链接,didProposeVisitToURL mechanism不会为 session 委托(delegate)触发。因此,无法从那里决定如何处理链接的 pdf。

相反,可以通过成为 turbolinks 的 web View 的导航委托(delegate)作为 shown in the README 来拦截点击链接。 :

extension NavigationController: SessionDelegate {
// ...

func sessionDidLoadWebView(session: Session) {
session.webView.navigationDelegate = self
}
}

extension NavigationController: WKNavigationDelegate {
func webView(webView: WKWebView,
decidePolicyForNavigationAction navigationAction: WKNavigationAction,
decisionHandler: (WKNavigationActionPolicy) -> ()) {

// This method is called whenever the webView within the
// visitableView attempts a navigation action. By default, the
// navigation has to be cancelled, since when clicking a
// turbolinks link, the content is shown in a **new**
// visitableView.
//
// But there are exceptions: When clicking on a PDF, which
// is not handled by turbolinks, we have to handle showing
// the pdf manually.
//
// We can't just allow the navigation since this would not
// create a new visitable controller, i.e. there would be
// no back button to the documents index. Therefore, we have
// to create a new view controller manually.

let url = navigationAction.request.URL!

if url.pathExtension == "pdf" {
presentPdfViewController(url)
}

decisionHandler(WKNavigationActionPolicy.Cancel)
}
}

呈现pdf View Controller

类似于将可访问 View 呈现为 shown in the turbolinks-ios demo application , 呈现 pdf View Controller :

extension NavigationController {
func presentPdfViewController(url: NSURL) {
let pdfViewController = PdfViewController(URL: url)
pushViewController(pdfViewController, animated: true)
}
}

或者,如果您还想显示其他文件类型,请将其命名为 fileViewController 而不是 pdfViewController

PdfViewController

新的 View Controller 继承自 turbolinks 的 VisitableViewController通过 url 使用初始化。

class PdfViewController: FileViewController {
}

class FileViewController: Turbolinks.VisitableViewController {
lazy var fileView: WKWebView = {
return WKWebView(frame: CGRectZero)
}()

lazy var filename: String? = {
return self.visitableURL?.pathComponents?.last
}()

override func viewDidLoad() {
view.addSubview(fileView)
fileView.bindFrameToSuperviewBounds() // https://stackoverflow.com/a/32824659/2066546
self.title = filename // https://stackoverflow.com/a/39022302/2066546
fileView.loadRequest(NSURLRequest(URL: visitableURL))
}
}

为了使 WebView 的大小正确,我使用了 bindFrameToSuperviewBounds as shown in this stackoverflow answer ,但我确信还有其他方法。

可选:共享 cookie

如果加载 pdf 需要身份验证,可以方便地将 cookie 与 turbolinks-ios webview 共享为 described in the README .

例如,创建一个可以传递给 pdfViewControllerwebViewConfiguration:

extension NavigationController {
let webViewProcessPool = WKProcessPool()

lazy var webViewConfiguration: WKWebViewConfiguration = {
let configuration = WKWebViewConfiguration()
configuration.processPool = self.webViewProcessPool
// ...
return configuration
}()

lazy var session: Session = {
let session = Session(webViewConfiguration: self.webViewConfiguration)
session.delegate = self
return session
}()
}

需要将相同的 webViewConfiguration 传递给 session(如上所示)以及新的 pdf View Controller 。

extension NavigationController {
func presentPdfViewController(url: NSURL) {
let pdfViewController = PdfViewController(URL: url)
pdfViewController.webViewConfiguration = self.webViewConfiguration
pushViewController(pdfViewController, animated: true)
}
}

class FileViewController: Turbolinks.VisitableViewController {
var webViewConfiguration: WKWebViewConfiguration

lazy var fileView: WKWebView = {
return WKWebView(frame: CGRectZero, configuration: self.webViewConfiguration)
}()

// ...
}

演示

Screenshot

关于ios - 如何使用 turbolinks-ios 打开链接的 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39024375/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com