Post

Replies

Boosts

Views

Activity

Convert nonce Data to 6 character String
I am implementing Apple Wallet, where I need to add the card to the wallet. My payment gateway requires me to send a 6 digit alphanumeric nonce to them. However, if I try to convert the nonce which is of type Data, that I receive in the generateRequestWithCertificateChain PassKit delegate method, to HexEncoding String, I am getting 8 character String. How to convert this nonce to a 6 digit hexString? func addPaymentPassViewController(_ controller: PKAddPaymentPassViewController, generateRequestWithCertificateChain certificates: [Data], nonce: Data, nonceSignature: Data, completionHandler handler: @escaping (PKAddPaymentPassRequest) -> Void) I am converting nonce data to hexString using the following method func hexEncodedString(options: HexEncodingOptions = []) -> String { 		let hexDigits = Array((options.contains(.upperCase) ? "0123456789ABCDEF" : "0123456789abcdef").utf16) 		var chars: [unichar] = [] 		chars.reserveCapacity(2 * count) 		for byte in self { 				chars.append(hexDigits[Int(byte / 16)]) 				chars.append(hexDigits[Int(byte % 16)]) 		} 		return String(utf16CodeUnits: chars, count: chars.count) }
1
0
948
Jul ’20
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? 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() 		} }
4
0
2.7k
Jul ’20