2013-10-10 31 views
7

我在UIWebview中加載不同的文件類型,如PDF,Excel,Doc等。某些文件需要授權並在標頭中傳遞值。無法從UIWebview中的url加載文件(doc,pdf等)for ios 7

這在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幅視圖傳遞任何額外的頭部字段?

+0

這裏有一些問題,試圖從Amazon S3加載PDF。我的文件有這裏提到的正確的.pdf擴展名http://stackoverflow.com/questions/1482736/frame-load-interrupted-error-while-loading-a-word-document-in-uiwebview。 –

+0

嘗試像這樣:NSString * strP = [[NSString alloc] initWithContentsOfFile:htmlFileP encoding:encodingP error:nil]; [self.webviewP loadData:[strP dataUsingEncoding:encodingP] MIMEType:@「text/html」textEncodingName:@「UTF-8」baseURL:urlP]; – itechnician

+0

我不清楚你在哪一點出錯。你在webview上調用loadRequest:傳遞self.request?那是什麼時候發生錯誤? – TomSwift

回答

3

我有種找到了解決方案。不完美,但它的作品!

在加載請求之前設置共享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]; 
    } 
} 

當然,您必須在網頁視圖的委託設置在某處你把上面的委託方法。

-1

此代碼在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]; 

iOS 7 Screenshot

+1

由於某些原因,這對我的文件不起作用。我認爲我和ram的文件或服務器是特殊的。 –

5

我試圖解決與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等)工作過的所有文件。