iOS 5에서 통합 된 주소 검색 표시 줄 만들기?
-
12-12-2019 - |
문제
브라우저 앱의 경우 iOS 5에서 통합 된 주소 표시 줄을 만드는 데 도움이됩니까?그래서 내 주소 표시 줄이 있습니다.
-(IBAction)url:(id)sender {
NSString *query = [urlBar.text stringByReplacingOccurrencesOfString:@" " withString:@"+"];
NSURL *urlQuery = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@", query]];
NSURLRequest *request = [NSURLRequest requestWithURL:urlQuery];
[webPage loadRequest:request];
}
.
주소가 아니면 Google 검색 태그를 추가하는 경우 "else"참조를 추가 할 수 없었습니다.그렇다면 어떻게?그리고 Google 대신에 빙을 사용하는 방법을 알고 있습니까?
-(IBAction)googleSearch:(id)sender {
NSString *query = [googleSearch.text stringByReplacingOccurrencesOfString:@" " withString:@"+"];
NSURL *urlQuery = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.google.com/search?hl=en&site=&q=%@", query]];
NSURLRequest *request = [NSURLRequest requestWithURL:urlQuery];
[webPage loadRequest:request];
}
. 해결책
여기에 도움이되는 몇 가지 팁이 있습니다.
- "+"대신
stringByAddingPercentEscapesUsingEncoding:
를 사용하십시오. - http : //가 를 추가하기 전에 URL 문자열에 접두사가 아닌지 확인해야합니다.
- 유효하지 않은 URL 를로드 할 때 오류가 발생할 때 식별하기 위해
- 은 대체로서 Google 검색을 시작합니다 (이제 "+"로 대체 할 수 있습니다 "... 또는 Bing, 뭐든간에!
UIWebViewDelegate
프로토콜을 구현해야합니다.
코드는 다음과 같이 보입니다.
...
webView.delegate = self; // Should appear in your code somewhere
...
-(IBAction)performSearch {
if ([searchBar.text hasPrefix:@"http://"]) {
... // Make NSURL from NSString using stringByAddingPercentEscapesUsingEncoding: among other things
[webView loadRequest:...]
} else if ([self isProbablyURL:searchBar.text]) {
... // Make NSURL from NSString appending http:// and using stringByAddingPercentEscapesUsingEncoding: among other things
[webView loadRequest:...]
} else {
[self performGoogleSearchWithText:searchBar.text]
}
}
- (BOOL)isProbablyURL:(NSString *)text {
... // do something smart and return YES or NO
}
- (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
[webView loadRequest:...]
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
... // Notify user
if (was not already a Google Search) {
[self performGoogleSearchWithText:searchBar.text]
}
}
. 제휴하지 않습니다 StackOverflow