eSign PDFs with a Certificate on iOS

This guide shows how Electronic Signatures and Digital Signatures can be used together.

To let a user create a signature drawing using the Electronic Signatures UI, which you’ll in turn use to create a digital signature, you can present a SignatureCreationViewController and implement its delegate, SignatureCreationViewControllerDelegate, and get a callback to when the user finished. You can use the drawView property on the view controller to extract the drawn image from it.

Using this in practice could look something like this:

let signatureController = SignatureCreationViewController()
var configuration = SignatureCreationViewController.Configuration()
configuration.availableModes = [.draw]
signatureController.configuration = configuration
signatureController.delegate = self
pdfController.present(signatureController, animated: true)

func signatureCreationViewControllerDidFinish(_ signatureController: SignatureCreationViewController) {
	 signatureController.dismiss(animated: true)
	 guard let signatureFormElement = document.annotations(at: pageIndex, type: SignatureFormElement.self).first else { return }
     let url = URL(fileURLWithPath: NSTemporaryDirectory().appending("\(UUID().uuidString).pdf"))
     guard let pageInfo = document.pageInfoForPage(at: 0) else { return }
     let valueLines = { ${ NSValue.valueWithDrawingPoint($0) }) }
     let lines = ConvertViewLines(pdfLines: valueLines, pageInfo: pageInfo, viewBounds: CGRect(origin: .zero, size: pageInfo.size))
     let inkAnnotation = InkAnnotation(lines: { ${ $0.drawingPointValue }) })
     // Create the `UIImage` from the ink annotation in your `SignatureViewController`.
     guard let image = inkAnnotation.image(size: inkAnnotation.boundingBox.size, options: nil) else { return }
     let appearance = PDFSignatureAppearance { builder in
          builder.signatureGraphic = Annotation.AppearanceStream(image: image)
     let p12data: Data // PSCKS12 data.
     let p12 = PKCS12(data: p12data)
     Task {
         let (certificates, privateKey) = try p12.unlockCertificateChain(withPassword: "test")
         try await document.sign(formElement: signatureFormElement, configuration: SigningConfiguration(dataSigner: privateKey, certificates: certificates, appearance: appearance), outputDataProvider: FileDataProvider(fileURL: url))
         pdfController.document = Document(url: url)