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

Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
Sandeep Khade
  • 2,832
  • 3
  • 21
  • 37

2 Answers2

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 

Cœur
  • 37,241
  • 25
  • 195
  • 267
Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
  • 1
    I don't think this code is clean. I get the warning : "incompatible pointer types initializing NNSTring with an expression of NSData" – drlobo Nov 26 '13 at 10:27
  • 10
    @drlobo you must have typed base64EncodedDataWithOptions: instead of base64EncodedStringWithOptions: – Bach Nov 29 '13 at 01:58
  • 4
    Thanks for this. Something I just found: a constant `kNilOptions` is equal to 0 which is arguably more readable than using 0 for options. – Adam Waite Mar 16 '14 at 22:28
  • 2
    @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. – Gabriele Petronella Mar 16 '14 at 22:52
  • 1
    it seems the method base64EncodedStringWithOptions is not url safe base64 – keisar Jun 09 '14 at 08:00
  • 2
    base64 decoding works only if the string length is a multiple of 4. otherwise NSData returns nil. – shshnk Dec 10 '14 at 15:49
  • 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 ??? – amit gupta Feb 20 '15 at 07:15
  • 1
    Decoding may not work if there's a new line following the encoded data. See, for example, [Decode base64 data encoded in bash](http://stackoverflow.com/q/31336064). – jww Jul 11 '15 at 01:14
  • 2
    if anyone getting a nil try `NSDataBase64DecodingOptions.IgnoreUnknownCharacters` i had this issue – spaceMonkey Jul 27 '16 at 05:03
  • I was decoding a JWT token, and learned there were a few more fancy steps to decode it. I used the method `base64decode` from https://github.com/Wstunes/SwiftyJWT after parsing the JWT token at the periods. – Chris Prince May 08 '18 at 20:29
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];
Tommy
  • 99,986
  • 12
  • 185
  • 204