AppAuth-iOS icon indicating copy to clipboard operation
AppAuth-iOS copied to clipboard

Fixing the unarchivedObject

Open maamjadi opened this issue 2 years ago • 1 comments

NSCoder is not very good at decoding heterogeneous objects.. With this work the issue with decodeObject is fixed as result of which NSKeyedUnarchiver.unarchivedObject(ofClass: OIDAuthState.self, from: decoded) throws should work as expected. This PR is relate to the issue https://github.com/openid/AppAuth-iOS/issues/479

maamjadi avatar Apr 22 '22 12:04 maamjadi

Thank you! I can confirm this is working for me.

static let userDefaultSuiteName = "group.net.openid.appauth.XYZOAuth"
private var authState: OIDAuthState? {
    didSet {
        if let authState = authState,
            let userDefaults = UserDefaults(suiteName: XYZOAuth.userDefaultSuiteName),
            let data = try? NSKeyedArchiver.archivedData(withRootObject: authState, requiringSecureCoding: true) {
            userDefaults.set(data, forKey: "authState")
            userDefaults.synchronize()
        }
    }
}
let instance = XYZOAuth()
if let data = UserDefaults(suiteName: XYZOAuth.userDefaultSuiteName)?.object(forKey: "authState") as? Data,
   let authStateSaved = try? NSKeyedUnarchiver.unarchivedObject(ofClass: OIDAuthState.self, from: data) {
    instance.authState = authStateSaved
}
return instance

nitrag avatar Jun 28 '22 18:06 nitrag

Thanks for the PR. #479 has been addressed in the 1.6.0 release.

petea avatar Sep 12 '22 18:09 petea