Make GET request through a WebView

0

I have a problem when trying to send a request via get to a WebView to open it containing a page, when I pass the concatenated faces I get an error, I think it's for coding reasons this only happens when I pass parameters with special characters , could anyone help me with this? Follow the code I'm using.

NSString *titulo = [listDictionary objectForKey:@"titulo"];
NSString *imagem = [listDictionary objectForKey:@"imagem"];
NSString *descricao = [listDictionary objectForKey:@"descricao"];

NSString *getString = [NSString stringWithFormat:@"https://meudominio.com.br/web.php?titulo=%@&image=%@&conteudo=%@", titulo, imagem, descricao];


getString = [getString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];

NSURL *url = [NSURL URLWithString:getString];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webview loadRequest:requestObj];
    
asked by anonymous 30.06.2016 / 17:39

1 answer

1

I have a function for these cases. You need to convert the text of the GET call values to the so-called Percent Encoding, but the characters '&' and '?' are not included in this Objective-C conversion (also remembering that you need to choose UTF-8 encoding in this function). Make an extension (extension) of the NSString and add the following:

-(NSString*)stringToWebStructure
{
    NSString* webString = [self stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    webString = [webString stringByReplacingOccurrencesOfString:@"&" withString:@"%26"];
    webString = [webString stringByReplacingOccurrencesOfString:@"?" withString:@"%3F"];

    return webString;
}

And then replace this:

NSString *getString = [NSString stringWithFormat:@"https://meudominio.com.br/web.php?titulo=%@&image=%@&conteudo=%@", titulo, imagem, descricao];


getString = [getString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];

NSURL *url = [NSURL URLWithString:getString];

So:

NSString *getString = [NSString stringWithFormat:@"https://meudominio.com.br/web.php?titulo=%@&image=%@&conteudo=%@", titulo.stringToWebStructure, imagem.stringToWebStructure, descricao.stringToWebStructure];
NSURL *url = [NSURL URLWithString:getString];

This is another important detail: you should not convert the entire URL, just the values of the arguments, after all the rest of the URL is already in an acceptable format.

    
11.10.2016 / 18:40