iOS Question How to delete all keychain entries for an app, even if they are unknown?

JordiCP

Expert
Licensed User
Longtime User
My app uses a 3rd party authorization framework which stores some data in the keychain. For this, the app provides the needed entitlements.

The framework stores account ids, tokens and other user data, which I don't want to be available when the app is deleted and reinstalled.

If I knew the keys, I guess it would be possible with phone.keyChainRemove(..) but in this case I don't know which keys were stored, just want to bulk erase all the entries so that the framework behaves as with a fresh install.

Hope it makes sense. Is it possible?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    Dim p As Phone
    p.KeyChainPut("aaa", "bbb")
    Log(p.KeyChainGet("aaa"))
    Me.As(NativeObject).RunMethod("deleteAllKeychainItems", Null)
    Log(p.KeyChainGet("aaa"))
    
End Sub

#if objc
- (void)deleteAllKeychainItems {
    NSArray *secItemClasses = @[(__bridge id)kSecClassGenericPassword,
                           (__bridge id)kSecClassInternetPassword,
                           (__bridge id)kSecClassCertificate,
                           (__bridge id)kSecClassKey,
                           (__bridge id)kSecClassIdentity];
    for (id secItemClass in secItemClasses) {
        NSDictionary *spec = @{(__bridge id)kSecClass: secItemClass};
        SecItemDelete((__bridge CFDictionaryRef)spec);
    }
}
#End If
Based on: https://stackoverflow.com/questions...-items-accessible-to-an-app?noredirect=1&lq=1
 
Upvote 0
Top