Can we open pdf file using UIWebView on iOS?
Yes, this can be done with the UIWebView.
If you are trying to display a PDF file residing on a server somewhere, you can simply load it in your web view directly:
Objective-C
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];NSURL *targetURL = [NSURL URLWithString:@"https://www.example.com/document.pdf"];NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];[webView loadRequest:request];[self.view addSubview:webView];
Swift
let webView = UIWebView(frame: CGRect(x: 10, y: 10, width: 200, height: 200))let targetURL = NSURL(string: "https://www.example.com/document.pdf")! // This value is force-unwrapped for the sake of a compact example, do not do this in your codelet request = NSURLRequest(URL: targetURL)webView.loadRequest(request)view.addSubview(webView)
Or if you have a PDF file bundled with your application (in this example named "document.pdf"):
Objective-C
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];NSURL *targetURL = [[NSBundle mainBundle] URLForResource:@"document" withExtension:@"pdf"];NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];[webView loadRequest:request];[self.view addSubview:webView];
Swift
let webView = UIWebView(frame: CGRect(x: 10, y: 10, width: 200, height: 200))let targetURL = NSBundle.mainBundle().URLForResource("document", withExtension: "pdf")! // This value is force-unwrapped for the sake of a compact example, do not do this in your codelet request = NSURLRequest(URL: targetURL)webView.loadRequest(request)view.addSubview(webView)
You can find more information here: Technical QA1630: Using UIWebView to display select document types.
use this for open pdf file in webview
NSString *path = [[NSBundle mainBundle] pathForResource:@"mypdf" ofType:@"pdf"];NSURL *targetURL = [NSURL fileURLWithPath:path];NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];UIWebView *webView=[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];[[webView scrollView] setContentOffset:CGPointMake(0,500) animated:YES];[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"window.scrollTo(0.0, 50.0)"]];[webView loadRequest:request];[self.view addSubview:webView];[webView release];