Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monitoring Keychain item changes in Swift

The Keychain Services API allows adding/updating/deleting items from the keychain. However, it doesn't seem to include any mechanism for watching a specific keychain item for changes.

My app is storing a value in the keychain, and I want to be notified if that value gets changed by the user or another process. Is such a thing possible, or will I just have to periodically poll the keychain item and check for changes myself?

like image 626
sschilli Avatar asked Jan 24 '26 12:01

sschilli


1 Answers

Here is API of Security framework for your purpose

/**
    @function SecKeychainAddCallback
    @abstract Registers your keychain event callback function
    @param callbackFunction A pointer to your keychain event callback function, described in SecKeychainCallback. You indicate the type of keychain events you want to receive by passing a bit mask of the desired events in the eventMask parameter.
    @param eventMask A bit mask indicating the keychain events that your application wishes to be notified of. See SecKeychainEventMask for a description of this bit mask. The Keychain Manager tests this mask to determine the keychain events that you wish to receive, and passes these events in the keychainEvent parameter of your callback function. See SecKeychainEvent for a description of these events.
    @param userContext A pointer to application-defined storage that will be passed to your callback function. Your application can use this to associate any particular call of SecKeychainAddCallback with any particular call of your keychain event callback function.
    @result A result code.  See "Security Error Codes" (SecBase.h).
*/
public func SecKeychainAddCallback(_ callbackFunction: SecKeychainCallback, 
    _ eventMask: SecKeychainEventMask, 
    _ userContext: UnsafeMutableRawPointer?) -> OSStatus
like image 82
Asperi Avatar answered Jan 27 '26 05:01

Asperi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!