【发布时间】:2015-09-27 08:55:32
【问题描述】:
如何使用 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 编码没有行长限制,所以我该如何解决这个问题。
【问题讨论】:
-
你能不能在得到字符串后不删除任何新行?使用
NSDataBase64EncodingEndLineWithCarriageReturn,然后将\n 替换为stringByReplacingOccurrencesOfString:withString:。
标签: ios objective-c ios8 base64 ios9