diff --git a/Airship/AirshipCore/Source/AirshipKeychainAccess.swift b/Airship/AirshipCore/Source/AirshipKeychainAccess.swift index 6f128dc4c..81c75f0cb 100644 --- a/Airship/AirshipCore/Source/AirshipKeychainAccess.swift +++ b/Airship/AirshipCore/Source/AirshipKeychainAccess.swift @@ -61,15 +61,16 @@ public class AirshipKeychainAccess: NSObject { identifier: identifier, service: self.service ) - - // Delete old + + // Write to old location in case of a downgrade if let bundleID = Bundle.main.bundleIdentifier { - Keychain.deleteCredentials( + let _ = Keychain.writeCredentials( + credentials, identifier: identifier, service: bundleID ) } - + completionHandler?(result) } } @@ -135,39 +136,35 @@ public class AirshipKeychainAccess: NSObject { /// Helper method that migrates data from the old storage location to the new on read. private func readCredentialsHelper(identifier: String) -> AirshipKeychainCredentials? { - var credentials = Keychain.readCredentials( + if let credentials = Keychain.readCredentials( identifier: identifier, service: self.service - ) - + ) { + return credentials + } + // If we do not have a new value, check // the old service location - if credentials == nil, let bundleID = Bundle.main.bundleIdentifier { - - credentials = Keychain.readCredentials( + if let bundleID = Bundle.main.bundleIdentifier { + + let old = Keychain.readCredentials( identifier: identifier, service: bundleID ) - - if let credentials = credentials { + + if let old = old { // Migrate old data to new service location - let result = Keychain.writeCredentials( - credentials, + let _ = Keychain.writeCredentials( + old, identifier: identifier, service: self.service ) - if (result) { - Keychain.deleteCredentials( - identifier: identifier, - service: bundleID - ) - } else { - AirshipLogger.error("Failed to migrate credentials") - } + + return old } } - - return credentials + + return nil } }