xamarin / xamarin-macios

.NET for iOS, Mac Catalyst, macOS, and tvOS provide open-source bindings of the Apple SDKs for use with .NET managed languages such as C#
Other
2.48k stars 514 forks source link

How to add observer to WKHTTPCookieStore ? #9984

Open SeRgI1982 opened 4 years ago

SeRgI1982 commented 4 years ago

Hi,

I know that there is an interface IWKHttpCookieStoreObserver but how to use it ? I have implemented in that way:

        public class CookieStoreObserver : NSObject, IWKHttpCookieStoreObserver
        {
            public void CookiesDidChangeInCookieStore(WKHttpCookieStore cookieStore)
            {

            }
        }

and added to

WKWebsiteDataStore.DefaultDataStore.HttpCookieStore

but it is never called. I couldn't find any samples in the internet, so this place is my last chance.

chamons commented 4 years ago

Thanks for the feedback.

Next time consider attaching a sample project showing your problem.

Reviewing the API, it appears WKHttpCookieStore.AddObserver sets up use of a IWKHttpCookieStoreObserver. Have you tried using that?

SeRgI1982 commented 4 years ago

It is a part of my company project, so I couldn't give you a real sample. I will try to prepare sample today evening.

About your question, yes I did exactly like you suggested. I have created this CookieStoreObserver and added an instance of this object to WKWebsiteDataStore.DefaultDataStore.HttpCookiesStore by AddObserver method. I know that generated cookie is added to this cookies store because we used it to check it if we are authenticated in SharePoint service.

Currently, it is much more like pooling approach - check if in cookie store there is a cookie which has in his property Domain a proper server name (NSHttpCookie.Domain).

I would like to avoid pooling approach and raise an event when proper cookie appears in cookie store.

SeRgI1982 commented 4 years ago

XFMacSample 4.zip

Take a look on class CookieStore (XFMacSample.macOS)

One time, when I used System.Net.Cookie as a parameter for NSHttpCookie it started to work but I didn't change nothing and when I run app once again, it stopped working. Of course in this sample, I add cookie manually.

In our production app, we use WebView (WKWebView under the hood on macOS side) where we navigate to standard Microsoft login page to login to the SharePoint. As I described above, we know that successful login creates cookie and adds to WKHttpCookieStore because we check this cookie storage in a loop. We would like to move responsibility about appearing a new cookie in WKHttpCookieStore to our class CookieStore to have a chance remove this approach with loop. When observer do his job properly, it shouldn't be a problem.

rolfbjarne commented 3 years ago

Sorry for the late reply, we've been busy :smile:

Your sample works for me if I store the CookieStoreObserver instance in a field:

CookieStoreObserver observer;

public CookieStore ()
{
        observer = new CookieStoreObserver ();
        observer.CookiesDidChanged += OnCookiesDidChanged;
        WKWebsiteDataStore.DefaultDataStore.HttpCookieStore.AddObserver (observer);
}

then I get a dialog saying a cookie was added.

Apple's documentation about adding the observer for the event says: "The cookie store doesn’t maintain a strong reference to the object you specify."

Admittedly this is not something C# developers should have to think about, but I'm not sure we can change the API in any case. However, we can (and should) reflect this in the documentation, so I'm keeping this open as a documentation issue.