How to show pdf on any view from base64 encoded string in IOS?

I am getting base64 encoded data in JSON file.

What I want is that I want to show that that pdf file in view or web view.

I tried many answers from StackOverflow but that is not working for me.




I tried below code, it is now working.


NSString * base64EncodedString = @"VBERi0xLjMNJeLjz9MNCj...........";

NSData *data = [[NSData alloc]initWithBase64EncodedString:base64EncodedString options:NSDataBase64DecodingIgnoreUnknownCharacters];

[webView loadData:data MIMEType:@"application/pdf" textEncodingName:@"" baseURL:[NSURL URLWithString:@""]];


Even I tried the code, it is also not working



NSString * base64EncodedString = @"VBERi0xLjMNJeLjz9MNCj...........";

NSData *nsdataFromBase64String = [[NSData alloc] base64EncodedString options:0];

[webView loadData:data MIMEType:@"application/pdf" textEncodingName:@"" baseURL:[NSURL URLWithString:@""]];

First things first I recommend that you check that the PDF data is correct. You could do this as follows:

  1. Once you’ve decoded the data, write it to a temporary

    .pdf
    file and log the path to that file.
  2. Run your app on the simulator.

  3. Find that path on your Mac and open the PDF in Preview.

If it displays correctly then you know that the problem is with the web view side of things rather than with decoding the PDF.

Also, how big is this PDF? When you run the test above, what’s the resulting file size?

Finally, on the web view front, what type of web view are you using? In general I recommend that folks adopt

WKWebView
rather than the legacy
UIWebView
.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
How to show pdf on any view from base64 encoded string in IOS?
 
 
Q