NSURL中未检测到垂直管道符号

Blo*_*der 7 objective-c nsurl

我正在尝试在谷歌词典中进行搜索,但网址包含垂直管道/栏 我的代码无法加载网站

 //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]];
    }
Run Code Online (Sandbox Code Playgroud)

使用%7C也不起作用..

NSString *searchURL =[NSString stringWithFormat:@"http://www.google.com/dictionary?aq=f&langpair=en%7Cen&q=%@&hl=en", mutateSearchString];
Run Code Online (Sandbox Code Playgroud)

Dam*_*ien 18

考虑使用以下内容编码您的URL:

NSURL *url = [NSURL URLWithString:[searchURL stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]]; 
Run Code Online (Sandbox Code Playgroud)

而不是自己手动完成.也许你的mySearchString NSString中还有一些字符也需要编码.在将stringWithFormat和完整的URL放在一起之后运行此编码.