I'm trying to make a POST request in a WebView in Xamarin.Forms for iOS. I've already implemented the WkWebViewRenderer and WebViewRenderer renderers as per the answer here. This implementation works fine on Android, but on iOS, I'm encountering an issue.
I need to perform a form-data submission in the POST request. I've found a native method in Swift that converts data into URL-encoded format and returns a Data object. The Swift method is as follows: https://github.com/asjservicios/pluspagosfirm/blob/master/PlusPagosFirm/Classes/FormularioModel.swift
public func toUrlEncodedForm() -> Data? {
var allowed = CharacterSet.alphanumerics
allowed.insert(charactersIn: "-._~")
if hash == nil {
hash = SHA256Firm.getFirm(ipClient: ip, secretKey: secretKey, guidComercio: comercio, sucursalId: sucursalComercio, monto: monto)
}
var r = "";
r += "Hash=\(hash!.addingPercentEncoding(withAllowedCharacters: allowed)!)&"
r += "TransaccionComercioId=\(transaccionComercioId.addingPercentEncoding(withAllowedCharacters: allowed)!)&"
r += "COMERCIO=\(comercio.addingPercentEncoding(withAllowedCharacters: allowed)!)&"
var i = 0
for p in producto {
r += "Producto[\(i)]".addingPercentEncoding(withAllowedCharacters: allowed)! + "=\(p.addingPercentEncoding(withAllowedCharacters: allowed)!)&"
i += 1
}
r += "Monto=\(AESEncrypter.encryptString(plainText: monto, phrase: secretKey)!.addingPercentEncoding(withAllowedCharacters: allowed)!)&"
r += "SucursalComercio=\(AESEncrypter.encryptString(plainText: sucursalComercio, phrase: secretKey)!.addingPercentEncoding(withAllowedCharacters: allowed)!)&"
r += "CallbackCancel=\(AESEncrypter.encryptString(plainText: callbackCancel, phrase: secretKey)!.addingPercentEncoding(withAllowedCharacters: allowed)!)&"
r += "CallbackSuccess=\(AESEncrypter.encryptString(plainText: callbackSuccess, phrase: secretKey)!.addingPercentEncoding(withAllowedCharacters: allowed)!)"
return r.data(using: .utf8)
}
This method works well in Swift, but I need to implement an equivalent solution in Xamarin.Forms for iOS. I've tried several approaches, but haven't been able to find a solution that works.
Could someone provide guidance on how I can implement a similar method in Xamarin.Forms for iOS that generates form data in URL-encoded format and returns it as an NSData object?