File coordination

File coordination is a system of ensuring that no two processes (or threads within a single process) can change a commonly accessed file in a way the other does not expect, which could result in unexpected behavior and data loss.

iOS provides the UIDocument abstract base class which enables a subclass to be notified of any changes to the file or its state, which in turn allows an adopting app the ability to coordinate its actions with others.

Apryse provides PTCoordinatedDocument, a concrete subclass of UIDocument, to coordinate reading and writing of PDF files (both locally and cloud stored) with other apps. PTCoordinatedDocument is compatible with both the DocumentViewer and TabbedViewer, and is used by our sample project Complete Reader. It is recommended to use a PTCoordinatedDocument to open a document whenever the document could be written to by another app.

Below is sample code showing how to open a coordinated document in a new PTDocumentController.

1func presentDocument(at documentURL: URL?) {
2
3 // Create a document viewer in which to show a document
4 let documentViewController = PTDocumentController()
5
6 // Ensure a URL is available
7 guard let anURL = documentURL else { return }
8
9 // Create a coordinated document backed by the PDF document at the file URL `documentURL`
10 let coordinatedDocument = PTCoordinatedDocument(fileURL: anURL)
11
12 // Create a weak reference to avoid a reference cycle
13 weak var weakself = self
14
15 // Open the coordinated document in the document viewer
16 documentViewController.setCoordinatedDocument(coordinatedDocument, completionHandler: { error in
17
18 // Create a strong self to ensure access during use
19 guard let strongSelf = weakself else { return }
20
21 // Create an empty navigation controller to host the document viewer
22 let navigationController = UINavigationController(rootViewController: documentViewController)
23
24 // Create a button to close the navigation controller#selector(sayHello(sender:))
25 let item = UIBarButtonItem(barButtonSystemItem: .done, target: strongSelf, action: #selector(strongSelf.close(_:)))
26
27 // Add the close button to the navigation controller
28 documentViewController.navigationItem.setLeftBarButton(item, animated: false)
29
30 strongSelf.view.setNeedsLayout()
31
32 // Present the view controller
33 strongSelf.present(navigationController, animated: true)
34 })
35
36}
37
38// Method to dismiss the view controller
39@objc func close(_ barButtonItem: UIBarButtonItem?) {
40 self.dismiss(animated: true)
41}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales