Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to decode base64 string to NSString in ios?

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!!!

like image 365
Pratik Patel Avatar asked Sep 16 '13 12:09

Pratik Patel


2 Answers

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);  
like image 121
codercat Avatar answered Nov 07 '22 05:11

codercat


Try to use like this..First you need to get this base64 library

https://github.com/l4u/NSData-Base64

And then import this file

   #import "NSData+Base64.h"

NSData *data = [NSData dataFromBase64String:baseString];

NSString *convertedString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

I hope it helps.

like image 44
Dharmbir Singh Avatar answered Nov 07 '22 04:11

Dharmbir Singh