Attempt to Display Local PDF With WKWebView Results in White Screen - ios

I have a PDF file stored in my local applications directory. I wrote the following code years ago to display the pdf in a webView. It has worked flawlessly, but no longer works. It instead displays a white screen.
NSString* pdfFileName = [self getPDFFileName];
WKWebView* webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
NSURL *url = [NSURL URLWithString:pdfFileName];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
[self.view addSubview:webView];
I have since tried other methods of WKWebView such as:
NSData *data = [NSData dataWithContentsOfFile:pdfFileName];
[webView loadData:data MIMEType:#"application/pdf" characterEncodingName:#"" baseURL:[NSURL URLWithString:pdfFileName]];
or
[webView loadFileURL:url allowingReadAccessToURL:url];
but these don't work either.
What changed that is preventing a local pdf from displaying?

You need to use:
[NSURL fileURLWithPath:<valid_file_path>]
Always use your file browser to confirm both the file path and the path itself are valid in case of any issues.

Related

images are not updating in the webview

I am using the below code,
NSURL * url=[NSURL fileURLWithPath:htmlfilepath];
NSURLRequest * request=[[NSURLRequest alloc] initWithURL:url];
[webview loadRequest:request];
All the contents from html are updating to pdf except the signature.
UIWebView: Images are not updating
But I am still facing the same problem.
If you are using same url with updated content use this.
UIWebView* webV=[[UIWebView alloc]init];
NSString* htmlString= #"Loading...";
[webV loadHTMLString:htmlString baseURL:nil];
[webV loadRequest:[NSURLRequest requestWithURL:mainURL]];

Image doesn't fit web view

Am displaying a Image on a web view and it doesn't fit the web view completely and shows blank white space around the border.How can i fit or scale the image completely to the size of web view?
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
self.webView.backgroundColor=[UIColor blackColor];
self.webView.delegate = self;
[self.webView setScalesPageToFit:YES];
Try this html code snippet
<html><body><img src='%#' width='100%' height='100%'></body></html>
It will fill the web view completely without any white space.
try to load like this
- (void) showImageWithLoadHTMLString {
// Create URL string for image file location
NSURL *imageURL = [NSURL fileURLWithPath: path];
// Create HTML string from image URL
NSString *htmlString = #"<html><body><img src='%#' width='900'></body></html>";
NSString *imageHTML = [[NSString alloc] initWithFormat:htmlString, imageURL];
// Load image in UIWebView
self.webView.backgroundColor=[UIColor blackColor];
self.webView.scalesPageToFit = YES;
[self.webView loadHTMLString:imageHTML baseURL:nil];
}

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. 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.
This code works just fine in iOS 7 , screenshot attached. 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];

Web content doesn't fit webview's frame?

I'm using a UIWebView to display a mobile website on my iPhone. The UiWebView is smaller than the display of the iPhone.
My Problem is that the content size of the website (a mobile website based on JQuery mobile) doen't fit the size of the web view.
Just an example with a small web view trying to display the google mobile website:
NSString *urlAdress = #"http://google.de";
NSURL *url = [NSURL URLWithString:urlAdress];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
[webView sizeToFit];
Primarily : set the delegate to the WebView
In .h file
#interface WebViewViewController : UIViewController<UIWebViewDelegate>
In .m file
webView.delegate =self;
webView.scalesPageToFit = NO;
NSString *urlAdress = #"http://google.de";
NSURL *url = [NSURL URLWithString:urlAdress];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
add these to your code for your webview :
[webView setScalesPageToFit:YES];
[webView setContentMode:UIViewContentModeScaleAspectFit];

UIWebview doesen't display PDF

I have used this code for displaying PDF on webview.
webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
NSString *path = [[NSBundle mainBundle] pathForResource:#"fonts1" ofType:#"pdf"];
NSLog(#"%#",path);
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webview loadRequest:request];
webview.scalesPageToFit=YES;
[self.view addSubview:webview];
PDF in bundle I get PDF correctly.
What is wrong in this code.
try below one
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
NSString *path = [[NSBundle mainBundle] pathForResource:#"document" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
[webView release];
If it is local then just put
UIWebView *WebViewObj = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
[self.view addSubview:WebViewObj];
[self.WebViewObj loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"filename" ofType:#"pdf"]]]];
If you are trying to display a PDF file residing on a server somewhere, you can simple load it to your web view directly:
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
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];
[webView release];
Or if you have a PDF file bundled with your application (in this example named "document.pdf"):
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
NSString *path = [[NSBundle mainBundle] pathForResource:#"document" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
[webView release];
Let me know if you face any problem.
if i add
UIWebView *
at the beginning of your code everything works fine already tested
i dont think that is your problem
you should check the name of the file maybe is misspelled or check if the pdf file appears with red letters
if red letters the file is missing/damaged/moved
good luck
I think your pdf file not in target member so remove file and add like below
sagarcool89,SAMIR RATHOD,and dhaya: Every one given the right solutions:
UIWebView *pdfDisplay = [[UIWebView alloc] initWithFrame:self.view.frame];
NSURL *pathURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"IHI0053A_acle" ofType:#"pdf"]];
[pdfDisplay loadRequest:[NSURLRequest requestWithURL:pathURL]];
[self.view addSubview:pdfDisplay];
and it's the correct way to load pdf file in uiwebview.

Resources