Converting between NSData and base64 strings
Asked Answered
A

6

44

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.

Astrobiology answered 14/5, 2011 at 2:3 Comment(3)
All reasonable solutions are going to look something like the Matt Gallagher post you linked to.Gynecium
Here's to finding Gallagher's library where people have mysteriously used [NSData dataFromBase64String] without linkingYokoyokohama
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.Mariel
N
60

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.

Neill answered 14/5, 2011 at 3:33 Comment(8)
I have downloaded the two, so do I need to add that project to my or can I just simply drag the NSData + Base64?Astrobiology
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.Neill
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.Neill
how can I do that Ryan? mind giving me some pointers, I think that's the easiest way to do itAstrobiology
I would think the code sample in my answer would be sufficient... Can you be more specific with what pointers you need?Neill
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 errorAstrobiology
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 haveAstrobiology
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?Neill
P
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.)

Perished answered 26/9, 2013 at 16:26 Comment(0)
T
44

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];
Trainee answered 15/10, 2014 at 13:2 Comment(1)
This was added in iOS 7/OSX 10.9 and it is hands down the best solution offered here. Documentation here.Turnip
A
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]];
Ainsley answered 18/5, 2012 at 19:0 Comment(2)
All I get ist 'NSData+Base64.h' file not found. 😞Outmoded
@Outmoded 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)Ainsley
W
1

Be aware that there are more Base64 formats.

For example JWTs use a URL safe format.

Woolpack answered 13/2, 2019 at 20:46 Comment(0)
D
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).

Desouza answered 26/9, 2013 at 15:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.