Search code examples
objective-cgetmobile-safarinsurl

open Safari with NSURL using web link in get variable


I need open link kind of "http://www.web.com/?text=link =) http://google.ru is goooooood"

message=@"http://www.web.com/?text=link+=)+http://google.ru+is+goooooood";
twitter=[NSURL URLWithString:message];
[[UIApplication sharedApplication] openURL:twitter];

But it doesn't work. safari opens link: "http://www.web.com/?text=link =) ". But I need all text. Any ideas?


Solution

  • you should URL encode your parameters (special char in your patameters) you can use following function to url encode

    - (NSString *)stringByURLEncode:(NSString *)param {
    
        NSMutableString *tempStr = [NSMutableString stringWithString:param];
        [tempStr replaceOccurrencesOfString:@" " withString:@"+" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [tempStr length])];
    
    
        return [[NSString stringWithFormat:@"%@",tempStr] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    }
    

    Your message variable should look something like this -

    message=[NSString stringWithFormat:@"http://www.web.com/?text=%@",[self stringByURLEncode:@"link+=)+http://google.ru+is+goooooood"]];