Podemos abrir o arquivo pdf em UIWebView?
Podemos abrir o arquivo pdf em UIWebView?
Respostas:
Sim, isso pode ser feito com o UIWebView.
Se você estiver tentando exibir um arquivo PDF residente em um servidor em algum lugar, pode simplesmente carregá-lo diretamente em sua visualização da web:
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];
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)
Ou se você tiver um arquivo PDF em seu aplicativo (neste exemplo, denominado "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];
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)
Você pode encontrar mais informações aqui: Técnico QA1630: Usando UIWebView para exibir tipos de documentos selecionados .
UIWebview
s também podem carregar o método .pdf
using loadData
, se você adquiri-lo como NSData
:
[self.webView loadData:self.pdfData
MIMEType:@"application/pdf"
textEncodingName:@"UTF-8"
baseURL:nil];
use isso para abrir o arquivo pdf no 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];
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];
Versão Swift:
if let docPath = NSBundle.mainBundle().pathForResource("sample", ofType: "pdf") {
let docURL = NSURL(fileURLWithPath: docPath)
webView.loadRequest(NSURLRequest(URL: docURL))
}
Uma atualização na resposta de Martin Alléus, para obter a tela inteira, seja um telefone ou um iPad, sem ter 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];
NSString *path = [[NSBundle mainBundle] pathForResource:@"document" ofType:@"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
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];
WKWebView : Acho que esta questão é o melhor lugar para permitir que as pessoas saibam que devem começar a usar o WKWebview, pois o UIWebView agora está obsoleto .
Objetivo C
WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.frame];
webView.navigationDelegate = self;
NSURL *nsurl=[NSURL URLWithString:@"https://www.example.com/document.pdf"];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webView loadRequest:nsrequest];
[self.view addSubview:webView];
Rápido
let myURLString = "https://www.example.com/document.pdf"
let url = NSURL(string: myURLString)
let request = NSURLRequest(URL: url!)
let webView = WKWebView(frame: self.view.frame)
webView.navigationDelegate = self
webView.loadRequest(request)
view.addSubview(webView)
Não copiei este código diretamente do Xcode, então pode, pode conter algum erro de sintaxe. Por favor, verifique ao usá-lo.