App Auth crash when unarchiving AuthState

218 views Asked by At

I'm implementing the App Auth library in my iOS app, and trying to clean up a couple warnings when archiving the Auth State.

'unarchiveObject(with:)' was deprecated in iOS 12.0: Use +unarchivedObjectOfClass:fromData:error: instead

and

'archivedData(withRootObject:)' was deprecated in iOS 12.0: Use +archivedDataWithRootObject:requiringSecureCoding:error: instead

The following works fine but with the warnings:

    private func loadAuthState() {
        guard let data = UserDefaults.standard.object(forKey: kAppAuthAuthStateKey) as? Data else {
            return
        }

        if let authState = NSKeyedUnarchiver.unarchiveObject(with: data) as? OIDAuthState {
            print("AuthState loaded. Access token: \(authState.lastTokenResponse?.accessToken ?? "DEFAULT_TOKEN")")
            self.authState = authState
            self.signedIn.update(with: .signedIn)
        }
    }

    private func saveAuthState() {
        if let authState = self.authState {
            let data = NSKeyedArchiver.archivedData(withRootObject: authState)
            
            UserDefaults.standard.set(data, forKey: kAppAuthAuthStateKey)
            UserDefaults.standard.synchronize()
            
        }
    }

After changing to the following I get a crash on the unarchive (Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'The response_type "(null)" isn't supported. AppAuth only supports the "code" or "code id_token" response_type.'):

    private func loadAuthState() {
        guard let data = UserDefaults.standard.object(forKey: kAppAuthAuthStateKey) as? Data else {
            return
        }

        if let authState = try? NSKeyedUnarchiver.unarchivedObject(ofClass: OIDAuthState.self, from: data) {
            print("AuthState loaded. Access token: \(authState.lastTokenResponse?.accessToken ?? "DEFAULT_TOKEN")")
            self.authState = authState
            self.signedIn.update(with: .signedIn)
        }
    }

    private func saveAuthState() {
        if let authState = self.authState {
            guard let data = try? NSKeyedArchiver.archivedData(withRootObject: authState, requiringSecureCoding: false) else {
                return
            }
            
            UserDefaults.standard.set(data, forKey: kAppAuthAuthStateKey)
            UserDefaults.standard.synchronize()
            
        }
    }
0

There are 0 answers