-1
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()
            })
        })
    }

Save PDF file to iBooks was working fine until upgrade to xCode7. Now getting an error: Downcast from String? to String only unwraps optionals... for this line:

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

Not sure how to fix that. Tried:

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

But got new error. Any help would be appreciated.

Octavious
  • 11
  • 3

1 Answers1

0

Try this:

    let documentsDirPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as String!
Danilo M.
  • 91
  • 1
  • 1