iOS 7: Base64 Encode and Decode NSData and NSString Objects

简介: iOS 7: Base64 Encode and Decode NSData and NSString ObjectsFRI, JAN 24 CORE SERVICESTWEETWith the release of iOS 7, Apple added support for encoding and decoding data using Base64.

iOS 7: Base64 Encode and Decode NSData and NSString Objects

FRI, JAN 24 

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

First, we will create an NSString object that is generated by Base64 encoding an NSData object. This will be followed by decoding the Base64 NSString back into an NSData object. We will display the NSString data, both encoded and decoded to make sure all is well.

The second example will encode and decode NSData to/from Base64. This example is relevant if you have an NSData object that needs to be Base64 encoded, or you need to decode a Base64 NSData object (for whatever reason).

Create a Base64 Encoded NSString Object

Let’s begin by encoding an NSData object into Base64 and returning an NSString object:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// 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 way, from a Base64 encoded NSString object to an NSData object is as easy as calling the method initWithBase64EncodedString, passing in the Base64 encoded NSString (lines 14-15).

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

The output looks as follows:

Encoded: aU9TIERldmVsb3BlciBUaXBzIGVuY29kZWQgaW4gQmFzZTY0
Decoded: iOS Developer Tips encoded in Base64

Base64 Encode an NSData Object

There’s a good chance the NSString conversions (above) may be more about debugging than a day-to-day need to Base64 encoded strings. With that in mind, let’s look at how to directly encode an NSData object to Base64 as well as how to decode an NSData object from Base64.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 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 call 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 is the same as what comes back out.

Decoding the Base64 NSData object is nothing more than calling the method initWithBase64EncodedData with the encoded NSData object (line 13). Once again, I’ve included an NSString conversion for testing.

Additional Reading

  • The NSData class reference includes the information on how 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 be helpful when debugging. This Mac OS X doc describes the base64 OS X terminal utility.
目录
相关文章
|
iOS开发 存储 Swift
iOS - Swift NSData 数据
前言 public class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding public class NSMutableData : NSData NSData 和它的可变长子类 NSMutableData 是字节缓冲区的对象化封装。
1731 0
|
iOS开发
IOS学习笔记二十四(NSData归档多个对象和归档对象实现深复制)
IOS学习笔记二十四(NSData归档多个对象和归档对象实现深复制)
149 0
|
存储 iOS开发
iOS - OC NSData 数据
前言 @interface NSData : NSObject @interface NSMutableData : NSData NSData 和它的可变长子类 NSMutableData 是字节缓冲区的对象化封装。
1092 0
|
数据安全/隐私保护 iOS开发
ios afnetworking base64 乱码问题
     处理json 时出现乱码 ,使用 utf-8  或者 base64 都不能解决,最后发现…… [@"中文1" dataUsingEncoding:NSUTF8StringEncoding] 或 [NSString stringWithCString:[@"中文1...
865 0
|
iOS开发
iOS NSString 和NSData 转换
NSString 转换成NSData 对象  NSData* xmlData = [@"testdata" dataUsingEncoding:NSUTF8StringEncoding]; NSData 转换成NSString对象  NSData * data; NSString *resul...
930 0
|
iOS开发 HTML5 移动开发
iOS 基础类解析 - NSData、NSMutableData
iOS 基础类解析 - NSData、NSMutableData 太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循“署名-非商业用途-保持一致”创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS、Android、Html5、Arduino、pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作。
1093 0
|
存储 编解码 数据安全/隐私保护
iOS 中 Base64 编解码分类实现
iOS 中 Base64 编解码分类实现 太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循“署名-非商业用途-保持一致”创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS、Android、Html5、Arduino、pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作。
1183 0
|
iOS开发
IOS开发---菜鸟学习之路--(十六)-将Image转换为Base64
我们直接在.m文件的引用头文件部分 和 @interface   AddPictureViewController ()  之间  加入 增加部分的代码 然后就可以使用图片转Base64了 #import "AddPictureViewController.
908 0