views:

293

answers:

3

Hi, 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?

+2  A: 

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.

Azeem.Butt
The exact code are:1) native to NSString escaping: NSString *str = @"http://google.com"; NSString *escaped = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL *url = [NSURL URLWithString:escaped];and 2) CFStringRef escaping: NSString *str = @"http://google.com"; CFStringRef escaped = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)str, NULL, (CFStringRef)@";/?:@ NSURL *url = [NSURL URLWithString:(NSString *)escaped];Both are failing.
taokakao
+1  A: 

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];
Tim
This code makes the same error. When I look in debugger: _urlString parameter of url is "Invalid". And next use of url raises exception.
taokakao
A: 

Hi taokakao,

Could you solve the problem? I run into the exact same issue...

Thanks Markus

Markus
Nope, couldn't. Finally I switched to WebView which works fine.
taokakao
Thanks for your Answer, I'll give it a try!
Markus