iPhone: ¿podemos abrir un archivo pdf usando UIWebView?

¿Podemos abrir el archivo pdf desde UIWebView?

Si alguien ha implementado esto, por favor ayudame.

Cualquier fragmento de código o cualquier enlace web ayudaría.

Sí, esto se puede hacer con UIWebView.

Si está intentando mostrar un archivo PDF que reside en un servidor en alguna parte, simplemente puede cargarlo en su vista web directamente:

C objective

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]; 

Rápido

 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 code let request = NSURLRequest(URL: targetURL) webView.loadRequest(request) view.addSubview(webView) 

O si tiene un archivo PDF incluido con su aplicación (en este ejemplo llamado “document.pdf”):

C objective

 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]; 

Rápido

 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 code let request = NSURLRequest(URL: targetURL) webView.loadRequest(request) view.addSubview(webView) 

Puede encontrar más información aquí: Técnico QA1630: Uso de UIWebView para visualizar los tipos de documentos seleccionados .

Webviews también puede cargar el pdf usando el método loadData , si lo adquiere como NSData:

 [self.webView loadData:self.pdfData MIMEType:@"application/pdf" textEncodingName:@"UTF-8" baseURL:nil]; 

use esto para abrir el archivo pdf en la vista web

 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]; 
 NSString *folderName=[NSString stringWithFormat:@"/documents/%@",[tempDictLitrature objectForKey:@"folder"]]; NSString *fileName=[tempDictLitrature objectForKey:@"name"]; [self.navigationItem setTitle:fileName]; NSString *type=[tempDictLitrature objectForKey:@"type"]; NSString *path=[[NSBundle mainBundle]pathForResource:fileName ofType:type inDirectory:folderName]; NSURL *targetURL = [NSURL fileURLWithPath:path]; NSURLRequest *request = [NSURLRequest requestWithURL:targetURL]; webView=[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 1024, 730)]; [webView setBackgroundColor:[UIColor lightGrayColor]]; webView.scalesPageToFit = YES; [[webView scrollView] setContentOffset:CGPointZero animated:YES]; [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"window.scrollTo(0.0, 50.0)"]]; [webView loadRequest:request]; [self.view addSubview:webView]; 

Versión Swift:

 if let docPath = NSBundle.mainBundle().pathForResource("sample", ofType: "pdf") { let docURL = NSURL(fileURLWithPath: docPath) webView.loadRequest(NSURLRequest(URL: docURL)) } 

Una actualización de la respuesta de Martin Alléus, para obtener la pantalla completa, ya sea un teléfono o un iPad sin tener que codificarlo:

 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]; 
 NSString *path = [[NSBundle mainBundle] pathForResource:@"document" ofType:@"pdf"]; NSURL *targetURL = [NSURL fileURLWithPath:path]; NSURLRequest *request = [NSURLRequest requestWithURL:targetURL]; [webView loadRequest:request]; 

Swift 4

  let webView = UIWebView(frame: view.bounds) let targetURL = Bundle.main.url(forResource: "sampleFileName", withExtension: "pdf") let request = URLRequest(url: targetURL!) webView.loadRequest(request) view.addSubview(webView) 
 UIWebView *pdfWebView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)]; NSURL *targetURL = [NSURL URLWithString:@"http://unec.edu.az/application/uploads/2014/12/pdf-sample.pdf"]; NSURLRequest *request = [NSURLRequest requestWithURL:targetURL]; [pdfWebView loadRequest:request]; [self.view addSubview:pdfWebView];