I am loading different file types like PDF, Excel, Doc etc in UIWebview. 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.
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?
I have tried to solve the problem with NSURLCache solution but that didn't work for me.
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).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With