vertical pipe symbol not being detected in NSURL

回眸只為那壹抹淺笑 提交于 2019-12-30 04:46:24

问题


I'm trying to place a search in google dictionary but the url contains a vertical pipe/bar | My code just fails to load the site

 //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]];
    }

using %7C did not work either..

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

回答1:


Consider encoding your URL using something like:

NSURL *url = [NSURL URLWithString:[searchURL stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]]; 

rather than manually doing it yourself. Perhaps there is some characters in your mySearchString NSString that also needs encoding. Run this encoding after you have stringWithFormat'd the full URL together.



来源:https://stackoverflow.com/questions/6167331/vertical-pipe-symbol-not-being-detected-in-nsurl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!