Base64 Decoding in iOS 7+
Asked Answered
C

3

187

I have Encoded text(NSString) using NSData Class new API which is Added in iOS7.

using this

- (NSData *)dataUsingEncoding:(NSStringEncoding)encoding;  

here is my code

NSString *base64EncodedString = [[myText dataUsingEncoding:NSUTF8StringEncoding] base64EncodedStringWithOptions:0];

NSLog(@"%@", base64EncodedString);

I am looking to decode it

Camelopardalis answered 30/9, 2013 at 7:1 Comment(4)
m_EncodeText: please stopAircraft
where have you found base64EncodedStringWithOptions:?Siracusa
@Siracusa it's there in apple docs developer.apple.com/library/ios/documentation/cocoa/reference/…Camelopardalis
It seems It happens when I use mac os console application target. Or the old version of sdk/mac os targetSiracusa
K
446

Swift 3+

let plainString = "foo"

Encoding

let plainData = plainString.data(using: .utf8)
let base64String = plainData?.base64EncodedString()
print(base64String!) // Zm9v

Decoding

if let decodedData = Data(base64Encoded: base64String!),
   let decodedString = String(data: decodedData, encoding: .utf8) {
  print(decodedString) // foo
}

Swift < 3

let plainString = "foo"

Encoding

let plainData = plainString.dataUsingEncoding(NSUTF8StringEncoding)
let base64String = plainData?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
print(base64String!) // Zm9v

Decoding

let decodedData = NSData(base64EncodedString: base64String!, options: NSDataBase64DecodingOptions(rawValue: 0))
let decodedString = NSString(data: decodedData, encoding: NSUTF8StringEncoding)
print(decodedString) // foo

Objective-C

NSString *plainString = @"foo";

Encoding

NSData *plainData = [plainString dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64String = [plainData base64EncodedStringWithOptions:0];
NSLog(@"%@", base64String); // Zm9v

Decoding

NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding];
NSLog(@"%@", decodedString); // foo 

Kanal answered 30/9, 2013 at 7:9 Comment(11)
I don't think this code is clean. I get the warning : "incompatible pointer types initializing NNSTring with an expression of NSData"Rink
@Rink you must have typed base64EncodedDataWithOptions: instead of base64EncodedStringWithOptions:Commotion
Thanks for this. Something I just found: a constant kNilOptions is equal to 0 which is arguably more readable than using 0 for options.Organogenesis
@AdamWaite kNilOptions is defined in MacTypes.h which is a legacy OSX header, so I don't like using it, since I think that header should be removed from the iOS SDK altogether. Anyway, you have a point in saying that is very readable, so feel free to use it.Kanal
it seems the method base64EncodedStringWithOptions is not url safe base64Gesticulate
base64 decoding works only if the string length is a multiple of 4. otherwise NSData returns nil.Marquee
this code works well But in my case i get xml response from server which are already in base64 encoded .I have decode that data but when i use this method it returns null. can anybody suggest me tackle this problem ???Gudgeon
Decoding may not work if there's a new line following the encoded data. See, for example, Decode base64 data encoded in bash.Approbation
if anyone getting a nil try NSDataBase64DecodingOptions.IgnoreUnknownCharacters i had this issueFoetid
I was decoding a JWT token, and learned there were a few more fancy steps to decode it. I used the method base64decode from github.com/Wstunes/SwiftyJWT after parsing the JWT token at the periods.Cl
While decoding the base64String I was getting nil for using NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:base64String options:0]; So, I used NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:base64String options:NSDataBase64DecodingIgnoreUnknownCharacters]; And it worked for me.Triangulate
F
82

In case you want to write fallback code, decoding from base64 has been present in iOS since the very beginning by caveat of NSURL:

NSURL *URL = [NSURL URLWithString:
      [NSString stringWithFormat:@"data:application/octet-stream;base64,%@",
           base64String]];

return [NSData dataWithContentsOfURL:URL];
Flowery answered 16/3, 2014 at 4:3 Comment(3)
I'm totally impressed with this answer. For years i've been using some ridiculous amount of code to decode a string, and all along there was a 2 line solution.Liberty
This is awesome! Hacky, but awesome :)Denary
I've never thought is is so easy! Thanks!!Suffer
T
1

While decoding the base64String I was getting nil for using:

NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:base64String options:0];

So, I used:

NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:base64String options:NSDataBase64DecodingIgnoreUnknownCharacters];

And it worked for me.

Triangulate answered 3/11, 2023 at 14:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.