Create and Store Electronic Signatures from Instant JSON Annotations

To store electronic signatures in the signature store using annotations provided via Instant JSON, you can use a custom SignatureStore implementation. This populates the signature store with annotations from Instant JSON. In turn, this means that those annotations can then be used to create a new signature.

By leveraging the existing KeychainSignatureStore, you don’t even have to add the serialization yourself, and therefore, all annotations that were added either via Instant JSON or via the UI will be saved to disk.

This is the logic to implement a signature store that accepts Instant JSON to populate it:

class InstantJSONSignatureStore: KeychainSignatureStore {
    func addAnnotationUsingJSON(_ json: String, documentProvider: PDFDocumentProvider) throws {
        guard let jsonData = json.data(using: .utf8) else { return }
        let annotation = try Annotation(fromInstantJSON: jsonData, documentProvider: documentProvider)
        let container = SignatureContainer(signatureAnnotation: annotation, signer: nil, biometricProperties: nil)
        addSignature(container)
    }
}

And to use it, here’s example code:

let json = "..." // Instant JSON Annotation

let jsonSignatureStore = InstantJSONSignatureStore()

document.didCreateDocumentProviderBlock = { documentProvider in
    try? jsonSignatureStore.addAnnotationUsingJSON(json, documentProvider: documentProvider)
}

let controller = PDFViewController(document: document) {
    $0.signatureStore = jsonSignatureStore
}