Converting between NSData and base64 strings

70,918

Solution 1

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.

Solution 2

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.)

Solution 3

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];

Solution 4

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]];

Solution 5

Be aware that there are more Base64 formats.

For example JWTs use a URL safe format.

Share:
70,918

Related videos on Youtube

aherlambang
Author by

aherlambang

iOS and web developer passionate on building apps

Updated on February 14, 2020

Comments

  • aherlambang
    aherlambang over 4 years

    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.

    • Art Gillespie
      Art Gillespie about 13 years
      All reasonable solutions are going to look something like the Matt Gallagher post you linked to.
    • bobobobo
      bobobobo about 11 years
      Here's to finding Gallagher's library where people have mysteriously used [NSData dataFromBase64String] without linking
    • Hot Licks
      Hot Licks over 10 years
      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.
  • aherlambang
    aherlambang about 13 years
    I have downloaded the two, so do I need to add that project to my or can I just simply drag the NSData + Base64?
  • Ryan Wersal
    Ryan Wersal about 13 years
    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
    Ryan Wersal about 13 years
    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.
  • aherlambang
    aherlambang about 13 years
    how can I do that Ryan? mind giving me some pointers, I think that's the easiest way to do it
  • Ryan Wersal
    Ryan Wersal about 13 years
    I would think the code sample in my answer would be sufficient... Can you be more specific with what pointers you need?
  • aherlambang
    aherlambang about 13 years
    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
    aherlambang about 13 years
    one question the file has a base64.m, do I need that as well? because if I do it has a include <openssl/bio.h> and #import <Security/cssm.h> 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
  • Ryan Wersal
    Ryan Wersal about 13 years
    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?
  • jonstaff
    jonstaff over 9 years
    This was added in iOS 7/OSX 10.9 and it is hands down the best solution offered here. Documentation here.
  • Martin
    Martin over 6 years
    All I get ist 'NSData+Base64.h' file not found. 😞
  • SeeCoolGuy
    SeeCoolGuy over 6 years
    @Martin I used the classes generated by the sudzc lib, it creates this class for you that you can then import in your implementation file (.m)