2

使用 AsiHttpRequest 和 UIWebView 查看文档(pdf、doc、xls)的最佳方式是什么???我尝试了以下操作,但 UIWebView 正在显示 html:

NSString * baseURL = @"http://xxxxxx/open-api/v1/";
NSString * itemRef = @"item/133/attachment/test.pdf";

NSString *urlString = [NSString stringWithFormat:@"%@%@", baseURL, itemRef];
NSURL *url = [NSURL URLWithString:urlString];

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];

[request setUsername:@"xx"];
[request setPassword:@"xx"];

[request startSynchronous];

NSError *error = [request error];
if (!error) {
    [self.webView loadHTMLString:[request responseString] baseURL: [request url]];
}
else {
    NSLog(@"Error: %@", error );
}

AsiHttpRequest 是设置基本身份验证和标头值所必需的...谢谢!

4

1 回答 1

1

好吧,公平地说,您是在告诉它显示 HTML,所以结果并不出乎意料 :-)

您需要在本地下载 pdf 文件:

NSString *tmpLocation = // some temporary file location
[request setDownloadDestinationPath:tmpLocation]];
[request startSyncronous];

然后在 UIWebView 中查看:

NSURL *url = [NSURL fileURLWithPath:tmpLocation];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
于 2010-10-08T13:22:44.373 回答