简体   繁体   English

无法从UIWebview for ios 7中的url加载文件(doc,pdf等)

[英]Not able to load the file(doc,pdf etc) from url in UIWebview for ios 7

I am loading different file types like PDF, Excel, Doc etc in UIWebview. 我在UIWebview中加载不同的文件类型,如PDF,Excel,Doc等。 Some files requires authorization and passed the value in header. 某些文件需要授权并在标头中传递值。

This works fine in ios 6. Not working in ios 7. Below is the code and error message. 这在ios 6中工作正常。不在ios 7中工作。下面是代码和错误消息。

NSURL *url =[NSURL URLWithString:regularURL];
self.webView.scalesPageToFit=YES;
self.request = [NSMutableURLRequest requestWithURL:url];
[self.request setValue:@"multipart/form-data" forHTTPHeaderField:@"Accept"];
NSString *auth = [NSString stringWithFormat:@"Bearer %@",userToken];
[self.request setValue:auth forHTTPHeaderField:@"Authorization"];

Error Message: 错误信息:

Error Domain=WebKitErrorDomain Code=102 "Frame load interrupted" UserInfo=0xd4b5310 {

Is there any additional header field to be passed for ios 7 web view? 是否有任何额外的头字段要传递给ios 7 Web视图?

I have tried to solve the problem with NSURLCache solution but that didn't work for me. 我试图用NSURLCache解决方案解决问题,但这对我不起作用。

You have to try next: 你必须尝试下一个:

    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];

    NSString *strUrl = [strUrl stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
    NSURL *targetURL = [NSURL URLWithString:strUrl];

    NSData *dataFromUrl = [NSData dataWithContentsOfURL:[NSURL URLWithString: strUrl]];
    [webView loadData:dataFromUrl MIMEType:@"application/pdf" textEncodingName:nil baseURL:nil];

    [self.view addSubview:webView];

It work for the all files that have not worked before (pdf, doc, etc). 它适用于以前没有工作过的所有文件(pdf,doc等)。

I have kind of found a solution. 我有点找到了解决方案。 Not perfect but it works! 不完美,但它的工作原理!

Set a shared URL cache before loading the request, then intercept the error and load the cached data with the correct MIME type into the webView manually. 在加载请求之前设置共享URL缓存,然后拦截错误并手动将具有正确MIME类型的缓存数据加载到webView中。

NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024 diskCapacity:256 * 1024 * 1024 diskPath:nil];
[NSURLCache setSharedURLCache:URLCache];

And then 接着

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
NSCachedURLResponse* cachedResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:self.originalRequest];
if (cachedResponse) {
        CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)(cachedResponse.response.URL.pathExtension), NULL);
        CFStringRef MIMEType = UTTypeCopyPreferredTagWithClass(UTI, kUTTagClassMIMEType);
        CFRelease(UTI);
        NSString* MIMETypeString = (__bridge_transfer NSString *)MIMEType;

        [self.webView loadData:cachedResponse.data MIMEType:MIMETypeString textEncodingName:nil baseURL:nil];
    }
}

Naturally, you must set your WebViews delegate to somewhere you put the above delegate method. 当然,您必须将WebViews委托设置为放置上述委托方法的位置。

This code works just fine in iOS 7 , screenshot attached. 此代码在iOS 7中运行正常,附带截图。 Hope it helps... 希望能帮助到你...

UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 320, 480)];

NSURL *targetURL = [NSURL URLWithString:@"http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIWebView_Class/UIWebView_Class.pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];     
[self.view addSubview:webView];

iOS 7截图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM