NSString+Base64.h代码
//
// NSString+Base64.h
// 121mai
//
// Created by 薛XX on 16/2/16.
//
//
#import <Foundation/Foundation.h>
@interface NSString (Base64)
+ (NSString *)stringWithBase64EncodedString:(NSString *)string;
- (NSString *)base64EncodedStringWithWrapWidth:(NSUInteger)wrapWidth;
- (NSString *)base64EncodedString;
- (NSString *)base64DecodedString;
- (NSData *)base64DecodedData;
@end
<pre name="code" class="objc"><pre name="code" class="objc">//
// NSString+Base64.m
// 121mai
//
// Created by 薛XX on 16/2/16.
//
//
#import "NSString+Base64.h"
#import "NSData+Base64.h"
@implementation NSString (Base64)
+ (NSString *)stringWithBase64EncodedString:(NSString *)string
{
NSData *data = [NSData dataWithBase64EncodedString:string];
if (data)
{
NSString *result = [[self alloc] initWithData:data encoding:NSUTF8StringEncoding];
#if !__has_feature(objc_arc)
[result autorelease];
#endif
return result;
}
return nil;
}
- (NSString *)base64EncodedStringWithWrapWidth:(NSUInteger)wrapWidth
{
NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
return [data base64EncodedStringWithWrapWidth:wrapWidth];
}
- (NSString *)base64EncodedString
{
NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
return [data base64EncodedString];
}
- (NSString *)base64DecodedString
{
return [NSString stringWithBase64EncodedString:self];
}
- (NSData *)base64DecodedData
{
return [NSData dataWithBase64EncodedString:self];
}
@end