在iOS xcode 4.3中,我几乎有了一个统一的搜索栏,但只有一个单词?

在iOS xcode 4.3中,我几乎有了一个统一的搜索栏,但只有一个单词?,xcode,url,search,Xcode,Url,Search,除了我试图搜索两个以上的单词外,一切似乎都很顺利。所以,“苹果”被发送到谷歌作为搜索,但“苹果的东西”只是无法加载。有什么想法吗?另外,我在didfailtoload中添加了谷歌搜索,但返回了一个didfailtoload循环 -(BOOL)textFieldShouldReturn:(UITextField *)textField { webView.delegate = self; if ([urlField.text hasPrefix:@"http://"]) {

除了我试图搜索两个以上的单词外,一切似乎都很顺利。所以,“苹果”被发送到谷歌作为搜索,但“苹果的东西”只是无法加载。有什么想法吗?另外,我在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;

}

-performGoogleSearchWithText:
中,您正在使用用户提供的搜索字符串创建一个Google URL。但是,如果查询包含多个单词,这些单词之间将有空格,NSURL将拒绝创建URL,因为它将包含无效字符。为了使其有效,您必须将空格字符替换为转义百分比等同物,例如%20

在以下版本的
-performGoogleSearchWithText:
方法中,我使用了NSStrings
+stringByAddingPercentEscapesUsingEncoding:
方法将搜索字符串中在URL中无效的字符替换为URL百分比编码的等效字符

- (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 = [text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    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];
}
注意:我使用了传入的text参数,而不是使用
urlField.text
直接从用户界面获取用户查询