mac
mac

Reputation: 4780

NSURL returns null while sending JSON service request?

My url is http://sample_domain/sample.json?{ "language" : "en", "category" : "sports"}

I am converting this to NSURL object like below.

NSURL *url=[NSURL URLWithString:@"http://sample_domain/sample.json?{ "language" : "en", "category" : "sports"}"];

its returning nil to NSURL object, i know the URL should be in specific format from apple documentation,otherwise it will be considered as a malware site and returns nil object,

my question is how can i pass these paremeters with apple standards, so that it will not retun nil object.

Thanks in advance.

Upvotes: 0

Views: 872

Answers (1)

Vladimir
Vladimir

Reputation: 170849

Try

NSURL *url=[NSURL URLWithString:[@"http://sample_domain/sample.json?{ \"language\" : \"en\", \"category\" : \"sports\"}"
                  stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

I'm not sure why '"' characters are not escaped in your example, but I think without escapes your code should not compile.

Also you need (and that's the main problem) to replace characters that are illegal in url (e.g. ':' with corresponding percent escapes - that's what stringByAddingPercentEscapesUsingEncoding function does.

Upvotes: 1

Related Questions