43

What is the easiest and fastest code to do a conversion between NSData and a base64 string? I've read a bunch of solutions at SO and mostly they involve in adding another class etc. I found a great solution here but it's too complex.

jscs
  • 63,694
  • 13
  • 151
  • 195
aherlambang
  • 14,290
  • 50
  • 150
  • 253
  • 6
    All reasonable solutions are going to look something like the Matt Gallagher post you linked to. – Art Gillespie May 14 '11 at 02:08
  • Here's to finding Gallagher's library where people have mysteriously used `[NSData dataFromBase64String]` without linking – bobobobo Apr 19 '13 at 00:54
  • For some incomprehensible reason Apple has never provided "native" support for Base64, but all of the 3rd party kits are pretty much identical. Just pick one. – Hot Licks Sep 26 '13 at 15:20

6 Answers6

59

Scroll down to the Conclusion section on the page you linked and download the provided NSData+Base64 files. Its the best solution I have seen so far and is incredibly easy to use. If you can learn anything about Cocoa, you can learn to use that project.


Example

NSString *originalString = [NSString stringWithFormat:@"test"]; 
NSData *data = [NSData dataFromBase64String:originalString];  
NSLog([data base64EncodedString]); 

The above will print out the original string after converting it to base64 and back to a normal unencoded string.

Ryan Wersal
  • 3,210
  • 1
  • 20
  • 29
  • 2
    I have downloaded the two, so do I need to add that project to my or can I just simply drag the NSData + Base64? – aherlambang May 14 '11 at 12:55
  • 2
    You want to add the two files into your project. Generally when you see names like `NSData+Base64`, the first thing that should run through your head is that this is a category on the `NSData` class. In other words, you only call these new methods using `NSData`, not some new `Base64` class. – Ryan Wersal May 14 '11 at 14:30
  • 1
    I believe, however, that you still need to import the category. Personally, I would recommend putting it into your precompiled header so you can use the methods on `NSData` from anywhere in your project. – Ryan Wersal May 14 '11 at 14:31
  • 1
    how can I do that Ryan? mind giving me some pointers, I think that's the easiest way to do it – aherlambang May 14 '11 at 17:12
  • 1
    I would think the code sample in my answer would be sufficient... Can you be more specific with what pointers you need? – Ryan Wersal May 14 '11 at 19:46
  • I downloaded that whole package you linked above, opened the project and drag the two files NSData+Base64.h and .m and then it gives me an error – aherlambang May 15 '11 at 00:17
  • one question the file has a base64.m, do I need that as well? because if I do it has a include and #import which gives me an error as it doesn't know how to find it. ALso the project has an external framework of lubcrypto.dylib, which I don't have – aherlambang May 15 '11 at 03:20
  • You should only have to import the header file to get it to work. In terms of a framework dependency, I am unaware of one with this particular project. Are you sure you're importing the header to be visible where you need it to be? – Ryan Wersal May 15 '11 at 03:23
45

As of iOS 7, NSData now directly provides this functionality with the new methods -base64EncodedDataWithOptions: and -base64EncodedStringWithOptions:. (The options let you specify that the string is/should be line-wrapped, the better to deal with email, and user-facing displays.)

Sixten Otto
  • 14,816
  • 3
  • 48
  • 60
43

You don't need any custom implementation. Creating base64 from NSData is shown in other answers. There is opposite direction. From Base64 string to NSData:

 NSString *base64Encoded = @"some base64 string";
 NSData *nsdataFromBase64String = [[NSData alloc] initWithBase64EncodedString:base64Encoded options:0];
kraag22
  • 3,340
  • 3
  • 29
  • 37
  • 7
    This was added in iOS 7/OSX 10.9 and it is hands down the best solution offered here. [Documentation here](https://developer.apple.com/library/ios/documentation/cocoa/Reference/Foundation/Classes/NSData_Class/index.html#//apple_ref/occ/instm/NSData/initWithBase64EncodedString:options:). – jonstaff Oct 23 '14 at 07:13
1

I ended up using this same class as provided by SUDZC

implementation was easy first I did an import

 #import "NSData+Base64.h"

then I was able to call my data.

 NSData *data = [[NSData alloc] initWithData:[NSData dataWithBase64EncodedString:strData]];
SeeCoolGuy
  • 81
  • 9
  • All I get ist 'NSData+Base64.h' file not found. – Martin Sep 26 '17 at 14:55
  • 1
    @Martin I used the classes generated by the [sudzc](http://www.sudzc.com) lib, it creates this class for you that you can then import in your implementation file (.m) – SeeCoolGuy Sep 29 '17 at 16:49
1

Be aware that there are more Base64 formats.

For example JWTs use a URL safe format.

meaning-matters
  • 21,929
  • 10
  • 82
  • 142
0

Or you may take a look to the (quite new) CryptoCompatibility sample project, I think there is a wrapper class for base64 operation. It is a MacOS sample but it uses the library libresolve.dylib with I think is available on iOS too (is see it at least here in iOS7).

Stephan
  • 4,263
  • 2
  • 24
  • 33