I have downloaded a PDF file form a web url and saved it on disk. I want to view the same file in webView. i used:
[self.navigationController pushViewController:self.fileViewController animated:YES];
i am using this code to navigate to other view controller. Following is my code in viewDidLoad:
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
NSString *path = [[NSBundle mainBundle] pathForResource:#"myFile" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
}
Just tell me that where i am wrong. thanks in anticipation.
I have checked your code and it works. May be you need to add webview as subview to your view controller by using following code: [self.view addSubview:webView];
I hope it will help you...
- (void)viewDidLoad
{
[super viewDidLoad];
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
[self.view addSubview:webView];
NSString *path = [[NSBundle mainBundle] pathForResource:#"myFile" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
}
Related
i want to display a web page in my app but it doesn't work
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIWebView *web1 = [[UIWebView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 640.0)];
NSURL *URL = [NSURL URLWithString:#"http://google.com"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:URL];
web1.delegate = self ;
[web1 loadRequest:requestObj];
[self.view addSubview:web1];
}
just replace this method
NSURL *URL = [NSURL URLWithString:#"http://google.com"];
in to
NSURL *URL = [NSURL URLWithString:#"https://www.google.com"];
i tried your code is
UIWebView *web1 = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];
NSURL *URL = [NSURL URLWithString:#"https://www.google.co.in"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:URL];
[web1 loadRequest:requestObj];
[self.view addSubview:web1];
the output is
I have a view controller that has a UIWebView. I want to load a html file in the UIWebView.
I have kept my html file under the Supporting files(Generated by Xcode)->Resources->html.
Also , I have created the html file and dragged and dropped in to the Html folder.
My code:
-(void)viewDidLoad
{
[super viewDidLoad];
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"contact"
ofType:#"html" inDirectory:#"Resources/html"] ;
NSURL *url = [NSURL fileURLWithPath:htmlFile ];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
_webView.delegate=(id)self;
}
What am I missing here . Looks a silly one but no luck..
Please help.
Replace your view did load method with this method. I checked with it. It's working for me. See that your html file should be placed as shown in image.(As per your need)
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIWebView *webView=[[UIWebView alloc]init];
webView.frame=CGRectMake(0, 0, 320, 568);
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"contact" ofType:#"html" inDirectory:nil];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
//Append javascript
//NSString *script = #"<script>alert(\"This is an alert!!\");</script>";
//htmlString = [htmlString stringByAppendingString:script];
webView.delegate = self;
[webView loadHTMLString:htmlString baseURL:nil];
[self.view addSubview:webView];
}
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.
I have loaded a PDF file in a UIWebView from the resource bundle.
Now, I am not able to zoom/pinch the PDF file.
I found the below 2 relevant links, but none of the answers are marked as correct -
How to zoom webView with pdf file
Enable zooming/pinch on UIWebView
How to make the PDF zoomable/pinchable which has been loaded in a UIWebView from the resource bundle, Will the below solution work ?
Problem with pdf while opening in UIWebView
Thanks for your help.
In your WebView on Interface Builder add check to Scale Page To Fit and you enable a Pinch ZoomIn ZoomOut ;)
Or if you want to lad better your PDF try to Look this code:
- (void)viewDidLoad
{
[super viewDidLoad];
[webView loadRequest:[NSURLRequest requestWithURL:#"http:pdfURL"]];
NSString *path = [[NSBundle mainBundle] pathForResource:#"yourPDFFile" ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest * request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
//--------------AND HERE USE SCALE PAGE TO FIT------------------//
[webView setScalesPageToFit:YES];
}
Hope this help you.
//Try like this
NSString *urlstr=#"www.example.com/yy.pdf";
web=nil;
web=[[UIWebView alloc] initWithFrame:CGRectMake(0, 98, 320, 367)];
web.delegate=self;
[self.view addSubview:web];
[web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlstr]]];
UIPinchGestureRecognizer *pgr = [[UIPinchGestureRecognizer alloc]
initWithTarget:self action:#selector(handlePinch:)];
pgr.delegate = self;
[web addGestureRecognizer:pgr];
// Target Action
- (IBAction)handlePinch:(UIPinchGestureRecognizer *)recognizer
{
recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
recognizer.scale = 1;
}
Before this add UIGestureRecognizerDelegate in .h. Hope it will helps you..
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:documentsDirectory])
{
NSURL *url = [NSURL fileURLWithPath:strFileName];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView setScalesPageToFit:YES];
[webview loadRequest:request];
}
I am trying to view a PDF file from a server but I only get a view in black and doesn't contain anything. Here's my code:
pdfViewer = [[UIWebView alloc] initWithFrame:CGRectMake(0, 44, 768, 949)];
pdfViewer.backgroundColor = [UIColor whiteColor];
pdfViewer.delegate = self;
[sgContainer addSubview:pdfViewer];
chatSpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
chatSpinner.frame = CGRectMake(334, 462, 100, 100);
[pdfViewer addSubview:chatSpinner];
NSString *httpSource = #"http://www.mywebsite.com/folder/subfolder/file.pdf";
NSURL *fullUrl = [NSURL URLWithString:httpSource];
NSURLRequest *httpRequest = [NSURLRequest requestWithURL:fullUrl];
[pdfViewer loadRequest:httpRequest];
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.