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の代わりにBingを使う方法を知っていますか?
-(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