IOS 7:base64 Encode and Decode NSData and NSString Objects

Source: Internet
Author: User
Tags base64 encode


IOS 7:base64 Encode and Decode NSData and NSString Objectsfri, JAN CORE servicestweet


With the release of IOS 7, Apple added support for encoding and decoding data using BASE64. In this post we'll walk through the examples using Base64 to encode and decode both NSData and NSString objects.



First, we'll create an NSString object, a generated by BASE64 encoding an NSData object. This is being followed by decoding the Base64 NSString back to an NSData object. We'll display the NSString data, both encoded and decoded to make sure all are well.



The second example would encode and decode NSData To/from Base64. This example was relevant if you had an NSData object that needs to being BASE64 encoded, or you need to decode a Base64 NSDA Ta object (for whatever reason).




Create a Base64 encoded nsstring Object


Let's begin by encoding an NSData object to Base64 and returning an NSString object:


// Create NSData object
NSData *nsdata = [@"iOS Developer Tips encoded in Base64"
  dataUsingEncoding:NSUTF8StringEncoding];
 
// Get NSString from NSData object in Base64
NSString *base64Encoded = [nsdata base64EncodedStringWithOptions:0];
 
// Print the Base64 encoded string
NSLog(@"Encoded: %@", base64Encoded);
 
// Let‘s go the other way...
 
// NSData from the Base64 encoded str
NSData *nsdataFromBase64String = [[NSData alloc]
  initWithBase64EncodedString:base64Encoded options:0];
 
// Decoded NSString from the NSData
NSString *base64Decoded = [[NSString alloc] 
  initWithData:nsdataFromBase64String encoding:NSUTF8StringEncoding];
NSLog(@"Decoded: %@", base64Decoded);


 
 


On line 2 We create the NSData to encode. Line 6 encodes the data and returns an NSString object.



To go back the other by, from a BASE64 encoded NSString object to an NSData object was as easy as calling the method I Nitwithbase64encodedstring, passing in the BASE64 encoded nsstring (lines 14-15).



Lines 18-20 go from the Base64 NSData object back to an nsstring (which are how we started this example, from a string).



The output looks as follows:


Encoded: aU9TIERldmVsb3BlciBUaXBzIGVuY29kZWQgaW4gQmFzZTY0
Decoded: iOS Developer Tips encoded in Base64


There ' s a good chance the NSString conversions (above) May is more about debugging than a day-to-day need to Base64 encode D strings. With so in mind, let's look at how to directly encode a NSData object to Base64 as well as what to decode an nsdata Obje CT from Base64.


// Create NSData object
NSData *dataTake2 = 
  [@"iOS Developer Tips" dataUsingEncoding:NSUTF8StringEncoding];
 
// Convert to Base64 data
NSData *base64Data = [dataTake2 base64EncodedDataWithOptions:0];
NSLog(@"%@", [NSString stringWithUTF8String:[base64Data bytes]]);
 
// Do something with the data
// ...
 
// Now convert back from Base64
NSData *nsdataDecoded = [base64Data initWithBase64EncodedData:base64Data options:0];
NSString *str = [[NSString alloc] initWithData:nsdataDecoded encoding:NSUTF8StringEncoding];
NSLog(@"%@", str);


 
 


On line 2 We create our NSData test subject. Line 6 We are the base64encodeddatawithoptions method of the NSData class to Base64 encode the data. I ' ve included an nsstring conversion of the data solely to verify what goes in are the same as what comes back out.



Decoding the Base64 NSData object is nothing more than calling the method Initwithbase64encodeddata with the Enco Ded NSData Object (line 13). Once again, I ' ve included a nsstring conversion for testing.


Additional Reading
    • The NSData class reference includes the information on what to encode and decode NSData using BASE64.
    • The NSString class reference explains the nuances of encoding and decoding when working with NSString objects.
    • I ' ve found that encoding and decoding data from the terminal can is helpful when debugging. This MAC OS X doc describes the base64 OS x Terminal Utility.


IOS 7:base64 Encode and Decode NSData and NSString Objects


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.