First of all, NSData is your PDF file, there is no need to convert it to another data type or use the library to manage it at the moment.
Now just save the NSData in the document directory on iOS, which is not a managed persistent storage such as CoreData or Realm, and is not UserDefaults. This is a folder with things 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 check if there is a file and open it on your Mac. This is to ensure that you save the actual PDF file, not something else. This is a two-step process. First, find out where, in fact, the file went by printing the location of the document folder from the iOS simulator:
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) let documentsDirectory = paths[0] print(documentsDirectory)
Now copy this really long path to the file and cd [paste location] to the terminal to go there, then open myCoolPDF.pdf to open it in Preview! These are magical times!
Now that you have confirmed that you are dealing with an actual PDF file, it’s time to display it in a WebView, as it’s pretty simple if you don’t have your own way of doing it.
Note that you still need to make the web view appear, drag it onto your viewController into the storyboard and make it an IBOutlet.
let url = NSURL(fileURLWithPath: filePath) let webView = UIWebView() webView.loadRequest(NSURLRequest(URL: url))
Obviously, this is a quick way to make sure that you are based on the basic principles, you don’t want to force the deployment to be disabled using ! .