3

In JSON file, I am receiving the base64 encoded string. It may contain image file, pdf file, doc or text file or audio/video file.

How can I show/play audio/video in iOS from base64 encoded string?

I implemented code for images, doc, pdf, text files except for audio/video file. I used below code to achieve this,

//for image

NSURL *url = [NSURL URLWithString:base64String];    
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage *ret = [UIImage imageWithData:imageData];

//for ppt, pdf, doc, and textfile

    NSData* myData = [NSData dataFromBase64String: base64EncodedString];
    [_webView loadData:myData
                 MIMEType:@"application/pdf"
         textEncodingName:@"NSUTF8StringEncoding"
                  baseURL:[NSURL URLWithString:@"https://www.google.co.in/"]];
Nikita Patil
  • 674
  • 1
  • 7
  • 17
  • Check this link may be it's resolve your issue. https://stackoverflow.com/a/22274763/1947682 – Anil Jangir Apr 02 '18 at 11:39
  • in JSON already getting file type, for me detecting a base64 string not required. – Nikita Patil Apr 02 '18 at 11:42
  • did you check these urls 1. https://stackoverflow.com/questions/43400041/swift-3-loading-a-previously-saved-audio-file-as-base64-string?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa 2. https://stackoverflow.com/questions/43400041/swift-3-loading-a-previously-saved-audio-file-as-base64-string/43400334?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Gagan_iOS Apr 02 '18 at 12:38

1 Answers1

3
//try this simple logic as per your code

NSURL *URL = [NSURL URLWithString:
                              [NSString stringWithFormat:@"data:audio/mp3;base64,%@",
                               yourBase64EncodedString]];


 [_webView loadRequest:[NSURLRequest requestWithURL:URL]];