nobu86
nobu86

Reputation: 55

NSURLRequest returning null

Someone knows why I'm getting null in this code?

NSURLRequest *requisicao = [NSURLRequest requestWithURL:
                               [NSURL URLWithString:URLAddress]];

When I debug this line and preview the content of this variable, I receive null -
"<NSURLRequest (null)>"

My URLAddress is a request for a JSON service.

Upvotes: 2

Views: 2214

Answers (1)

user467105
user467105

Reputation:

It's most likely null because URLAddress contains characters that need to be percent-escaped (which is what the URLWithString method needs).

Try using stringByAddingPercentEscapesUsingEncoding:

NSURLRequest *requisicao = [NSURLRequest requestWithURL:
    [NSURL URLWithString:
        [URLAddress stringByAddingPercentEscapesUsingEncoding:
                        NSUTF8StringEncoding]]];    

However, see this answer by Dave DeLong which points out limitations of stringByAddingPercentEscapesUsingEncoding and alternatives to it.

Upvotes: 6

Related Questions