shouldStartLoadWithRequest附加与applewebdata的链接

时间:2013-03-12 10:24:59

标签: iphone ios objective-c uiwebview

我正在接收HTML格式的描述文本,我正在webview中加载它,如果在描述中单击了链接,那么我将它加载到单独的视图控制器中。但是,shouldStartLoadWithRequest给出一些附加链接。这是我的代码

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

2 个答案:

答案 0 :(得分:20)

当您使用loadHTMLString并且您将baseURL设置为nil因此 applewebdata iOS方案被iOS用来代替用于访问内部的URI中的“http”设备上的资源。您可以尝试设置baseURL

答案 1 :(得分:5)

我有类似的问题。在实践中,将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;
}