Instantiate a Download from a Remote URL
PSPDFKit usually works best with PDF documents on the local file system of your device. There are several reasons for using local file system documents (these include performance, cache control, and battery impact).
However, if you have a PDF document that isn’t on the local file system, you can also instantiate a Document
(opens in a new tab) with a remote URL via Document(url:)
(opens in a new tab):
let document = Document(url: URL(string: "https://github.com/PSPDFKit/pspdfkit-ios-catalog/raw/master/Samples/Magazine.pdf")!)let controller = PDFViewController(document: document)
This will download the remote document and cache it. If you’re interested in how this works exactly, check out our blog post about it. You can also disable caching by doing the following:
// By default, downloads use this custom large cache to ensure files are being updated correctly.// Set the cache to `nil` to disable this caching.URLDataProvider.cache = nil
// Ensure the temporary file does not exist.try? FileManager.default.removeItem(at: URLDataProvider.defaultTargetURL(for: url)!)
Files downloaded from a remote URL using this method are read-only by default, and annotations cannot be saved, as the source file could change at any time.
Refer to RemoteDocumentURLExample.swift
(opens in a new tab) in the PSPDFKit Catalog(opens in a new tab) for a complete example.
Downloading Documents Using URLSession
If you want to annotate a remote document or make changes to it, you can use URLSession
(opens in a new tab) to download the PDF. Then move it to a permanent location and display it using PDFViewController
(opens in a new tab).
Create a URLSession
(opens in a new tab) object with the required configurations. Then, use the session to create a download task (URLSessionDownloadTask
(opens in a new tab)) with the remote URL of the PDF document. Before resuming the task, make sure you’ve set the session object’s delegate:
let session = URLSession(configuration: URLSessionConfiguration.default, delegate: self, delegateQueue: OperationQueue.main)let task = session.downloadTask(with: URL(string: "https://github.com/PSPDFKit/pspdfkit-ios-catalog/raw/master/Samples/Magazine.pdf")!)task.resume()
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];NSURLSessionDownloadTask *task = [session downloadTaskWithURL:[NSURL URLWithString:@"https://github.com/PSPDFKit/pspdfkit-ios-catalog/raw/master/Samples/Magazine.pdf"]];[task resume];
Once the file is downloaded, the urlSession(_:downloadTask:didFinishDownloadingTo:)
(opens in a new tab) delegate method is called where the location
object is a temporary file URL. Hence it should be moved to a permanent location before opening it with PDFViewController
(opens in a new tab):
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) { // The desired saving location of the file is `destinationFileURL`, which `PDFViewController` will later use for loading. try! FileManager.default.moveItem(at: location, to: destinationFileURL)}
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location { NSError *error;
// The desired saving location of the file is `destinationFileURL`, which `PDFViewController` will later use for loading. [[NSFileManager defaultManager] moveItemAtURL:location toURL:destinationFileURL error:&error]; if (error) { NSLog(@"%@", error.localizedDescription); }}
Refer to DocumentProgressExample.swift
(opens in a new tab) in the PSPDFKit Catalog(opens in a new tab) for a complete example.