2010-09-02 64 views
0

我試圖在UIWebView中點擊超鏈接時啓動外部Safari,但在我的情況下沒有任何反應。如果我嘗試將目標作爲空白並跳過UIWebView委託方法,它會在同一視圖中啓動Safari。請引導我的朋友如何在點擊UIWebView中的鏈接時打開外部瀏覽器..這是我的代碼..我正在創建一個UIWebView編程UIWebView的問題

CGRect webFrame = CGRectMake(10,78,300,50); 
     contactUsView.delegate = self; 
     contactUsView = [[UIWebView alloc] initWithFrame:webFrame]; 
     [contactUsView setOpaque:NO]; 
     contactUsView.backgroundColor = [UIColor clearColor]; 
     NSString *html = @"<html><head></head><body>Copyright \u00A9 2010 <a href='http://www.example.com'>Hello</a><br/>Hi <a href='http://example1.com>Click here</a></body></html>"; 
     [contactUsView loadHTMLString:html baseURL:[NSURL URLWithString:@"http://www.solstice-consulting.com"]]; 



- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request 
navigationType:(UIWebViewNavigationType)navigationType; { 

    NSURL *requestURL = [ [ request URL ] retain ]; 
    NSLog(@"expected:%d, got:%d", UIWebViewNavigationTypeLinkClicked, navigationType); 

    if (([ [ requestURL scheme ] isEqualToString: @"http" ] 
      || [ [ requestURL scheme ] isEqualToString: @"https" ]) 
     && (navigationType == UIWebViewNavigationTypeLinkClicked)) { 
     return ![ [ UIApplication sharedApplication ] openURL: [ requestURL autorelease ] ]; 
    } 

    [ requestURL release ]; 

    return YES; 
} 

預期爲0的日誌輸出,並獲得作爲5..I不明白該怎麼做..

+0

在'[request URL]'上仍然有'retain'的內存泄漏。 '保留'是沒有必要的。擺脫它。 – 2010-09-02 23:01:26

回答

1
NSURL *requestURL = [request URL]; 
NSString* urlString = [requestURL absoluteString]; 
if ([urlString isEqualToString: @"http://www.example.com"]) 
{ 

    // Try this, it will work fine 

} 
1

更改
(navigationType == UIWebViewNavigationTypeLinkClicked)

(navigationType == UIWebViewNavigationTypeLinkClicked || navigationType == UIWebViewNavigationTypeOther)