Zeshan Arif Zeshan Arif - 6 days ago 5
iOS Question

How to convert base64 into NSDATA in swift

I am working on an iOS project. It stores audio on web server in the form of base64 string. When I request server to get base64 Strings for all audios and tried convert it in

NSData
I am getting
nil
.

do
{
var audioData: NSData! = NSData(base64EncodedString: audioBase64String, options: NSDataBase64DecodingOptions(rawValue:0))
if audioData != nil
{
let sound = try AVAudioPlayer(data: audioData)
sound.play()
}
else
{
print("Data Not Exist")
}
}
catch
{
}


On Android same base64 string is converted into byte array and is playing, but in iOS
audioBase64String
return
nil
for
NSData
.

Answer

This works:

audioData = NSData(base64EncodedString: recording_base64, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)