在谷歌中搜索
所以我创建了一个文本字段,我可以在其中搜索网络或谷歌。问题是我无法在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的字符串可能未正确编码。也许尝试:
Your string may not be encoded correctly. Maybe try:
如果您在浏览器中查看 URL,您会看到类似这样的内容:
重要的参数是 &q=Stackoverflow+questions ,注意代表空格的“+”号。
因此,如果您希望代码正常运行,则必须将空格替换为“+”字符。
If you look at the URL in a browser, you'll see something like this:
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.