Base64与百分号编码相关 - iOS

本文介绍了Base64编码及其在iOS开发中的应用,包括如何使用NSData进行编码和解码操作。此外,还详细解释了百分号编码在URL处理中的作用,并提供了具体的编码与解码方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一. Base64 编码

Base64 是许多 web 协议的标准,在日常开发中很多地方会需要利用 Base64 进行编解码的操作;

在相互转换操作的时候,可以借用 NSData 来执行,例如接口中提供的如下方法:

/* Create an NSData from a Base-64 encoded NSString using the given options. By default, returns nil when the input is not recognized as valid Base-64.
*/
- (nullable instancetype)initWithBase64EncodedString:(NSString *)base64String options:(NSDataBase64DecodingOptions)options API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0));

/* Create a Base-64 encoded NSString from the receiver's contents using the given options.
*/
- (NSString *)base64EncodedStringWithOptions:(NSDataBase64EncodingOptions)options API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0));

其中,第一个方法用来编码时操作,官方大致意思为将一个字符串(NSString)类型的参数通过编码后转换为 NSData 类型,若所输入的数据未能被识别为有效 Base-64 时,则返回 nil.

第二个方法是将 NSData 类型数据通过 Base-64 的方式解码为字符串类型参数.

具体使用方式 code 如下:

1.Data 转 String

NSData *data = [[NSData alloc] initWithBase64EncodedString:@"Encoded String"
                                                   options:0];//编码
NSString *decodeStr = [data base64EncodedStringWithOptions:0];//解码
NSLog(@"data --- %@/n decodeStr --- %@", data, decodeStr);
2.字符串与 Data 间相互转换

NSString *encodeStr = @"Encode String Test";
NSData *encodeData = [[encodeStr dataUsingEncoding:NSUTF8StringEncoding] base64EncodedDataWithOptions:0];// 编码
NSData *decodeData = [[NSData alloc] initWithBase64EncodedData:encodeData
                                                           options:0];// 解码
NSString *decodeStr = [[NSString alloc] initWithData:decodeData
                                                encoding:NSUTF8StringEncoding];// Data 转 String
NSLog(@"编码:\n%@解码:\n%@Data to change String:\n%@", encodeData, decodeData, decodeStr);

二.百分号编码

百分号编码对 web 协议也很重要,尤其是对 URL 的处理上;

其中官方 API 提供的接口方法如下:

// Returns a new string made from the receiver by replacing all percent encoded sequences with the matching UTF-8 characters.
@property (nullable, readonly, copy) NSString *stringByRemovingPercentEncoding API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0));

// Returns a new string made from the receiver by replacing all characters not in the allowedCharacters set with percent encoded characters. UTF-8 encoding is used to determine the correct percent encoded characters. Entire URL strings cannot be percent-encoded. This method is intended to percent-encode an URL component or subcomponent string, NOT the entire URL string. Any characters in allowedCharacters outside of the 7-bit ASCII range are ignored.
- (nullable NSString *)stringByAddingPercentEncodingWithAllowedCharacters:(NSCharacterSet *)allowedCharacters API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0));
其中,第一项为将输入的字符串内容通过百分号编码序列后生成一个编码后的新字符串;

第二项与第一项类似,同样是将输入的字符串内容通过百分号编码序列的方式编码后生成一个新的字符串,但该方法允许开发者控制需要百分号编码的字符,目的是成一个 URL 字符串编码组件或子组件,而不是整个 URL 字符串.在7位 ASCII 范围外的任何字符被忽略.
具体使用方式 code 如下:

NSString *testStr = @"Percent Test";
NSString *encodStr = [testStr stringByRemovingPercentEncoding];// 编码
NSString *encodCharacters = [testStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];// 编码并控制所需百分号编码的字符

NSLog(@"\nPercentEncoding:\n%@\nPercentEncodingAndCharacters:\n%@", encodStr, encodCharacters);
打印结果如下:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值