2014-02-20 32 views
1

我在我的應用程序UIWebview,這是我如何瀏覽應用程序中的某個時候出現URL的UIWebView禁止重定向到AppStore的

NSURL *url = [NSURL URLWithString:@"http://mp3skull.com/mp3/nirvana.html"]; 
NSURLRequest* request = [NSURLRequest requestWithURL:url]; 

[webView loadRequest:request]; 

,有時我發現,如果我的鏈接點擊AppStore打開應用程序與一些應用程序。

可以禁用它嗎?

+0

你解決這個問題? – santhu

回答

0

在該方法中- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

你可以做

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
{ 
    NSString *requestedURL = [[request URL] absoluteString]; 
    // URL for opening itunes according to Apple docs is something like this @"http://itunes.apple.com 
    // But I do believe that it will be some sort of URL scheme that opens it specifically to the app on the app store. 
    // So without an example this is the best I can provide. 
    if([requestedURL isEqualToString:@"http://itunes.apple.com"]) { 
    // or if([requestedURL rangeOfString:@"itunes.apple.com"].location==0) { 
     // What is happening here is that if the request url that is being request is 
     // "http://mp3skull.com/mp3/nirvana.html" then we don't want to continue with the request so stop. 
     return NO; 
    } 
    // Otherwise for all other requests continue 
    return YES; 
} 

記住,你將需要設置委託您UIWebView因爲這種方法是一種UIWebViewDelegate方法 - 看Apple Documentation on UIWebViewDelegate for more information

+0

謝謝,但我寫道,如果我點擊這個頁面上的鏈接,它會發生,或任何其他頁面 – MTA

+0

@MTA這些鏈接背後的URL是什麼?因爲你沒有提供他們,我只是使用你提供的那個。通過它的聲音,這將是一個URL方案設置使用這種相同的方法,你可以阻止,但除非你提供了一個鏈接我猜的例子。請提供。 – Popeye

+0

'http:// mp3skull.com/mp3/nirvana.html'這是一個goog鏈接,但裏面有打開應用商店應用程序的鏈接。我想阻止該應用商店鏈接 – MTA

0

是它發生,有時當你點擊某些鏈接時,應用程序將打開另一個應用程序。

因爲這些鏈接包含其他應用程序模式。 (link) 因此,要禁用打開這樣的url-schemas,我們必須檢測它們並且不加載它們。

由於AppStore中有 「itunes.apple.com」,

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
{ 
    NSString *requestedURL = [[request URL] absoluteString]; 
    if([requestedURL rangeOfString:@"itunes.apple.com"].location==0) { 
     return NO; 
    } 
    return YES; 
}