Skip to content

Instantly share code, notes, and snippets.

@ArtSabintsev
Created September 11, 2013 06:19
Show Gist options
  • Save ArtSabintsev/6519920 to your computer and use it in GitHub Desktop.
Save ArtSabintsev/6519920 to your computer and use it in GitHub Desktop.
A category on NSString that exploits a set of CoreFoundation methods to create Base64 encoded strings. This is a simple solution that removes the need for external Base64 libraries in projects that targets apps that support iOS 2 - iOS 6. iOS 7 has a more concrete solution, but due to NDA, I cannot post it yet.
+ (NSString *)base64EncodeString:(NSString *)stringToEncode
{
CFHTTPMessageRef messageRef = CFHTTPMessageCreateEmpty(kCFAllocatorDefault, FALSE);
CFHTTPMessageAddAuthentication(messageRef, NULL, CFSTR("AS"), (__bridge CFStringRef)stringToEncode, kCFHTTPAuthenticationSchemeBasic, FALSE);
CFStringRef authStringRef = CFHTTPMessageCopyHeaderFieldValue(messageRef, CFSTR("Authorization"));
NSString *encodedString = [(__bridge NSString *)authStringRef substringFromIndex:10];
CFRelease(messageRef);
CFRelease(authStringRef);
return encodedString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment