使用NSURL在get变量中使用Web链接打开Safari

时间:2011-05-04 09:07:19

标签: objective-c get mobile-safari nsurl

我需要打开链接类型的“http://www.web.com/?text=link =)http://google.ru是goooooood”

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

但它不起作用。 safari打开链接:“http://www.web.com/?text=link =)”。 但我需要所有文字。 有什么想法吗?

1 个答案:

答案 0 :(得分:0)

你应该对你的参数进行URL编码(你的patameters中的特殊字符)你可以使用以下函数来进行url编码

- (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];

}

你的消息变量看起来应该是这样的 -

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