Iphone ASIHTTPRequest UIWebView加载文档

Iphone ASIHTTPRequest UIWebView加载文档,iphone,uiwebview,asihttprequest,Iphone,Uiwebview,Asihttprequest,使用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:@"%@%@", ba

使用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。。。谢谢

公平地说,您告诉它显示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];

你用过吗?如果我使用NSTemporaryDirectory()作为临时位置,它将不起作用……您需要在NSTemporaryDirectory()的末尾添加一个文件名。(不,我没有直接尝试这段代码,但是这是正确的方法,没有理由不起作用。)