Data to string in UTF-8 encoding format conversion return nil in PKAddPaymentPassViewControllerDelegate's generateRequestWithCertificateChain method

I am implementing Apple Wallet, where I need to add the card to the wallet. I am trying to convert the certificates, nonce and nonceSignature that I am receiving in the addPaymentPassViewController(_:generateRequestWithCertificateChain:nonce:nonceSignature:completionHandler:) delegate method of PKAddPaymentPassViewControllerDelegate of PassKit, from 'Data' to String encoded in 'UTF8' format.
However, the conversion returns nil while converting from Data to String using utf8 encoding format.
Pls find an image attached. I have highlighted where the conversion fails
Could any please help me to identify what is going wrong?

Code Block
class ViewController: PKAddPaymentPassViewControllerDelegate {
func addPaymentPassViewController(_ controller: PKAddPaymentPassViewController, generateRequestWithCertificateChain certificates: [Data], nonce: Data, nonceSignature: Data, completionHandler handler: @escaping (PKAddPaymentPassRequest) -> Void) {
var encodedCertificateString = ""
for index in 0..<certificates.count {
if let encodedCertString = String(data: certificates[index], encoding: .utf8) {
encodedCertificateString.append(encodedCertString)
if index != (certificates.count-1) {
encodedCertificateString.append(", ")
}
}
}
}
func addPaymentPassViewController(_ controller: PKAddPaymentPassViewController, didFinishAdding pass: PKPaymentPass?, error: Error?) {
controller.dismiss()
}
}

The documentation for addPaymentPassViewController(_:generateRequestWithCertificateChain:nonce:nonceSignature:completionHandler:) says that certificates is:

An array of NSData objects. Each object contains a DER encoded X.509 certificate, with the leaf first and root last. You must download the root CA to validate the entire chain.

These are binary certificates, commonly known as DER. You can create certificate objects (SecCertificate) from them using SecCertificateCreateWithData. You can then do trust evaluation using a SecTrust object.

However, I suspect that you actually want to pass this data to the issuer server. If that server accepts binary certificates, you can just pass the data along directly. If the server expects PEM certificates, you’ll have to wrap them in a PEM yourself. This is relatively straightforward:
Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
Thank you for your reply Eskimo.

Our server expects us to pass the nonce in the format of string as an alpha-numeric String of length 6.

However, if I try to convert the nonce which is of type Data to base64Encoded String as per your recommendation, I am getting alpha-numeric string with special characters of length 8.

Could you please help us to obtain to convert this nonce to an alpha-numeric String of length 6.

Could you please help us to obtain to convert this nonce to an alpha-numeric String of length 6.

When a nonce is made of 4 bytes, no well known encodings for binary data generate alpha-numeric String of length 6.
So, your server is expecting some private encoding.
Please ask your server side engineer how you can make it.

When generating a PKAddPaymentPassRequest there is a Data field called wrappedKey. What goes in this field?

Is it possible to test the functionality with a development provisioning profile, or is a distribution profile required?
Data to string in UTF-8 encoding format conversion return nil in PKAddPaymentPassViewControllerDelegate's generateRequestWithCertificateChain method
 
 
Q