Search code examples
iosobjective-ccharacter-encodingurl-encoding

encoding '+' character on a NSURLRequest


I am loading a webView with an external url and post data.

Everything works fine, unless the data contains the '+' character, then it fails. I've tried to encode the data in differents ways but I can't make it work. How can I correctly encode the plus character?

-(void) cargarWebViewTPVRedys:(TPVRedsys*) redsys{

    NSURL *url = [NSURL URLWithString: @"https://sis-t.redsys.es:25443/sis/realizarPago"]; // PRUEBAS
    // NSURL *url = [NSURL URLWithString: @"https://sis.redsys.es/sis/realizarPago"];     // REAL
    NSString *body = [NSString stringWithFormat: @"DS_SIGNATURE=%@&DS_MERCHANTPARAMETERS=%@&DS_SIGNATUREVERSION=%@",redsys.Ds_Signature,redsys.Ds_MerchantParameters,redsys.Ds_SignatureVersion];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url];
    [request setHTTPMethod: @"POST"];
    //[request setHTTPBody: [body dataUsingEncoding: NSUnicodeStringEncoding]];
    //[request setHTTPBody: [body dataUsingEncoding: NSASCIIStringEncoding]];
    [request setHTTPBody: [body dataUsingEncoding: NSUTF8StringEncoding allowLossyConversion:false]];
    NSLog(redsys.Ds_Signature);
    NSLog(redsys.Ds_MerchantParameters);
    NSLog(redsys.Ds_SignatureVersion);

    [self.webView loadRequest:request];
}

Solution

  • Seems that replace the '+' character for Its hexadecimal value

    NSString *body = [body2 stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];