gpt4 book ai didi

ios - WKWebView 将 pdf 保存到 ibooks,从链接保存 pdf

转载 作者:可可西里 更新时间:2023-11-01 02:12:54 25 4
gpt4 key购买 nike

我是 Junior,目前正在 WKWebView 中的项目中工作,并且有打开 pdf 的链接。我可以在 Safari 中打开它,然后在 iBooks 中打开,但我希望它在我的应用程序中进行。可能吗 ?

下面是图片:

我可以选择pdf的图片

description1

想象一下它会打开什么

description2我的 WebView 类

class WebVC: UIViewController, WKUIDelegate {



var webView: WKWebView!

override func viewDidLoad() {
super.viewDidLoad()
let myURL = NSURL(string: "\(savedURL!)")
let myRequest = URLRequest(url: myURL! as URL)
webView.load(myRequest)
webView.allowsBackForwardNavigationGestures = true
webView.allowsLinkPreview = false
}

override func viewWillAppear(_ animated: Bool) {
self.navigationController?.toolbar.isHidden = false
}

override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: CGRect(x: 100, y: 100, width: 110, height: 110), configuration: webConfiguration)
webView.uiDelegate = self
view = webView

}

@IBAction func logoutPressed(_ sender: AnyObject) {
defaults.set(false, forKey: "isLogged")
defaults.set("EMPTY URL", forKey: "savedURL")
_ = self.navigationController?.popToRootViewController(animated: true)
}


@IBAction func goBack(_ sender: Any?) {
if (self.webView.canGoBack) {
self.webView.goBack()
}
}

}

最佳答案

  • 在webview中打开pdf

    //第一步

    webview.uiDelegate = self

    //第 2 步 - 实现委托(delegate)函数(这个函数可以让你在 webview 中打开任何点击的 pdf)

    func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {

    // open in current view
    webView.load(navigationAction.request)
    // don't return a new view to build a popup into (the default behavior).
    return nil;
    }
  • 下载

在我的例子中,一旦我在 webview 中打开了 pdf,我就有了下载按钮,我用它来下载当前打开的 pdf

//第三步 - 委托(delegate)函数

    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
decisionHandler(WKNavigationActionPolicy.allow)
}
//find the mimeTime of the current url opened in webview, If it's pdf, we'll give option to download
func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {

if let mimeType = navigationResponse.response.mimeType {
//check if mime exists in our acceptable mimes list
if self.isMimeTypeConfigured(mimeType) {
self.mime = mimeType
self.currentUrl = navigationResponse.response.url
addDownloadButton()
}
}
decisionHandler(.allow)
}

//第 4 步 - 创建带有辅助函数的扩展

//=== PDF downloading ===
struct MimeType {
var type:String
var fileExtension:String
}
extension DownloadManual {
//button
private func addDownloadButton(){
let btn = UIBarButtonItem(image: UIImage(systemName: "tray.and.arrow.down.fill"), style: .plain, target: nil, action: #selector(downloadTapped))
self.navigationItem.rightBarButtonItem = btn
}
@objc func downloadTapped(){
self.showActivityIndicator(show: true)
if let url = currentUrl {
print("download from: \(url)")
let filename = getDefaultFileName(forMimeType: self.mime)

downloadData(fromURL: url, fileName: filename) { success, destinationURL in
if success, let destinationURL = destinationURL {

self.showActivityIndicator(show: false)
print("download result: \(success), \(destinationURL)")
self.fileDownloadedAtURL(url: destinationURL)
}
}
}
}

//helper funcs
private func isMimeTypeConfigured(_ mimeType:String) -> Bool {
for record in self.mimeTypes {
if mimeType.contains(record.type) {
return true
}
}
return false
}

func fileDownloadedAtURL(url: URL) {
print("downloaded at: \(url)")
DispatchQueue.main.async {
let activityVC = UIActivityViewController(activityItems: [url], applicationActivities: nil)
activityVC.popoverPresentationController?.sourceView = self.view
activityVC.popoverPresentationController?.sourceRect = self.view.frame
activityVC.popoverPresentationController?.barButtonItem = self.navigationItem.rightBarButtonItem
self.present(activityVC, animated: true, completion: nil)
}
}
private func downloadData(fromURL url:URL,
fileName:String,
completion:@escaping (Bool, URL?) -> Void) {
webview.configuration.websiteDataStore.httpCookieStore.getAllCookies() { cookies in
let session = URLSession.shared
print("downloading ....")
session.configuration.httpCookieStorage?.setCookies(cookies, for: url, mainDocumentURL: nil)
let task = session.downloadTask(with: url) { localURL, urlResponse, error in
if let localURL = localURL {
let destinationURL = self.moveDownloadedFile(url: localURL, fileName: fileName)
completion(true, destinationURL)
}
else {
completion(false, nil)
}
}

task.resume()
}
}
private func getDefaultFileName(forMimeType mimeType:String) -> String {
for record in self.mimeTypes {
if mimeType.contains(record.type) {
return "default." + record.fileExtension
}
}
return "default"
}

private func moveDownloadedFile(url:URL, fileName:String) -> URL {
let tempDir = NSTemporaryDirectory()
let destinationPath = tempDir + fileName
let destinationURL = URL(fileURLWithPath: destinationPath)
try? FileManager.default.removeItem(at: destinationURL)
try? FileManager.default.moveItem(at: url, to: destinationURL)
return destinationURL
}

希望这对某人有帮助:)

关于ios - WKWebView 将 pdf 保存到 ibooks,从链接保存 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40491077/

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