在谷歌中搜索

发布于 2025-01-07 09:31:25 字数 696 浏览 0 评论 0原文

所以我创建了一个文本字段,我可以在其中搜索网络或谷歌。问题是我无法在 google 上搜索多个单词:例如,我可以搜索 Stackoverflow,但无法搜索 Stackoverflow 问题。代码如下:

{

if ([textField.text hasPrefix:@"http"]){
    url=[NSURL URLWithString:[textField text]];
    request = [NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
    }else{
        stringOne = @"http://www.google.com/search?q=";
        NSString *stringTwo = TextField.text;
        NSString *googleSearchString =[stringOne stringByAppendingString:stringTwo];
        NSURL *google =  [NSURL URLWithString:googleSearchString ];
        request = [NSURLRequest requestWithURL:google ];
        [webView loadRequest:request];
    }
}

感谢您的帮助!

So I have made a text field, where I can search a web or in Google. The problem is that I can't search more than one word on google: I can search Stackoverflow, but I can't search Stackoverflow questions, for example. Here's the code:

{

if ([textField.text hasPrefix:@"http"]){
    url=[NSURL URLWithString:[textField text]];
    request = [NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
    }else{
        stringOne = @"http://www.google.com/search?q=";
        NSString *stringTwo = TextField.text;
        NSString *googleSearchString =[stringOne stringByAppendingString:stringTwo];
        NSURL *google =  [NSURL URLWithString:googleSearchString ];
        request = [NSURLRequest requestWithURL:google ];
        [webView loadRequest:request];
    }
}

Thank you for helping!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉梦枕江山 2025-01-14 09:31:26

您的字符串可能未正确编码。也许尝试:

NSString* newGoogleSearchString = 
          [googleSearchString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];

Your string may not be encoded correctly. Maybe try:

NSString* newGoogleSearchString = 
          [googleSearchString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
月下伊人醉 2025-01-14 09:31:26

如果您在浏览器中查看 URL,您会看到类似这样的内容:

http://www.google.com/search?client=safari&rls=en&q=Stackoverflow+questions&ie=UTF-8&oe=UTF-8

重要的参数是 &q=Stackoverflow+questions ,注意代表空格的“+”号。

因此,如果您希望代码正常运行,则必须将空格替换为“+”字符。

If you look at the URL in a browser, you'll see something like this:

http://www.google.com/search?client=safari&rls=en&q=Stackoverflow+questions&ie=UTF-8&oe=UTF-8

the argument that is important is &q=Stackoverflow+questions , notice the '+' sign that stands for a space.

So, if you want your code to work, you have to replace spaces with a '+' character.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文