ASIHTTPRequest UIWebView load documents

假如想象 提交于 2019-12-08 02:04:27

问题


What is the best way to view documents (pdf,doc,xls) using AsiHttpRequest and UIWebView??? I tried the following, but the UIWebView is displaying the 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 is required for setting Basic Authentication and Header Values... Thanks!


回答1:


Well, to be fair, you're telling it to display HTML, so the result isn't really unexpected :-)

You'd need to download the pdf file locally:

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

then view it in UIWebView:

NSURL *url = [NSURL fileURLWithPath:tmpLocation];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];


来源:https://stackoverflow.com/questions/3890528/asihttprequest-uiwebview-load-documents

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!