How to show PDF from NSData in Swift - how to save

2020-07-06 07:13发布

I have a PDF that I'm pulling from a sever that came in the form of NSData and now I need to display it. I've looked far and wide for a solution to this and I haven't found anything that bridges the gap between raw NSData and actually saving or showing the PDF.

I tried this but now I'm stuck not knowing how to save or show:

let cfData = CFDataCreate(kCFAllocatorDefault, UnsafePointer<UInt8>(data.bytes), data.length)                                                
let cgDataProvider = CGDataProviderCreateWithCFData(cfData)
let cgPDFDocument = CGPDFDocumentCreateWithProvider(cgDataProvider)

标签: swift pdf
3条回答
冷血范
2楼-- · 2020-07-06 07:30

First of all, that NSData IS your PDF, no need to convert to another data type or use a library to manipulate it at the moment.

For now, simply save the NSData to your documents directory on iOS, which is not a managed persistent store like CoreData or Realm, nor is it UserDefaults. It's a folder with stuff in it.

Save to Documents Folder:

let data = //the stuff from your web request or other method of getting pdf    
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
let filePath = "\(documentsPath)/myCoolPDF.pdf"
data.writeToFile(filePath, atomically: true)

Now verify that the file is there and open it on your mac. This is to make sure you've saved an actual PDF and not anything else. This is a two step process. First find out where on earth the file went by printing the location of the documents folder from the iOS simulator:

let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory = paths[0]
print(documentsDirectory)

Now copy that really long filepath and cd [paste location] in terminal to go there, then open myCoolPDF.pdf to open it in Preview! It's magical times!

Now that you have verified that you're dealing with an actual PDF it's time to display that in a WebView since it's quite simple unless you've got your own way of doing so.

Note that you still have to make it so the webview shows up, drag one onto your viewController in a storyboard and make an IBOutlet for it.

let url = NSURL(fileURLWithPath: filePath)
let webView = UIWebView()
webView.loadRequest(NSURLRequest(URL: url))

Obviously this is a quick way to make sure you get the basics going, you don't want to force unwrap unsafely using !.

查看更多
Explosion°爆炸
3楼-- · 2020-07-06 07:42

Update:

  • Xcode 8.1
  • Swift 3.0.1

For Saving Files to Documents Directory.

let documentPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let path = "\(documentPath)/filename.pdf"
yourData.write(toFile: path, atomically: true)

For retrieving the path of Document Directory.

let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = paths[0]

Here you can append your filename with backslash in front. And you can test whether your file exists there.

查看更多
【Aperson】
4楼-- · 2020-07-06 07:51

Starting with iOS 11 you don't need a web view and can use a dedicated PDFKit framework. With PDFKit loading a PDF from a URL or Data instance can be as simple as:

import PDFKit

// some other code here
// ...

let pdfView = PDFView()

// add pdfView to the view hierarchy and possibly add auto-layout constraints
// ...

pdfView.document = PDFDocument(data: data)

Check out PDFKit documentation for more details.

查看更多
登录 后发表回答