Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NSSecureCoding support for quick implementation macro MJSecureCodingImplementation(class, isSupport). #821

Merged
merged 3 commits into from
Sep 13, 2021

Conversation

wolfcon
Copy link
Collaborator

@wolfcon wolfcon commented Sep 9, 2021

Why

Old archive and unarchive methods are deprecated since iOS 12. So supporting secure coding is necessary job to accomplish.

How to use

Using MJSecureCodingImplementation(class, isSupport) macro to implement NSSecureCoding in a quite easy way, just like old MJCodingImplementation.

Archive and Unarchive

NSError *error = nil;
// Archive
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:bag requiringSecureCoding:YES error:&error];
[data writeToFile:file atomically:true];

// Unarchive
NSData *readData = [NSFileManager.defaultManager contentsAtPath:file];
error = nil;
MJBag *decodedBag = [NSKeyedUnarchiver unarchivedObjectOfClass:MJBag.class fromData:readData error:&error];

…onSecureCodingImplementationSupport(class, isSupport)``.
@@ -43,7 +43,8 @@ - (void)mj_decode:(NSCoder *)decoder
if (allowedCodingPropertyNames.count && ![allowedCodingPropertyNames containsObject:property.name]) return;
if ([ignoredCodingPropertyNames containsObject:property.name]) return;

id value = [decoder decodeObjectForKey:property.name];
// fixed `-[NSKeyedUnarchiver validateAllowedClass:forKey:] allowed unarchiving safe plist type ''NSNumber'(This will be disallowed in the future.)` warning.
id value = [decoder decodeObjectOfClasses:[NSSet setWithObjects:NSNumber.class, property.type.typeClass, nil] forKey:property.name];
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These code is for iOS 15 support.⚠️

iOS 15 would get some warning by using old method. This is a patch for that

@kinarobin
Copy link
Collaborator

MJExtensionSecureCodingImplementationSupport 太长了, MJSecureCodingImplementation 是不是简洁一点?

@wolfcon
Copy link
Collaborator Author

wolfcon commented Sep 13, 2021

MJExtensionSecureCodingImplementationSupport 太长了, MJSecureCodingImplementation 是不是简洁一点?

#define MJExtensionCodingImplementation MJCodingImplementation

主要是参照这个给了个命名哈哈哈, 确实, 改短点吧.🤣

@wolfcon wolfcon changed the title Add NSSecureCoding support for quick implementation macro MJExtensionSecureCodingImplementationSupport(class, isSupport). Add NSSecureCoding support for quick implementation macro MJSecureCodingImplementation(class, isSupport). Sep 13, 2021
@wolfcon wolfcon merged commit e31c9a6 into master Sep 13, 2021
@wolfcon wolfcon deleted the feature/support-NSSecureCoding branch September 13, 2021 09:15
@VeinGuo
Copy link
Contributor

VeinGuo commented Dec 3, 2021

@wolfcon
decodeObjectOfClasses 时
如果是Array 类型 需要对 Array 内的 类型 也放入 NSSet 不然会解析失败;

PR: #830

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants