Pergunta

Ajuda para fazer um servidor de Unificação barra de endereço do ios 5 para um Aplicativo Navegador?Então aqui está a minha barra de endereço.

-(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];
}

Não poderia eu acrescentar uma "pessoa de referência" para dizer se isso não é um endereço, em seguida, acrescentar o google tag de pesquisa?Se sim, como?E você sabe como usar o Bing em vez do 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];
}
Foi útil?

Solução

Aqui estão algumas dicas para ajudar você:

  • utilização stringByAddingPercentEscapesUsingEncoding: em vez de o "+" de substituição.
  • você deve verificar se a http:// não é um prefixo para a seqüência de caracteres de URL, antes de acrescentar que
  • você deve implementar a UIWebViewDelegate protocolo para identificar quando ocorre um erro ao carregar um URL inválido
  • em seguida, como uma reversão de lançamento de seu Google search (agora você pode substituir " o " por "+")...ou Bing, o que quer e até para você!

Seu código deve parecer algo como a seguir:

...
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]
    }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top