webview不会加载字符串

时间:2014-07-25 05:11:59

标签: ios url search webview nsurl

在我的webview viewdidload中,我实现了以下代码。问题是当我包含NSString * encodedString时,webview不会出现。我有searchString空格,有时&amp ;.关于这里发生了什么的任何想法?

NSString *urlString = [NSString stringWithFormat:@"http://google.com?q=%@", searchString];
        NSString * encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(
           NULL,(CFStringRef)urlString,
       NULL,(CFStringRef)@"!*'();:@&=+$,/?%#[]",
         kCFStringEncodingUTF8 ));

    NSLog(@"searchString is %@", searchString);
    NSURL *myURL =[NSURL URLWithString:encodedString];
    NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
    [searchWebView loadRequest:myRequest];

为此searchString生成google的第一页结果需要什么?

2 个答案:

答案 0 :(得分:0)

使用它来编码SearchString

SearchString=[[NSString stringWithFormat:@"%@", SearchString]  stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLQueryAllowedCharacterSet];

然后将其附加到您的网址

答案 1 :(得分:0)

试试这个

NSString *urlString = [NSString stringWithFormat:@"http://www.google.com/search?q=%@",searchString];
   urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];


   NSURL *myURL =[NSURL URLWithString:urlString];
    NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
    [searchWebView loadRequest:myRequest];