:wave: Hi, does anyone know anything about how Kt...
# ktor
m
👋 Hi, does anyone know anything about how Ktor syncs with cookies on iOS side? running into an issue where its not picking up new cookies I set https://stackoverflow.com/questions/73966313/ios-shared-cookie-storage-not-synced-with-ktor-client
a
Unfortunately, there is no such syncing mechanism in Ktor. Could you please file a feature request?
m
Thats too bad. By using NSURLSession Ktor is intrinsicly using NSHttpCookieStorage whether it knows it or not. Would love to see a feature where Ktor understands this relationship and uses NSHttpCookieStorage instead of artificially creating a mirrored Cookie Storage on top of it, like it currently seems to be doing
Thanks for responding! I’ll try to submit feature request when I get time
a
Please note that by default Ktor creates NSURLSession without NSHttpCookieStorage (there is an explicit call to
setHTTPCookieStorage(null)
).
m
huh, I wonder if setting that to null doesn’t do what you’d expect then… I have an observer setup to listen to NSHTTPCookieManagerCookiesChangedNotification , and while making requests with Ktor I get notifications on this whenever I get a set-cookie in the response.
So the NSHttpCookieStorage is somehow still receiving cookies
a
Do you you pass a session object to the
preconfiguredSession
property?
m
oh, actually I have this code that sets the shared cookie storage to the
defaultSessionConfiguration
, so probably Ktor is setting it to null, but I’m setting it back again 🤦‍♂️
Let me double check some things, but this already helps my understanding a lot
Is there a reason Ktor client didn’t opt for using built in systems for handling cookies on iOS and Android? The reason is I’m looking for a way to use same cookie storage between http requests and web views.
And fyi I double checked - cookies are getting set to the iOS shared NSHttpCookieStorage from Ktor requests, even without the cookie plugin installed. You can check by registering for notifications:
Copy code
[[NSNotificationCenter defaultCenter] addObserverForName:NSHTTPCookieManagerCookiesChangedNotification object:[NSHTTPCookieStorage sharedHTTPCookieStorage] queue:nil usingBlock:^(NSNotification * _Nonnull note) {
    NSHTTPCookieStorage *storage = note.object;
    NSArray *sessionCookies = [storage cookiesForURL:[NSURL URLWithString:@".<http://mydomain.com|mydomain.com>"]];
}];
This is obj-c but you could easily convert to kotlin or swift.