使用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 ...谢谢!
答案 0 :(得分: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];