Some test text!

Search
Hamburger Icon

iOS / Guides

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.

func presentDocument(at documentURL: URL?) {
    
    // Create a document viewer in which to show a document
    let documentViewController = PTDocumentController()
    
    // Ensure a URL is available
    guard let anURL = documentURL else { return }
    
    // Create a coordinated document backed by the PDF document at the file URL `documentURL`
    let coordinatedDocument = PTCoordinatedDocument(fileURL: anURL)

    // Create a weak reference to avoid a reference cycle
    weak var weakself = self

    // Open the coordinated document in the document viewer
    documentViewController.setCoordinatedDocument(coordinatedDocument, completionHandler: { error in
        
        // Create a strong self to ensure access during use
        guard let strongSelf = weakself else { return }
        
        // Create an empty navigation controller to host the document viewer
        let navigationController = UINavigationController(rootViewController: documentViewController)
        
        // Create a button to close the navigation controller#selector(sayHello(sender:))
        let item = UIBarButtonItem(barButtonSystemItem: .done, target: strongSelf, action: #selector(strongSelf.close(_:)))
        
        // Add the close button to the navigation controller
        documentViewController.navigationItem.setLeftBarButton(item, animated: false)
        
        strongSelf.view.setNeedsLayout()
        
        // Present the view controller
        strongSelf.present(navigationController, animated: true)
    })
    
}

// Method to dismiss the view controller
@objc func close(_ barButtonItem: UIBarButtonItem?) {
    self.dismiss(animated: true)
}

Get the answers you need: Chat with us