How Do I Get Notified When a Password-Protected Document Is Unlocked?

Q: If I initialize a PSPDFViewController with a password-protected document, the documentViewController property is nil until the document is unlocked. Is there a way to be notified when the documentViewController becomes valid?

You can implement -[PSPDFViewControllerDelegate pdfViewController:didChangeControllerState:error:] to get the non-nil document view controller when the document is unlocked, like so:

Copy
1
2
3
4
5
6
7
8
9
10
11
12
let pdfController = PSPDFViewController(document: lockedDocument)
pdfController.delegate = self

...

func pdfViewController(_ pdfController: PSPDFViewController, didChange controllerState: PSPDFControllerState, error: Error?) {
    if controllerState == .default {
        if let layout = pdfController.documentViewController?.layout as? PSPDFContinuousScrollingLayout {
            layout.fillAlongsideTransverseAxis = true
        }
    }
}
Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
PSPDFViewController *pdfController = [[PSPDFViewController alloc] initWithDocument:lockedDocument];
pdfController.delegate = self;

...

- (void)pdfViewController:(PSPDFViewController *)pdfController didChangeControllerState:(PSPDFControllerState)controllerState error:(nullable NSError *)error {
    if (controllerState == PSPDFControllerStateDefault) {
        PSPDFDocumentViewLayout *layout = pdfController.documentViewController.layout;
        if ([layout isKindOfClass:PSPDFContinuousScrollingLayout.class]) {
            ((PSPDFContinuousScrollingLayout *)layout).fillAlongsideTransverseAxis = YES;
        }
    }
}