Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert string to base64 encoding in iOS 6.1

I am converting string to base64 encoded in iOS 7 and its working fine,but when application is running in iOS 6.1 application get crashed.for iOS 7, I am using :

NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:pdfDataString options:0];

in iOS 6 my application get crashed on this line.Please help me.How can i convert string to Base64 encoded in iOS 6.

like image 268
Nancy Avatar asked Oct 08 '14 10:10

Nancy


People also ask

How do you convert a string to Base64?

To convert a string into a Base64 character the following steps should be followed: Get the ASCII value of each character in the string. Compute the 8-bit binary equivalent of the ASCII values. Convert the 8-bit characters chunk into chunks of 6 bits by re-grouping the digits.

Is BTOA same as Base64?

The btoa() method creates a Base64-encoded ASCII string from a binary string (i.e., a string in which each character in the string is treated as a byte of binary data).


1 Answers

You can use this method... or you can use it like reference :)

- (NSString*)encodeStringTo64:(NSString*)fromString
{  
    NSData *plainData = [fromString dataUsingEncoding:NSUTF8StringEncoding];
    NSString *base64String;
    if ([plainData respondsToSelector:@selector(base64EncodedStringWithOptions:)]) {
        base64String = [plainData base64EncodedStringWithOptions:kNilOptions];  // iOS 7+
    } else {
        base64String = [plainData base64Encoding];                              // pre iOS7
    }

    return base64String;
}
like image 128
TonyMkenu Avatar answered Oct 30 '22 04:10

TonyMkenu