- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSString *path = [[NSBundle mainBundle] pathForResource:#"About" ofType: #"pdf"];
NSLog(#"%#", path);
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[_webView loadRequest:request];
}
just use following code for open PDF in web view
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"About" ofType:#"pdf"];
[self.webVctr loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];
And check it out the PDF is exist or not?
This thinks may helping to you
Happy codeing..
Try this:
in the
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
NSString *path = [[NSBundle mainBundle] pathForResource:#"About" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
[webView release];
It was some other issue not related to the pdf loading over web view. I cross checked and found the actual cause of crash. Anyways thanks guys for the help
Related
I’m using the code below to display pdfs or filename.doc Microsoft Word documents. It works fine when I use a ofType:#:”pdf”, but my code crashes when I use ofType:#:”doc” or ofType:#:”docx”. The Word document is mostly text and a few url links. Any help appreciated.
NSString *path = [[NSBundle mainBundle] pathForResource:#"C9" ofType:#"doc"];
pdfUrl = [NSURL fileURLWithPath:path];}
[webView loadRequest:[NSURLRequest requestWithURL:pdfUrl]];
As per your code, it looks like you're already using a UIWebView.
Here is some code I've used in the past.
-(void)loadDocument:(NSString*)documentName inView:(UIWebView*)webView {
NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:nil];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
In viewDidLoad (change webView frame to whatever you need)..
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
[self.view addSubview:webView];
[self loadDocument:#"file.doc" inView:webView];
Take a look at using QLPreviewController to display these document types.
For the crash, are you sure the file exists at the specified location? Check it's actually being copied to the bundle and that there is a file at that path / URL before you use it.
I am display local pdf file in UIWebView but it shows exception in path here is my code
NSString*fileName=content_Source;
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView setScalesPageToFit:YES];
[webView loadRequest:request]
You must be using the file name extension while setting the file name. So make sure the extension will not be used in file name if it's setting along with Type (like ofType:#"pdf").
Here's my code:
UIWebView *webView = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
webView.backgroundColor = [UIColor redColor];
NSString*fileName= #"About_Downloads";
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:#"pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:path isDirectory:NO]];
[webView setScalesPageToFit:YES];
[webView loadRequest:request];
[self.view addSubview:webView];
There's a better way to show PDFs:
UIDocumentInteractionController *dc = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:path isDirectory:NO]];
dc.delegate = self;
[dc presentPreviewAnimated:YES];
Make sure use the delegate along with that code to work it properly.
(UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
return self;
}
first validate that string and the assign to URL like bellow..
NSString *strURLPath = [self trimString: path];
NSURL *url = [NSURL fileURLWithPath:strURLPath];
use my bellow method...
-(NSString*) trimString:(NSString *)theString {
if (theString != [NSNull null])
theString = [theString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
return theString;
}
It is working fine with the given code problem was that in my file Name i was giving also the type like ali.pdf they type pdf so it get exception it worked fine now when i use filename like following
NSString*fileName=#"ali";
Instead of
NSString*fileName=#"ali.pdf";
CGRect rect = [[UIScreen mainScreen] bounds];
CGSize screenSize = rect.size;
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)];
NSString *path = [[NSBundle mainBundle] pathForResource:#"pdf" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
I am having a strange issue. UIwebview is not showing PDF file properly as it shows when I load the PDF file from web URL or in safari browser.
http://www.winsolz.com/temp/2.jpg
I am using the following code to open the PDF from local system.
NSString *pathFOrPDF = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"Page%#", strPDFNumber] ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:pathFOrPDF];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[myWebview loadRequest:request];
[myWebviewLandscape loadRequest:request];
Thanks.
I don't think it would fix your case but I had a pdf showing in safari and not showing in a UIWebView and my problem was that the file extension was not correct. I appended a .pdf at the end of the local file url and started working.
I've noticed that safari check also content types so maybe it is working on safari as you provide a remote url?
this might help.
UIWebView *webView = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
webView.backgroundColor = [UIColor redColor];
NSString*fileName= #"About_Downloads";
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:#"pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:path isDirectory:NO]];
[webView setScalesPageToFit:YES];
[webView loadRequest:request];
[self.view addSubview:webView]
ihave write the following code
but it does nothing in consloe it shows Unknown scheme, doing nothing
[webview setDeleagte self];
[webView loadRequest:[[NSURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:sourcelinkStr]]];
NSString *strURL = #"https://facebook.com";
NSURL *url = [NSURL URLWithString:strURL];
webView.delegate = self;
[webView loadRequest:[NSURLRequest requestWithURL:url]];
now ,
we have to define Methods For LoadRequest
thanks..
I have HTML files that I want to load locally. I have included the files in the resources folder in XCode. I am not sure what the syntax is to load them.
This is the code I use to connect to say google.
NSString *urlAddress=#"http\\someurl";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webHelpView loadRequest:requestObj];
Could someone provide an example where a HTML file is loaded locally.
Thanks in advance.
This is the only code that worked for me.
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"SettingsHelp.html"];
NSURL *url = [NSURL fileURLWithPath:path isDirectory:NO];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
Thanks all for the help.
Can't you just do
NSURL *url = [NSURL URLWithString:#"file:///path/to/file"];
?
Sure, here is some code from an app that loads a locallly stored html page into a web view:
- (void) viewDidLoad
{
// Load the content into the view
NSString* path = [NSString stringWithFormat: #"%#/index.html",
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0]];
[webView_ loadRequest: [NSURLRequest requestWithURL: [NSURL fileURLWithPath: path]]];
}