Explorar el Código

Update deprecated archiving API usage (#449)

mdmathias hace 1 año
padre
commit
6fbd7b9976

+ 6 - 1
GoogleSignIn/Sources/GIDSignInButton.m

@@ -230,12 +230,17 @@ static UIColor *colorForStyleState(GIDSignInButtonColorScheme style,
 }
 
 - (void)encodeWithCoder:(NSCoder *)aCoder {
-  [super encodeWithCoder:aCoder];
   [aCoder encodeInteger:_style forKey:kStyleKey];
   [aCoder encodeInteger:_colorScheme forKey:kColorSchemeKey];
   [aCoder encodeInteger:_buttonState forKey:kButtonState];
 }
 
+#pragma mark - NSSecureCoding
+
++ (BOOL)supportsSecureCoding {
+  return YES;
+}
+
 #pragma mark - UI
 
 - (void)updateUI {

+ 1 - 1
GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignInButton.h

@@ -46,7 +46,7 @@ typedef NS_ENUM(NSInteger, GIDSignInButtonColorScheme) {
 /// control to an `IBAction`, or something similar, that calls
 /// signInWithPresentingViewController:completion: on `GIDSignIn` and add it to your view
 /// hierarchy.
-@interface GIDSignInButton : UIControl
+@interface GIDSignInButton : UIControl <NSSecureCoding>
 
 /// The layout style for the sign-in button.
 /// Possible values:

+ 11 - 3
GoogleSignIn/Tests/Unit/GIDConfigurationTest.m

@@ -99,9 +99,17 @@
 // Deprecated in iOS 13 and macOS 10.14
 - (void)testLegacyCoding {
   GIDConfiguration *configuration = [GIDConfiguration testInstance];
-  NSData *data = [NSKeyedArchiver archivedDataWithRootObject:configuration];
-  GIDConfiguration *newConfiguration = [NSKeyedUnarchiver unarchiveObjectWithData:data];
-  XCTAssertEqualObjects(configuration, newConfiguration);
+  NSError *archivedError;
+  NSData *data = [NSKeyedArchiver archivedDataWithRootObject:configuration
+                                       requiringSecureCoding:NO
+                                                       error:&archivedError];
+  XCTAssertNil(archivedError);
+  NSError *unArchivedError;
+  GIDConfiguration *newConfig = [NSKeyedUnarchiver unarchivedObjectOfClass:[GIDConfiguration class]
+                                                                  fromData:data
+                                                                     error:&unArchivedError];
+  XCTAssertNil(unArchivedError);
+  XCTAssertEqualObjects(configuration, newConfig);
   XCTAssertTrue(GIDConfiguration.supportsSecureCoding);
 }
 #endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST

+ 11 - 2
GoogleSignIn/Tests/Unit/GIDGoogleUserTest.m

@@ -133,8 +133,17 @@ static NSString *const kNewScope = @"newScope";
 - (void)testLegacyCoding {
   GIDGoogleUser *user = [[GIDGoogleUser alloc] initWithAuthState:[OIDAuthState testInstance]
                                                      profileData:[GIDProfileData testInstance]];
-  NSData *data = [NSKeyedArchiver archivedDataWithRootObject:user];
-  GIDGoogleUser *newUser = [NSKeyedUnarchiver unarchiveObjectWithData:data];
+  
+  NSError *archiveError;
+  NSData *data = [NSKeyedArchiver archivedDataWithRootObject:user
+                                       requiringSecureCoding:NO
+                                                       error:&archiveError];
+  XCTAssertNil(archiveError);
+  NSError *unarchiveError;
+  GIDGoogleUser *newUser = [NSKeyedUnarchiver unarchivedObjectOfClass:[GIDGoogleUser class]
+                                                             fromData:data
+                                                                error:&unarchiveError];
+  XCTAssertNil(unarchiveError);
   XCTAssertEqualObjects(user, newUser);
   XCTAssertTrue(GIDGoogleUser.supportsSecureCoding);
 }

+ 20 - 4
GoogleSignIn/Tests/Unit/GIDProfileDataTest.m

@@ -138,8 +138,16 @@ static NSString *const kFIFEAvatarURL2WithDimension =
 // Deprecated in iOS 13 and macOS 10.14
 - (void)testLegacyCoding {
   GIDProfileData *profileData = [self profileData];
-  NSData *data = [NSKeyedArchiver archivedDataWithRootObject:profileData];
-  GIDProfileData *newProfileData = [NSKeyedUnarchiver unarchiveObjectWithData:data];
+  NSError *archiveError;
+  NSData *data = [NSKeyedArchiver archivedDataWithRootObject:profileData
+                                       requiringSecureCoding:NO
+                                                       error:&archiveError];
+  XCTAssertNil(archiveError);
+  NSError *unarchiveError;
+  GIDProfileData *newProfileData = [NSKeyedUnarchiver unarchivedObjectOfClass:[GIDProfileData class]
+                                                                     fromData:data
+                                                                        error:&unarchiveError];
+  XCTAssertNil(unarchiveError);
   XCTAssertEqualObjects(profileData, newProfileData);
   XCTAssertTrue(GIDProfileData.supportsSecureCoding);
 }
@@ -149,8 +157,16 @@ static NSString *const kFIFEAvatarURL2WithDimension =
                                                                       name:kName
                                                                   imageURL:kFIFEImageURL];
   [NSKeyedArchiver setClassName:@"GIDProfileData" forClass:[GIDProfileDataOld class]];
-  NSData *data = [NSKeyedArchiver archivedDataWithRootObject:oldProfile];
-  GIDProfileData *profileData = [NSKeyedUnarchiver unarchiveObjectWithData:data];
+  NSError *archiveError;
+  NSData *data = [NSKeyedArchiver archivedDataWithRootObject:oldProfile
+                                       requiringSecureCoding:NO
+                                                       error:&archiveError];
+  XCTAssertNil(archiveError);
+  NSError *unarchiveError;
+  GIDProfileData *profileData = [NSKeyedUnarchiver unarchivedObjectOfClass:[GIDProfileData class]
+                                                                  fromData:data
+                                                                     error:&unarchiveError];
+  XCTAssertNil(unarchiveError);
   XCTAssertEqualObjects(profileData.email, kEmail);
   XCTAssertEqualObjects(profileData.name, kName);
   XCTAssertNil(profileData.givenName);

+ 10 - 2
GoogleSignIn/Tests/Unit/GIDSignInButtonTest.m

@@ -73,8 +73,16 @@ static NSString * const kAppBundleId = @"FakeBundleID";
   GIDSignInButton *button = [[GIDSignInButton alloc] init];
   button.style = kGIDSignInButtonStyleIconOnly;
   button.colorScheme = kGIDSignInButtonColorSchemeLight;
-  NSData *data = [NSKeyedArchiver archivedDataWithRootObject:button];
-  GIDSignInButton *newButton = [NSKeyedUnarchiver unarchiveObjectWithData:data];
+  NSError *archiveError;
+  NSData *data = [NSKeyedArchiver archivedDataWithRootObject:button
+                                       requiringSecureCoding:NO
+                                                       error:&archiveError];
+  XCTAssertNil(archiveError);
+  NSError *unarchiveError;
+  GIDSignInButton *newButton = [NSKeyedUnarchiver unarchivedObjectOfClass:[GIDSignInButton class]
+                                                                 fromData:data
+                                                                    error:&unarchiveError];
+  XCTAssertNil(unarchiveError);
   XCTAssertEqual(button.style, newButton.style);
   XCTAssertEqual(button.colorScheme, newButton.colorScheme);
 }