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

后端 未结 4 1940
庸人自扰
庸人自扰 2020-12-30 11:26

I am currently trying to get a small soap client to work, which includes to send a certificate file within the xml of the request.

I have no trouble getting the file

相关标签:
4条回答
  • 2020-12-30 12:12

    This works for OSX and it's ok using this with SDK's starting from 10.6 to 10.8. For 10.9 the methods have changed a bit (although at the time of writing, they work), but it's all 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;
    }
    
    0 讨论(0)
  • 2020-12-30 12:22

    Here is a simple NSData Base64 category I wrote. It uses the plist serialization/deserialization mechanism under the hood. Also, duping radar #9896929 would be nice.

    0 讨论(0)
  • 2020-12-30 12:24

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

    If you are using an older SDK, just add this declaration to get NSData base64 encoding and decoding. 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
    
    0 讨论(0)
  • 2020-12-30 12:33

    Here is a base64 encoding done with CommonCrypto:

    it is very easy code, it would not be difficult to put it in a category

    if you add this to your project you need also to add the 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);
    }
    
    0 讨论(0)
提交回复
热议问题