ioscookiesnshttpcookienshttpcookiestorage

How to listen for cookie being set in iOS NSHTTPCookieStorage?


I'd like to know when a cookie is set in NSHTTPCookieStorage. Is there an on change method or event handler? I'd like to prevent a cookie from being set.


Solution

  • You can listen to the cookie changes by the following notification.

    NSHTTPCookieManagerCookiesChangedNotification.

    Apple documentation link below.

    https://developer.apple.com/documentation/foundation/nshttpcookiemanagercookieschangednotification?language=objc

    Example:

    [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(methodYouWantToInvoke) //note the ":" - should take an NSNotification as parameter
                                                     name:NSHTTPCookieManagerCookiesChangedNotification
                                                   object:nil];