Objc: create an NSURL with a string that does not comply with RFC 2396

I have a situation where I need to create a URL with a string containing ".

For instance:

NSString *myStringWithURL = [NSString stringWithFormat: 
                             @"http://myurl.com/setParam.php?setName=
                             {\"name\":\"%@\"} ", name];
// myStringWithURL will return the desired URL but it is a String.
// of course I need it as URL in order to make the request.
NSURL *targetURL = [NSURL URLWithString : myStringWithURL]; 
                       //This will return nil. 

So the question is, how do I make my URL not return null?

The {"name": "My Name"} part should be JSON, but it seemed easier than using an NSDictionary and then using a JSON parser, etc.

I really thank you for your help!

+3
source share
1 answer

use stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding to convert your string

+5
source

Source: https://habr.com/ru/post/1787924/


All Articles