Created
February 24, 2012 15:03
-
-
Save 0xced/1901480 to your computer and use it in GitHub Desktop.
NSData Base64 category
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created by Cédric Luthi on 2012-02-24. | |
// Copyright (c) 2012 Cédric Luthi. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
@interface NSData (Base64) | |
+ (id) dataWithBase64Encoding_xcd:(NSString *)base64String; | |
- (NSString *) base64Encoding_xcd; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created by Cédric Luthi on 2012-02-24. | |
// Copyright (c) 2012 Cédric Luthi. All rights reserved. | |
// | |
#import "NSData+Base64.h" | |
#ifndef __has_feature | |
#define __has_feature(x) 0 | |
#endif | |
@implementation NSData (Base64) | |
+ (id) dataWithBase64Encoding_xcd:(NSString *)base64Encoding | |
{ | |
if ([base64Encoding length] % 4 != 0) | |
return nil; | |
NSString *plist = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?><plist version=\"1.0\"><data>%@</data></plist>", base64Encoding]; | |
return [NSPropertyListSerialization propertyListWithData:[plist dataUsingEncoding:NSASCIIStringEncoding] options:0 format:NULL error:NULL]; | |
} | |
- (NSString *) base64Encoding_xcd | |
{ | |
NSData *plist = [NSPropertyListSerialization dataWithPropertyList:self format:NSPropertyListXMLFormat_v1_0 options:0 error:NULL]; | |
NSRange fullRange = NSMakeRange(0, [plist length]); | |
NSRange startRange = [plist rangeOfData:[@"<data>" dataUsingEncoding:NSASCIIStringEncoding] options:0 range:fullRange]; | |
NSRange endRange = [plist rangeOfData:[@"</data>" dataUsingEncoding:NSASCIIStringEncoding] options:NSDataSearchBackwards range:fullRange]; | |
if (startRange.location == NSNotFound || endRange.location == NSNotFound) | |
return nil; | |
NSUInteger base64Location = startRange.location + startRange.length; | |
NSUInteger base64length = endRange.location - base64Location; | |
NSData *base64Data = [NSData dataWithBytesNoCopy:(void *)((uintptr_t)base64Location + (uintptr_t)[plist bytes]) length:base64length freeWhenDone:NO]; | |
NSString *base64Encoding = [[NSString alloc] initWithData:base64Data encoding:NSASCIIStringEncoding]; | |
base64Encoding = [base64Encoding stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; | |
base64Encoding = [base64Encoding stringByReplacingOccurrencesOfString:@"\n" withString:@""]; | |
#if __has_feature(objc_arc) | |
return base64Encoding; | |
#else | |
return [base64Encoding autorelease]; | |
#endif | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Adding a mention of http://www.openradar.me/9896929 so I remember to dupe it when I get a moment.