taokakao
taokakao

Reputation: 19

NSURL not instantiating properly

I am new to IPhone developing.

I have got very strange issue with NSURL which is driving me crazy. I want to instantiate NSURL object to use it in loading image. But instantiation never happens in proper way. It always says my url in invalid.

Basically I use code like below:

NSString *str = @"http://google.com";
NSURL *url = [NSURL URLWithString:str];

but I also have tried a lot of different modifications (with CFStringRef + CFURLCreateStringByAddingPercentEscapes and so on). All of them are not working for me. In debugger url is set to "Invalid".

What it could be? I don't think this is algorithmic or environment issue. It could be about some settings?

Have anyone any idea of what happens?

Upvotes: 1

Views: 1025

Answers (2)

Tim
Tim

Reputation: 60150

Your string isn't escaped properly - you should do it like this:

NSString *str = @"http://google.com";
NSString *escStr = [str stringByAddingPercentEscapesUsingEncoding:
                                              NSASCIIStringEncoding];
NSURL *url = [NSURL URLWithString:escStr];

Upvotes: 3

Azeem.Butt
Azeem.Butt

Reputation: 5869

Are you using that actual code or just code "like" it? That method will return nil if you don't feed it a valid string with everything escaped properly.

Upvotes: 2

Related Questions