当PDF大小很大时,UIDocumentInteractionController下载文件失败

时间:2016-11-16 15:58:18

标签: ios iphone swift

我使用以下代码从我的swift应用程序下载并显示PDF:

var docController:UIDocumentInteractionController!
let pdfUrl = NSURL(string: "ENTER_URL_OF_PDF")

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    downloadDoc(pdfUrl: pdfUrl!)
}

@IBAction func buttonAction(_ sender: AnyObject) {
    docController.presentOptionsMenu(from: self.view.frame, in: self.view, animated: true)
}

func downloadDoc(pdfUrl : NSURL) {
    let urlTest = self.pdfUrl!.absoluteString
    let pdfUrl = NSURL(string: urlTest!)
    if(pdfUrl != nil){
        let pdfRequest: NSURLRequest = NSURLRequest(url: pdfUrl! as URL)
         NSURLConnection.sendAsynchronousRequest(pdfRequest as URLRequest, queue: OperationQueue.main) {(response, data, error) in
            let httpResponse = response as? HTTPURLResponse
            if(httpResponse?.statusCode == 200 && error == nil){
               let documentsUrl =  FileManager.default.urls(for: FileManager.SearchPathDirectory.documentDirectory, in: FileManager.SearchPathDomainMask.userDomainMask).first as! NSURL

               if let fileName = self.pdfUrl!.lastPathComponent {
                  let destinationUrl = documentsUrl.appendingPathComponent(fileName)
                  if let data = data {
                        do {
                            try data.write(to: destinationUrl!, options: .atomic)
                        } catch {
                            print(error)
                        }
                        self.docController = UIDocumentInteractionController(url: destinationUrl!)
                  }
               }

            }

        }

    }

}

我面临的问题是,当我尝试使用包含3页以上的PDF时,我收到了此错误:

  

致命错误:在解包可选值时意外发现nil

我的应用程序崩溃了。当我搜索可以下载的最大尺寸时,我在man上写了SO帖子,没有限制! 那我为什么会遇到这个问题?

0 个答案:

没有答案