问题描述
我正在尝试在Google字典中进行搜索,但网址中包含垂直的竖线/栏|
我的代码只是无法加载网站
//http://www.google.com/dictionary?aq=f&langpair=en|en&q=test+test+test&hl=en
if ([mySearchEngineName isEqualToString:@\"Google Dictionary\"]){
NSLog(@\"Currently searching %@ using %@\",mySearchString,mySearchEngineName);
Nsstring *mutateSearchString = [mySearchString stringByReplacingOccurrencesOfString:@\" \" withString:@\"+\"];
Nsstring *searchURL =[Nsstring stringWithFormat:@\"http://www.google.com/dictionary?aq=f&langpair=en|en&q=%@&hl=en\",mutateSearchString];
NSURL *url = [NSURL URLWithString:searchURL];
[webbrowser loadRequest:[NSURLRequest requestWithURL:url]];
}
使用%7C也不起作用。
Nsstring *searchURL =[Nsstring stringWithFormat:@\"http://www.google.com/dictionary?aq=f&langpair=en%7Cen&q=%@&hl=en\",mutateSearchString];
解决方法
考虑使用以下方式对URL进行编码:
NSURL *url = [NSURL URLWithString:[searchURL stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
而不是自己手动进行。也许您的mySearchString NSString中有些字符也需要编码。在将stringWithFormat \完整的URL放在一起之后,运行此编码。