无法从ios 7的UIWebview中的url加载文件(文档、pdf等)

无法从ios 7的UIWebview中的url加载文件(文档、pdf等),ios,uiwebview,ios7,Ios,Uiwebview,Ios7,我正在UIWebview中加载不同的文件类型,如PDF、Excel、Doc等。某些文件需要授权,并在标头中传递了值 这在ios 6中运行良好。不在ios 7中工作。 下面是代码和错误消息 NSURL *url =[NSURL URLWithString:regularURL]; self.webView.scalesPageToFit=YES; self.request = [NSMutableURLRequest requestWithURL:url]; [self.request setVa

我正在UIWebview中加载不同的文件类型,如PDF、Excel、Doc等。某些文件需要授权,并在标头中传递了值

这在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 Domain=WebKitErrorDomain Code=102 "Frame load interrupted" UserInfo=0xd4b5310 {

ios 7 web视图是否需要传递其他标题字段?

我找到了一种解决方案。不完美,但它的工作

在加载请求之前设置共享URL缓存,然后拦截错误并手动将具有正确MIME类型的缓存数据加载到webView中

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

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

当然,您必须将WebViews委托设置为放置上述委托方法的位置。

此代码在iOS 7中运行良好,附屏幕截图。希望对你有帮助

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

我曾尝试使用NSURLCache解决方案解决该问题,但对我来说不起作用

您必须尝试下一步:

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

它适用于所有以前未使用过的文件(pdf、doc等)。

这里有一些问题,试图从Amazon S3加载pdf。我的文件具有此处提到的正确的.pdf扩展名。请这样尝试:NSString*strP=[[NSString alloc]initWithContentsOfFile:htmlFileP encoding:encodingP error:nil];[self.webviewP loadData:[strP dataUsingEncoding:encodingP]MIMEType:@“text/html”textcodengname:@“UTF-8”baseURL:urlP];我不清楚你是在什么时候出错的。您是否在webview上调用loadRequest:传递self.request?这就是错误发生的时候吗?出于某种原因,这对我的文件不起作用。我觉得我的和ram的文件或服务器有点特别。