2010-05-14 11 views

Respuesta

265

Sí, esto se puede hacer con UIWebView.

Si usted está tratando de mostrar un archivo PDF que reside en un servidor en algún lugar, sólo tiene que cargarlo en la vista web directamente:

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 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 un amplio llamado "DOCUMENTO.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 code 
let request = NSURLRequest(URL: targetURL) 
webView.loadRequest(request) 

view.addSubview(webView) 

Puede encontrar más información aquí: Technical QA1630: Using UIWebView to display select document types.

+0

Muchas gracias ... alleus. ¿Podemos guardarlo en el directorio de documentos local en iPhone? –

+3

Sí, es posible, pero debe usar otro método para recuperar el documento que no sea WebView. Búscalo aquí en Stack Overflow. Por ejemplo: http://stackoverflow.com/questions/2102267/downloading-a-file-from-url-and-saving-to-resources-on-iphone – alleus

+0

Utilicé su código pero no funcionó bien para mí .... – Jitendra

9

uso de este para el archivo PDF abierto en 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]; 
3
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]; 
+0

Primero, cree la ruta de su pdf y conviértalo en url y use este código para cargar la vista web; funciona para mí, así que por favor use el mismo en su código – dheerendra

2

Una actualización de la respuesta de Martin Alléus, para obtener la pantalla completa si se trata de un teléfono o un iPad sin tener que codificar:

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

webviews también se puede cargar el archivo PDF utilizando loadData método, si se adquiere como NSData:

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

versión Swift:

if let docPath = NSBundle.mainBundle().pathForResource("sample", ofType: "pdf") { 
    let docURL = NSURL(fileURLWithPath: docPath) 
    webView.loadRequest(NSURLRequest(URL: docURL)) 
} 
1
NSString *path = [[NSBundle mainBundle] pathForResource:@"document" ofType:@"pdf"]; 
NSURL *targetURL = [NSURL fileURLWithPath:path]; 
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL]; 
[webView loadRequest:request]; 
0
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]; 
0

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) 
Cuestiones relacionadas