4

I have the following warning (Xcode 10.1 - iOS 12.1)

'initForReadingWithData:' is deprecated: first deprecated in iOS 12.0 - Use -initForReadingFromData:error: instead*

When I'm change the method to initForReadingFromData, the NSKeyedUnarchiver returns nil.

// Current code which produces the warning (but works fine) : 

NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];

NSMutableArray *loadedCredentialIdentities = (NSMutableArray *)[unarchiver decodeObjectForKey:kStoredCredentialIdentities];

[unarchiver finishDecoding];

...

// using initForReadingFromData produces no warning (but doesn't work - loadedCredentialIdentities is nil) : 


NSError *error = nil;
NSKeyedUnarchiver *unarchiver = unarchiver = [[NSKeyedUnarchiver    alloc] initForReadingFromData:data error:&error];

NSMutableArray *loadedCredentialIdentities = (NSMutableArray *)[unarchiver decodeObjectForKey:kStoredCredentialIdentities];

[unarchiver finishDecoding];
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Frank Möller
  • 301
  • 2
  • 7

3 Answers3

6

Turning off secureCoding solved the problem.

[[NSKeyedUnarchiver alloc] initForReadingFromData:data error:&error]; 

unarchiver.requiresSecureCoding = NO;

[unarchiver decodeObjectForKey:...] 

In cases you don't have to be backward compatible it's better not to turn off secureCoding

dengApro
  • 3,848
  • 2
  • 27
  • 41
Frank Möller
  • 301
  • 2
  • 7
1

It because you are using decodeObjectForKey. If you don't set requiresSecureCoding to NO, you must use decodeObjectOfClass:forKey: instead.

Thomas A.
  • 11
  • 2
0

Watch this video from WWDC 2018 and then fix your unarchivers to use secure coding: https://developer.apple.com/videos/play/wwdc2018/222/

Vladimir Grigorov
  • 10,903
  • 8
  • 60
  • 70