Is it possible to write a UIView to disk using NSSecureCoding. The below code results in an error.
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:object requiringSecureCoding:YES error:&error];
Error: The data couldn’t be written because it isn’t in the correct format.
We also tried the following:
NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initRequiringSecureCoding:YES];
[archiver encodeObject:view forKey:@"view"];
[archiver finishEncoding];
Error: This decoder will only decode classes that adopt NSSecureCoding. Class 'UIView' does not adopt it.
NSSecureCoding, in addition to NSCoding requirements, simply requires the class to implement a class function +(BOOL)supportsSecureCoding. UIView already supports NSCoding and it seems like it might be an oversight that it doesn't already conform to NSSecureCoding; the Xcode debugger issues warnings about non-NSSecureCoding serialization calls going away in the distant future.
You can add the class function to UIView using a category:
So as pointed out in the comments, this doesn't mean that you'll be able to deserialize with the NSKeyedUnarchiver because it appears that UIViews weren't intended to be serialized that way. I'm guessing the main reason they support serialization is for xibs/nibs/storyboards. Here's an example of UIView serialization that DOES work, but uses private APIs, so it's for illustrative purposes only:
Add declarations to access unpublished APIs:
Serialization/Deserialization: