4

I am receiving a description text in HTML format, and I am loading it in a webview, if a link clicked in the description so I load it in separate view controller. But shouldStartLoadWithRequest giving a some appended link. here is my code

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

it prints this

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

and I am loading it like this

[webViewDescription loadHTMLString:description baseURL:nil];
4

2 回答 2

20

当您使用loadHTMLString并且设置baseURL为 nil 时,因此iOS 使用applewebdata URI 方案,而不是用于访问设备内部资源的 URI 中的“http”。你可以尝试设置baseURL

于 2013-03-12T10:37:03.763 回答
7

我有一个类似的问题。在实践中,将baseURL'http://' 或类似的设置也对我不起作用。我也只看到了applewebdata大约 50% 的时间方案,另外 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;
}
于 2015-04-12T22:41:28.287 回答