如何在iOS中打开文件? [英] How to open file in iOS?

查看:147
本文介绍了如何在iOS中打开文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在下载后打开.pdf文件,该文件是通过 Alamofire 下载的.但是我只看过使用"webview".因此,该应用程序占用大量内存,并且不可行.

I'm trying to open a .pdf file after download which is downloaded with Alamofire. But I've seen only using a "webview". Thus the application consumes lots of memory and is not viable.

我想要的是使用本机设备应用程序打开它.有什么建议?谢谢.

What I want is to open it with the native device application. Any suggestions? Thank you.

这是我下载文件的代码:

This is my code for download file:

    var localPath: NSURL?

    Alamofire.download(.GET, url, destination: { (temporaryURL, response) in
    let directoryURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
    let pathComponent = response.suggestedFilename
    localPath = directoryURL.URLByAppendingPathComponent(pathComponent!)
    return localPath!
    })
    .response { (request, response, _, error) in

    if error != nil
    {
    // got an error in getting the data, need to handle it
    print("Error: \(error!)")

    }

    //print(response)
    print("Download file en:\(localPath!)")

    self.view.hideToastActivity()
    //self.actioncall()

    }

 }

我需要从本地路径打开文件...

I need open file from localpath...

推荐答案

您应使用UIDocumentInteractionController.您可以在 Apple文档页面上了解有关它的信息.

You should use UIDocumentInteractionController. You can read about it on this Apple documentation page.

通过执行谷歌搜索,您甚至应该会看到一些示例实现.例如,此处,您可以请参阅" mattneub "完成的一些代码.

By doing some Googling you should see even some example implementations. For example here you can see some code about this done by "mattneub".

我再给你一个你可以使用的代码:

I let you one more code that you can use:

var documentInteractionController: UIDocumentInteractionController!

@IBAction func openDocument(sender: UIButton) {
  let URL: NSURL = NSBundle.mainBundle().URLForResource("yourPDF", withExtension: "pdf")!

    if (URL != "") {
        // Initialize Document Interaction Controller
        self.documentInteractionController = UIDocumentInteractionController(URL: URL)

        // Configure Document Interaction Controller
        self.documentInteractionController.delegate = self

        // Present Open In Menu
        self.documentInteractionController.presentOptionsMenuFromRect(sender.frame, inView: self.view, animated: true)
            //presentOpenInMenuFromRect
    }
}

// UIDocumentInteractionControllerDelegate
func documentInteractionControllerViewControllerForPreview(controller: UIDocumentInteractionController) -> UIViewController {
    return self
}

这篇关于如何在iOS中打开文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆