1. 程式人生 > >[iOS] how to decode base64 string to NSString?

[iOS] how to decode base64 string to NSString?

NSString *decodeString = @"Raja";

Encode String

NSData *encodeData = [decodeString dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64String = [encodeData base64EncodedStringWithOptions:0];
NSLog(@"Encode String Value: %@", base64String);

Decode String

NSData *decodedData = [[NSData alloc] initWithBase64EncodedString
:base64String options:0]; NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding]; NSLog(@"Decode String Value: %@", decodedString);

As of iOS 7, NSData now directly provides this functionality with the new methods -base64EncodedDataWithOptions: and -base64EncodedStringWithOptions:

. (The options let you specify that the string is/should be line-wrapped, the better to deal with email, and user-facing displays.)