Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C Load PDF file in UIWebView

EDIT: There was something wrong with my Base64 decoding. I searched for a external Base64 decoder and it working just like this:

This is the case: I have a Base64 encoded byte array I get from a webservice and convert it to NSData:

NSData *data = [Base64 decodeBase64WithString:response];

And in my Webview Controller I declared:

[webview loadData:fileData MIMEType:@"application/pdf" textEncodingName:@"utf-8" baseURL:nil];

fileData is the decoded data.

When I run this I get a gray screen. So I assume I'm not giving it a correct NSData object.

like image 303
Jordy Langen Avatar asked Oct 31 '11 11:10

Jordy Langen


3 Answers

I already answered my own question when I was typing it.

So I assume I'm not giving it a correct NSData object.

My Base64 decoding was wrong.

Using this statement works like a charm:

[webview loadData:fileData MIMEType:@"application/pdf" textEncodingName:@"utf-8" baseURL:nil];

I'm just posting so other people can look at it of they have the same problem.

like image 51
Jordy Langen Avatar answered Sep 22 '22 03:09

Jordy Langen


[webView loadata:data MIMEType:@"application/pdf" textEncodingName:@"UTF-8" baseURL:nil];

That should do the trick for you, if it doesn't you can write it to a file as V1ru8 suggests, but that is an extra step in most of the cases.

Hope this will helps

like image 34
mdominick Avatar answered Sep 25 '22 03:09

mdominick


In addition to the already provided answers, I've found that loading NSData* into a UIWebView in the initializer function of a containing UIViewController doesn't work and there'll be no error.

The NSData*needs to be loaded into the UIWebView in the viewDidLoad function.

like image 43
Pin Avatar answered Sep 24 '22 03:09

Pin