37

I want to store some userinfo as a string in Keychain on iOS, so is there any length limit of the string in Keychain?

Thanks.

fannheyward
  • 18,599
  • 12
  • 71
  • 109

3 Answers3

62

I threw together an iOS app that would do a binary search using this library to interact with the keychain, and determined that the most I could store was an NS(Mutable)String with length 16,777,110. So noticeably less than either the max length of an NSString or the default value of SQLITE_MAX_LENGTH as suggested in other answers.

EDIT: If you're looking to quickly verify this answer, I'd suggest trying to store a String with length 16,777,110 in the keychain, and then trying one with length 16,777,111. If the first one works and the second one doesn't, this answer is correct.

Mark
  • 1,746
  • 2
  • 18
  • 19
16

It's hard to answer :) It should support the maximum length of NSString

So the Keychain string can hold a little over 4.2 billion characters same as NSString

Shamsudheen TK
  • 30,739
  • 9
  • 69
  • 102
5

The keychain of iOS is realized as SQLite-database (see here for example). So the max length of a string stored in an SQLite-database could be an upper bound for the max length for a string in the keychain.

According to this page on sqlite.org the max number of bytes for a string or BLOB is limited by the preprocessor macro SQLITE_MAX_LENGTH, whose default-value is 10^9. So assuming a 16-bit-encoding of the string, the max length of a string would be the half of this value. However, it could be that Apple uses a lower value for SQLITE_MAX_LENGTH.

user1364368
  • 1,474
  • 1
  • 16
  • 22