如何使用ASIHTTP将PDF文档添加到UIWebView

时间:2011-03-26 13:50:14

标签: iphone uiwebview webview asihttprequest

我在viewDidLoad

中有以下代码
NSURL *url = [NSURL URLWithString:URL_TO_DOWNLOAD];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];

[request setDelegate:self];
[request startAsynchronous];
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];

完成此操作后,我想查看在UIWebView内下载的网址(PDF)。我以编程方式创建UIWebView但是我无法找到loadRequest实际ASIHTTP请求的方法。

1 个答案:

答案 0 :(得分:0)

您可以通过双向方式在设备中显示pdf。

首先直接将url传递给UIWebView,在这种情况下你不需要下载pdf文件

UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 360, 420)];

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

第二种方式,您首先需要下载(正如您所做),然后从您的应用程序包中获取pdf文件的路径,

UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];

NSString *pdfpath = [[NSBundle mainBundle] pathForResource:@"mypdf" ofType:@"pdf"];
NSURL *pdfURL = [NSURL fileURLWithPath:pdfpath ];
NSURLRequest *pdfRequest = [NSURLRequest requestWithURL:pdfURL ];
[webView loadRequest:pdfRequest];

[self.view addSubview:webView];
[webView release];