2015-08-22 83 views
0

我正在为Ios开发一个Epub Reader,注意我没有使用任何epub库,并且我自己解析了epub。Ios通过NSURLCache拦截UIWebview请求并替换响应

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

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

,所以我可以拦截请求,并从EPUB找到正确的数据,然后创建一个NSCachedURLResponse并返回。

迄今为止好。 但是问题在于数据(即图像)不会显示在web视图中。看看下面的代码:

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

在这一点上一切看起来都很正常,该方法获取调用,被找到了正确的数据和响应对象的回报,但它的图像不会加载到web视图。

那么是什么问题?

tnx提前。

注意:我是新的IOS编程世界(从Android的未来:)

回答