how to decode base64 string to NSString in ios?

Pratik Patel picture Pratik Patel · Sep 16, 2013 · Viewed 27.1k times · Source

I have received some data in base64string format from JSON pasring in iphone. Now i want that data to store or decode in String. I have user Base64 class. It return me null value. so pls help me!!!

Answer

codercat picture codercat · Nov 29, 2013

I hope it will helpful to you

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);