除非我尝试搜索两个以上的单词,否则一切似乎都运行良好。因此,“apple”作为搜索被发送到谷歌,但“apple stuff”无法加载。有任何想法吗?此外,我将谷歌搜索添加到 didfailtoload 中,但返回了一个 didfailtoload 循环。
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
webView.delegate = self;
if ([urlField.text hasPrefix:@"http://"]) {
[webView loadRequest: [NSURLRequest requestWithURL: [NSURL URLWithString:urlField.text]]];
[urlField resignFirstResponder];
return NO;
} else if ([self isProbablyURL:urlField.text]) {
NSString *query = [urlField.text stringByReplacingOccurrencesOfString:@" " withString:@"+"];
NSURL *urlQuery = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@", query]];
NSURLRequest *request = [NSURLRequest requestWithURL:urlQuery];
[webView loadRequest:request];
[urlField resignFirstResponder];
return NO;
} else {
([self performGoogleSearchWithText:urlField.text]);
[urlField resignFirstResponder];
return YES;
}
}
- (void)performGoogleSearchWithText:(NSString *)text {
// Make a google request from text and mark it as not being "fallbackable" on a google search as it is already a Google Search
NSString *query = urlField.text;
NSURL *urlQuery = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.google.com/search?hl=en&site=&source=hp&q=%@", query]];
NSURLRequest *request = [NSURLRequest requestWithURL:urlQuery];
[webView loadRequest:request];
}
- (BOOL)isProbablyURL:(NSString *)text {
// do something smart and return YES or NO
NSString *urlRegEx =
@"((\\w)*|(m.)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|(m.)*|([0-9]*)|([-|_])*))+";
NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx];
return [urlTest evaluateWithObject:urlField.text];
//return NO;
}