NSSecureCoding: Writing UIView to disk

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.

Never serialize anything but the plist types, or classes that you control, to disk. NSSecureCoding will write out details of the data members for the class for a restoration. If you don't control the class, such as with UIView, you are subject to implementation details changing over different iOS releases, which means you won't be able to deserialize the class in the future when those implementation details change.

You should determine the real information you want to serialize to disk — usually part of your app's data model — and only store that information. When you destabilize this data, you then build the appropriate view hierarchy by creating the views at that time and setting your data into those views.

NSSecureCoding: Writing UIView to disk
 
 
Q