2013-03-12 122 views
3

我收到HTML格式的描述文本,並且我在webview中加載它,如果鏈接在描述中單擊,所以我將它加載到單獨的視圖控制器中。但是,shouldStartLoadWithRequest會給出一些附加鏈接。這裏是我的代碼shouldStartLoadWithRequest附加鏈接與applewebdata

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { 
if(navigationType == UIWebViewNavigationTypeLinkClicked) { 

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 
    WebsiteViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"WebsiteViewController"]; 
    vc.url = request.URL.absoluteString; 
    NSLog(@"link is : %@", [[request URL] absoluteString]); 
    [self.navigationController pushViewController:vc animated:YES]; 
    return false; 
} 
return true; 
} 

它打印此

link is : applewebdata://038EEEBF-A4C9-4C7D-8FB5-32056714B855/www.yahoo.com 

和我加載像這樣

[webViewDescription loadHTMLString:description baseURL:nil]; 

回答

18

當您使用loadHTMLString和你設置baseURL到零,因此applewebdata URI方案由iOS使用,而不是用於訪問設備上內部資源的URI中的「http」 。你可以嘗試設置baseURL

+1

我應該爲baseURL而不是baseURL:無? – 2013-09-19 09:50:19

+1

我遇到了這個問題,爲了讓鏈接正確加載,我將基URL設置爲@「http://」 – BreadicalMD 2013-10-18 21:56:58

+4

只是一個小小的修復。 baseURL參數需要一個NSURL對象,所以它應該是'[NSURL URLWithString:@「http://」]'不是'@「http://」'。 – Hlung 2013-11-25 10:50:55

4

我有類似的問題。實際上,將baseURL設置爲'http://'或類似的東西也不適合我。我只在50%的時間內看到applewebdata方案,另外50%的時間我看到了我期待的正確方案。

爲了解決這個問題,我最終攔截了-webView:shouldStartLoadWithRequest:navigationType:回調並使用正則表達式去掉Apple的applewebdata方案。這是它看起來像什麼

// Scheme used to intercept UIWebView callbacks 
static NSString *bridgeScheme = @"myCoolScheme"; 

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

    NSURL *requestURL = request.URL; 

    // Strip out applewebdata://<UUID> prefix applied when HTML is loaded locally 
    if ([requestURL.scheme isEqualToString:@"applewebdata"]) { 
     NSString *requestURLString = requestURL.absoluteString; 
     NSString *trimmedRequestURLString = [requestURLString stringByReplacingOccurrencesOfString:@"^(?:applewebdata://[0-9A-Z-]*/?)" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, requestURLString.length)]; 
     if (trimmedRequestURLString.length > 0) { 
      requestURL = [NSURL URLWithString:trimmedRequestURLString]; 
     } 
    } 

    if ([requestURL.scheme isEqualToString:bridgeScheme]) { 
     // Do your thing 
     shouldStartLoad = NO; 
    } 

    return shouldStartLoad; 
}