使用xCode7将PDF保存到iBooks问题

时间:2015-09-25 12:18:28

标签: pdf ibooks

func downloadPDF() {
        // Running operations that takes a long time in a background thread is recommended
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { () -> Void in
            // Get the PDF data from the URL
            let url = self.webview.request?.URL
            let pdfURL = url?.absoluteString
            let pdfData = NSData(contentsOfURL: NSURL(string: pdfURL!)!)!

            // Store the data locally as a PDF file in the Documents directory
            let documentsDirPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as? String
            localPdfPath = documentsDirPath.stringByAppendingPathComponent(pdfURL!.lastPathComponent)
            pdfData.writeToFile(localPdfPath, atomically: true)

            // UI related stuff should be called in the main thread.
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                self.openIniBooks()
                self.stopActivityIndicator()
            })
        })
    }

将PDF文件保存到iBooks工作正常,直到升级到xCode7。现在收到错误: 来自String的Downcast? to String只展开选项... 这一行:

let documentsDirPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as? String

不确定如何解决这个问题。尝试:

let documentsDirPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String

但得到了新的错误。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

试试这个:

    let documentsDirPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as String!