Last active
August 29, 2015 14:26
-
-
Save douglashill/05d8ac23e6435ae578ad to your computer and use it in GitHub Desktop.
How to enable secure decoding with NSKeyedUnarchiver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// It isn’t immediately obvious how to enable secure decoding with NSKeyedUnarchiver. | |
// NSKeyedUnarchiver has a read-write requiresSecureCoding property, but we normally use: | |
id decodedObject = [NSKeyedUnarchiver unarchiveObjectWithData:archive]; | |
// so never see an instance to be able to set this property. | |
// However, it is quite simple. The critical part is that we use the | |
// normal decodeObject… methods, passing the key of the root object. | |
NSData *archive; | |
Class rootObjectClass; | |
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:archive]; | |
[unarchiver setRequiresSecureCoding:YES]; | |
id decodedObject = [unarchiver decodeObjectOfClass:rootObjectClass forKey:NSKeyedArchiveRootObjectKey]; | |
[unarchiver finishDecoding]; | |
// You can verify this is right by decompiling unarchiveObjectWithData. | |
// If the root object is a collection, use decodeObjectOfClasses. | |
// finishDecoding does not do anything except notifiy the unarchiver’s delegate. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment