Iphone – NSURL with string

iphonensurl

I have problem with NSURL. I am trying to create NSURL with string

code

    NSString *prefix = (@"tel://1234567890 ext. 101");
    NSString *dialThis = [NSString stringWithFormat:@"%@", prefix];
    NSURL *url = [[NSURL alloc] initWithString:dialThis];
    NSLog(@"%@",url);

also tried

    NSURL *url = [NSURL URLWithString:dialThis];

but it gives null . what is wrong ?

Thanks..

Best Answer

Your problem is the unescaped spaces in the URL. This, for instance, works:

    NSURL *url = [NSURL URLWithString:@"tel://1234567890x101"];

Edit: As does this..

    NSURL *url2 = [NSURL URLWithString:[@"tel://1234567890 ext. 101"
        stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
Related Topic