如何在没有行长度限制的情况下使用 ios8 和 ios9 api 对字符串进行 base64 编码。
我正在准备一些自定义的基本身份验证,我需要根据标准对凭证进行编码,这意味着:
然后使用 Base64 的 RFC2045-MIME 变体对生成的字符串进行编码,但不限于 76 个字符/行
在旧的 ios7 中有一个方法:NSData base64Encoding 但现在已弃用,取而代之的是:
- (NSString *)base64EncodedStringWithOptions:
(NSDataBase64EncodingOptions)options NS_AVAILABLE(10_9, 7_0);
选项是:
typedef NS_OPTIONS(NSUInteger, NSDataBase64EncodingOptions) {
// Use zero or one of the following to control the maximum line length after which a line ending is inserted. No line endings are inserted by default.
NSDataBase64Encoding64CharacterLineLength = 1UL << 0,
NSDataBase64Encoding76CharacterLineLength = 1UL << 1,
// Use zero or more of the following to specify which kind of line ending is inserted. The default line ending is CR LF.
NSDataBase64EncodingEndLineWithCarriageReturn = 1UL << 4,
NSDataBase64EncodingEndLineWithLineFeed = 1UL << 5,
} NS_ENUM_AVAILABLE(10_9, 7_0);
所以我可以选择行长 64 或 76。用于基本身份验证的 base64 编码没有行长限制,所以我该如何处理。
最佳答案
如果您不选择任何选项,line endings are not added .
[myData base64EncodedStringWithOptions:0]; // One long string
关于ios - 使用 ios8/ios9 api 的 base64 编码没有行长度限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32806200/