此NSURL请求已编译,但无法按预期工作。我错过了什么?

时间:2015-02-01 13:32:45

标签: ios objective-c xcode boolean nsurl

我目前正在参加iOS开发课程。我已经创建了一个基本的Web浏览器,可以处理输入的URL,如果用户没有添加它,将添加“http://”。使用下面的代码,用户在textField中输入文字,该应用会加载网页。

我的任务是在Google搜索时使用空格处理textField条目。当我运行我的应用时,它会加载网页,但Google搜索无效。我很难过为什么,但我知道我可以放心,这是我做错了。问题是什么?

以下是属性:

// Properties
@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, assign) BOOL isSearchTerm;

我写的BOOL用来测试textField是网址还是搜索字词:

// Test to determine whether textField.text is a searchTerm
- (BOOL)isSearchTerm:(UITextField *)textField {
    NSString *textInURLBar = textField.text;
    NSRange whiteSpaceRange = [textInURLBar rangeOfCharacterFromSet:[NSCharacterSet whitespaceCharacterSet]];
    if (whiteSpaceRange.location != NSNotFound) {
        return NO;
    } else {
        return YES;
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];

    // Do a test of textField to see if it's a webpage or a search term
    if (self.isSearchTerm == YES) {
        // Homework: write code to searchGoogle, see method below
        [self searchGoogle:textField];
    } else {
        // isSearchTerm == NO, so treat as an URL
        NSString *URLString = textField.text;
        NSURL *URL = [NSURL URLWithString:URLString];

        // User enters incomplete URL
        if (!URL.scheme) {
            // if the user didn't enter http:// or https://
            URL = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@", URLString]];
        }

        // User enters complete URL
        if (URL) {
            NSURLRequest *request = [NSURLRequest requestWithURL:URL];
            [self.webView loadRequest:request];
        }
    }
    return NO;
}

搜索Google的代码:

- (void)searchGoogle:(UITextField *)textField {
    NSString *URLString = textField.text;
    NSRange spaces = [URLString rangeOfString:@" "];
    NSString *searchTerm = [URLString stringByReplacingCharactersInRange:spaces withString:@"+"];
    // Google search query is http://www.google.com/search?q=
    NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.google.com/search?q=%@", searchTerm]];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    [self.webView loadRequest:request];
}

2 个答案:

答案 0 :(得分:0)

如果您要用+替换空格,那么您目前只会在第一次出现时更换空格。您可能希望为所有事件执行此操作。因此,您可以使用stringByReplacingCharactersInRange代替stringByReplacingOccurrencesOfString:withString:

或者,您可能希望百分比转义输入(不仅要处理空格,还要处理其他保留字符.Web浏览器倾向于为您执行此操作,但在自己创建请求时,您必须手动执行此操作您的代码。至少,您可以使用stringByAddingPercentEscapesUsingEncoding创建新字符串(代替stringByReplacingCharactersInRange)。

从技术上讲,您可能希望进一步优化google逻辑(执行百分比转义,用+个字符替换空格,但正确的百分比转义字符,而不是上述字符)您需要提前转义的字符数超过{{ 1}}将自行完成),因此您将对添加到Google查询的值(而不是URL本身)执行以下百分比转义:

stringByAddingPercentEscapesUsingEncoding

答案 1 :(得分:0)

它并不漂亮,但我让它发挥作用。我取出了BOOLgoogleSearch函数,并将所有内容转储到textFieldShouldReturn

我不想将一堆代码转储到textFieldShouldReturn中,而是希望将其分解为更易于使用的代码(可行)。虽然它有效,但我觉得它很难看。

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];

    NSString *URLString = textField.text;

    // This is if it's a normal request
    if ([URLString rangeOfString:@" "].location == NSNotFound) {
        NSURL *URL = [NSURL URLWithString:URLString];

        // User enters incomplete URL
        if (!URL.scheme) {
            // if the user didn't enter http:// or https://
            URL = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@", URLString]];
        }

        // User enters complete URL
        if (URL) {
            NSURLRequest *request = [NSURLRequest requestWithURL:URL];
            [self.webView loadRequest:request];
        }
    }

    // This handles a space in the textField.text
    if ([URLString rangeOfString:@" "].location != NSNotFound) {
        NSLog(@"there's a space in the request");
        URLString = [URLString stringByReplacingOccurrencesOfString:@" " withString:@"+"];
        NSLog(@"The new URLString is %@", URLString);
        NSURL *URL = [NSURL URLWithString:URLString];
        // User enters incomplete URL
        if (!URL.scheme) {
            // if the user didn't enter http:// or https://
            URL = [NSURL URLWithString:[NSString stringWithFormat:@"http://google.com/search?q=%@", URLString]];
        }

        // User enters complete URL
        if (URL) {
            NSURLRequest *request = [NSURLRequest requestWithURL:URL];
            [self.webView loadRequest:request];
        }
    }
    return NO;
}