Skip to content

Instantly share code, notes, and snippets.

@douglashill
Last active August 29, 2015 14:26
Show Gist options
  • Save douglashill/05d8ac23e6435ae578ad to your computer and use it in GitHub Desktop.
Save douglashill/05d8ac23e6435ae578ad to your computer and use it in GitHub Desktop.
How to enable secure decoding with NSKeyedUnarchiver
// 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