質問

ブラウザアプリのために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
  • をロードするときにエラーが発生したときに識別するためのUIWebViewDelegateプロトコルを実装する必要があります。
  • その後、フォールバックとして、Google検索(今は「+」に置き換えることができます)...またはBing、あなた次第です。

あなたのコードは以下のように何かを見るべきです:

...
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]
    }
}
.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top