eSign PDFs with a Certificate in 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 path = NSTemporaryDirectory().appending("\(UUID().uuidString).pdf")

	 guard let pageInfo = document.pageInfoForPage(at: 0) else { return }
	 let valueLines = signatureController.drawView.pointSequences.map { $0.map({ NSValue.valueWithDrawingPoint($0) }) }
	 let lines = ConvertViewLines(pdfLines: valueLines, pageInfo: pageInfo, viewBounds: CGRect(origin: .zero, size: pageInfo.size))
	 let inkAnnotation = InkAnnotation(lines: lines.map { $0.map({ $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 = // Data for the PSCKS12 signer.
	 let p12 = PKCS12(data: p12data)
	 let signer = PKCS12Signer(displayName: "John Appleseed", pkcs12: p12)
	 signer.sign(signatureFormElement, usingPassword: "test", writeTo: path, appearance: appearance, biometricProperties: nil) {(_ success: Bool, _ document: Document, _ err: Error?) -> Void in
		  pdfController.document = document
	 }
}