如何在UIWebview iOS上以二进制格式加载pdf?

时间:2014-08-18 13:49:25

标签: objective-c pdf ios7 uiwebview

我想在用户点击下载按钮时打开一个pdf文件,但是加载的数据是解码的,我如何将响应数据转换为pdf文件?我没有从本地文档或包中加载它。

NSMutableURLRequest *requestObj  = [NSMutableURLRequest requestWithURL:url];
[webViewForDocsView loadRequest:requestObj];
[self.view addSubview:webViewForDocsView];

1 个答案:

答案 0 :(得分:0)

要加载原始数据,首先要创建一个带有url响应内容的NSData对象,然后通过指定mime类型和数据加载数据,如代码片段:

NSURL *url = [[NSURL alloc] initWithString:@"binary file link"];
NSError *error;

/**
 *  GET the data from a url link
 */
NSData *data = [NSData dataWithContentsOfURL:url
                                     options:NSDataReadingUncached
                                       error:&error];

if (error) { // validation
    NSLog(@"data error: %@", error);

}

UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.frame];
[self.view addSubview:webView];

/**
 *  Load the request fro mthe binaray data
 */
[webView loadData:data
         MIMEType:@"application/pdf"
 textEncodingName:@"utf-8"
          baseURL:[NSURL URLWithString:@"http://example.com/"]];