> For the complete documentation index, see [llms.txt](https://apryse.gitbook.io/api-markdown-test/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://apryse.gitbook.io/api-markdown-test/ios/viewer/viewer/file-coordination.md).

# File coordination

Learn about file coordination in iOS using UIDocument and PTCoordinatedDocument by Apryse. Ensure data integrity and avoid conflicts when accessing shared files. Optimize your app's performance with c

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`](https://developer.apple.com/uikit/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`](https://sdk.apryse.com/api/ios/Classes/PTCoordinatedDocument.html), 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`](/api-markdown-test/ios/basic-operations/open/doc-view.md) and [`TabbedViewer`](/api-markdown-test/ios/basic-operations/open/doc-tabbed.md), 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`.

{% tabs %}
{% tab title="Swift" %}
{% code lineNumbers="true" %}

```swift
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)
}
```

{% endcode %}
{% endtab %}

{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
- (void)presentDocumentAtURL:(NSURL *)documentURL {
    
    // Create a document viewer in which to show a document
    self.documentViewController = [[PTDocumentController alloc] init];
    
    // Create a coordinated document backed by the PDF document at the file URL `documentURL`
    PTCoordinatedDocument* coordinatedDocument = [[PTCoordinatedDocument alloc] initWithFileURL:documentURL];
    
    // Create a weak pointer to avoid a reference cycle
    __weak typeof(self) weakself = self;
    
    // Open the coordinated document in the document viewer
    [self.documentViewController setCoordinatedDocument:coordinatedDocument completionHandler:^(NSError* error)
    {
        // Create a strong self to ensure access during use
        typeof(self) strongSelf = weakself;
        
        if( strongSelf )
        {
             // Create an empty navigation controller to host the document viewer
             UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:weakself.documentViewController];
            
             // Create a button to close the navigation controller
             UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:strongSelf action:@selector(close:)];
            
             // Add the close button to the navigation controller
             [strongSelf.documentViewController.navigationItem setLeftBarButtonItem:item animated:NO];
            
             [strongSelf.view setNeedsLayout];
            
             // Present the view controller
             [strongSelf presentViewController:navigationController animated:YES completion:nil];
        }
         
     }];
}

// Method to dismiss the view controller
-(void)close:(UIBarButtonItem*)barButtonItem
{
    [self dismissViewControllerAnimated:YES completion:Nil];
}
```

{% endcode %}
{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://apryse.gitbook.io/api-markdown-test/ios/viewer/viewer/file-coordination.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
