【问题标题】:Ios Intercept UIWebview request via NSURLCache and replace the responseios通过NSURLCache拦截UIWebview请求并替换响应
【发布时间】:2015-08-22 08:57:53
【问题描述】:

我正在为 Ios 开发一个 Epub 阅读器,请注意我不使用任何 epub 库并且我自己解析 epub。

我需要一种将资源从 epub 加载到 UIWebView 的方法,例如图像或 CSS 文件...(在 html 文件中请求的资源)。

为此,我决定使用NSURLCache 类并覆盖它的方法(NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request

这样我就可以拦截请求并从 epub 中找到正确的数据,然后创建一个 NSCachedURLResponse 并返回它。

到目前为止一切顺利。 但是问题是数据(即图像)不会显示在 webview 中。看下面的代码:

- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request
{
    /*
    the request.URL is something like this applewebdata://UUID/OEBPS/Images/cover.jpg ,
    so the getResourcePathFromRequest give me the path OEBPS/Images/cover.jpg,
    so i can find the right data in epub
    */
    NSString *resourcePath = [self getResourcePathFromRequest:request.URL];
    /*
    ResourceResponse is a class contaning data and mimeType, 
    i tested this and the data and mimeType are good so this is not the problem
    */
    ResourceResponse *response = [[self getBook] fetch:resourcePath];

    NSURLResponse *urlResponse = [[NSURLResponse alloc] initWithURL:[request URL] MIMEType:response.mMimeType expectedContentLength:[response.mData length] textEncodingName:@"UTF-8"];
    NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:urlResponse data:response.mData];

    return cachedResponse;
}

此时一切看起来正常,方法被调用,找到正确的数据并返回响应对象,但它不会将图像加载到 webview 中。

那么问题出在哪里?

tnx 提前。

注意:我是 ios 编程世界的新手(来自 android:)

【问题讨论】:

    标签: ios objective-c uiwebview nsurlcache


    【解决方案1】:

    我使用 base url (http://localhost) 加载了数据,一切正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-25
      • 2017-11-07
      • 2012-12-13
      • 1970-01-01
      • 2020-10-22
      • 1970-01-01
      • 2013-11-07
      相关资源
      最近更新 更多