我目前正在学习iOS开发课程。我创建了一个基本的 Web 浏览器,可以处理输入的 URL,如果用户不添加它,它将添加"http://"。使用下面的代码,用户在textField
中输入文本,应用程序加载一个网页。
我的任务是将带有空格textField
条目视为Google搜索。当我运行我的应用程序时,它会加载网页,但 Google 搜索不起作用。我很困惑为什么,但我知道我可以放心,这是我做错了什么。问题是什么?
以下是属性:
// Properties
@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, assign) BOOL isSearchTerm;
以下是我为测试textField
是URL还是搜索词而编写BOOL
:
// 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;
}
搜索谷歌的代码:
- (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];
}
用+
替换空格,则当前仅在第一次出现时执行此操作。您可能希望对所有匹配项执行此操作。因此,您可以使用 stringByReplacingOccurrencesOfString:withString:
而不是 stringByReplacingCharactersInRange
。
或者,您可能希望对输入进行百分比转义(不仅要处理空格,还要处理其他保留字符)。Web 浏览器倾向于为您执行此操作,但是在自己创建请求时,您必须在代码中手动执行此操作。因此,至少您可以使用stringByAddingPercentEscapesUsingEncoding
创建新字符串(代替stringByReplacingCharactersInRange
)。
从技术上讲,您可能希望进一步完善 google 逻辑(执行百分比转义,用 +
个字符替换空格,但正确百分比转义字符,上述字符不会)您需要比stringByAddingPercentEscapesUsingEncoding
本身执行的字符数),因此您将对添加到 google 查询的值(但不是 URL 本身)执行以下百分比转义:
- (NSString *)percentEscapeString:(NSString *)string
{
NSString *result = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)string,
(CFStringRef)@" ",
(CFStringRef)@":/?@!$&'()*+,;=",
kCFStringEncodingUTF8));
return [result stringByReplacingOccurrencesOfString:@" " withString:@"+"];
}
它并不漂亮,但我让它工作了。我取出了BOOL
和googleSearch
功能,并将所有内容都倾倒在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;
}