How to encode and decode files as Base64 in Cocoa / Objective-C

I'm currently trying to get a small soap client to work, which involves sending a certificate file in an xml request.

It’s not difficult for me to get the file in an NSData object, but then I need to convert it to some Base64 String. Environment - Mac OSX, Xcode 4.3.

I found a lot of old posts related to this - but the best I found was some code that used the OpenSSL libraries and which contained a lot of obsolete methods.

So my question is this: Is there a better way than using OpenSSL libraries? If so, do you have some kind of url or later code snippets?

If not, I think there is some kind of project that deals with Base64 that you can recommend. After all, Base64 is not so unusual.

Thanks for your help!

+8
objective-c base64 cocoa
Jul 08 2018-12-12T00:
source share
4 answers

Here is the base64 encoding done with CommonCrypto:

this is a very simple code, it's easy to put it in a category

if you add this to your project you also need to add Security.framework

#include <CoreFoundation/CoreFoundation.h> #include <Security/Security.h> static NSData *base64helper(NSData *input, SecTransformRef transform) { NSData *output = nil; if (!transform) return nil; if (SecTransformSetAttribute(transform, kSecTransformInputAttributeName, input, NULL)) output = (NSData *)SecTransformExecute(transform, NULL); CFRelease(transform); return [output autorelease]; } NSString *base64enc(NSData *input) { SecTransformRef transform = SecEncodeTransformCreate(kSecBase64Encoding, NULL); return [[[NSString alloc] initWithData:base64helper(input, transform) encoding:NSASCIIStringEncoding] autorelease]; } NSData *base64dec(NSString *input) { SecTransformRef transform = SecDecodeTransformCreate(kSecBase64Encoding, NULL); return base64helper([input dataUsingEncoding:NSASCIIStringEncoding], transform); } 
+11
Jan 25 '13 at
source share
β€” -

If you are using iOS 7 or OS X 10.9 SDK, you can use the new base64 NSData features.

If you are using the old SDK, just add this declaration to get the encoding and decoding of the base64 NSData database. This will work on iOS 4+ and OS X 10.7+.

 #ifndef __IPHONE_7_0 @interface NSData (NSDeprecated) - (id)initWithBase64Encoding:(NSString *)base64String NS_DEPRECATED(10_6, 10_9, 4_0, 7_0); - (NSString *)base64Encoding NS_DEPRECATED(10_6, 10_9, 4_0, 7_0); @end #endif 
+5
Sep 12 '13 at 18:59 on
source share

Here is a simple NSData Base64 category . I wrote. It uses plist serialization / deserialization mechanism under the hood. In addition, it would be nice to recoup the radar # 9896929 .

+3
Jul 08 2018-12-12T00:
source share

This works for OSX, and it's fine using it with the SDK, starting from 10.6 to 10.8. For 10.9, the methods have changed a bit (although they work at the time of writing), but all this is documented on

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/DeprecationAppendix/AppendixADeprecatedAPI.html#//apple_ref/occ/instm/NSData/base64Encoding

 + (NSString *) base64StringFromFileAtPath: (NSString*) filePath { NSData * dataFromFile = [NSData dataWithContentsOfFile:filePath]; return [dataFromFile base64Encoding]; } + (NSData*) dataFrom64String : (NSString*) stringEncodedWithBase64 { NSData *dataFromBase64 = [[NSData alloc] initWithBase64Encoding:stringEncodedWithBase64]; return dataFromBase64; } 
+2
Apr 23 '14 at 8:51
source share



All Articles