Am I doing this right? I have this code is a simple Xamarin.Mac app.
var cookieStorage = NSHttpCookieStorage.SharedStorage;
var cookies2 = cookieStorage.Cookies;
cookies2 is always empty!
/Users//Library/Cookies has plenty of cookies, as does the Privacy section in the Safari Preferences.
Here's my environment.
=== Visual Studio Community 2019 for Mac ===
Version 8.7.8 (build 4)
Installation UUID: aaddbdd9-5a26-4ee5-abd9-1a5540d751e7
GTK+ 2.24.23 (Raleigh theme)
Xamarin.Mac 6.18.0.23 (d16-6 / 088c73638)
Package version: 612000093
=== Mono Framework MDK ===
Runtime:
Mono 6.12.0.93 (2020-02/620cf538206) (64-bit)
Package version: 612000093
=== Roslyn (Language Service) ===
3.7.0-6.20427.1+18ede13943b0bfae1b44ef078b2f3923159bcd32
=== NuGet ===
Version: 5.7.0.6702
=== .NET Core SDK ===
SDK: /usr/local/share/dotnet/sdk/3.1.402/Sdks
SDK Versions:
3.1.402
3.1.401
3.1.302
3.1.301
3.1.300
3.1.202
3.1.200
3.1.102
3.1.100
3.0.101
2.1.700
2.1.4
MSBuild SDKs: /Library/Frameworks/Mono.framework/Versions/6.12.0/lib/mono/msbuild/Current/bin/Sdks
=== .NET Core Runtime ===
Runtime: /usr/local/share/dotnet/dotnet
Runtime Versions:
3.1.8
3.1.7
3.1.6
3.1.5
3.1.4
3.1.2
3.1.0
3.0.1
2.1.22
2.1.21
2.1.20
2.1.19
2.1.18
2.1.17
2.1.16
2.1.15
2.1.14
2.1.11
2.0.5
=== Xamarin.Profiler ===
Version: 1.6.12.29
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler
=== Updater ===
Version: 11
=== Apple Developer Tools ===
Xcode 12.0.1 (17220)
Build 12A7300
=== Xamarin.Mac ===
Version: 6.20.2.2 (Visual Studio Community)
Hash: 817b6f72a
Branch: d16-7
Build date: 2020-07-18 18:44:59-0400
=== Xamarin.iOS ===
Version: 14.0.0.0 (Visual Studio Community)
Hash: 7ec3751a1
Branch: xcode12
Build date: 2020-09-16 11:33:15-0400
=== Xamarin Designer ===
Version: 16.7.0.495
Hash: 03d50a221
Branch: remotes/origin/d16-7-vsmac
Build date: 2020-08-28 13:12:52 UTC
=== Xamarin.Android ===
Version: 11.0.2.0 (Visual Studio Community)
Commit: xamarin-android/d16-7/025fde9
Android SDK: /Users/hpadmin/Library/Developer/Xamarin/android-sdk-macosx
Supported Android versions:
6.0 (API level 23)
7.1 (API level 25)
8.1 (API level 27)
SDK Tools Version: 26.1.1
SDK Platform Tools Version: 30.0.4
SDK Build Tools Version: 30.0.2
Build Information:
Mono: 83105ba
Java.Interop: xamarin/java.interop/[email protected]
ProGuard: Guardsquare/proguard/[email protected]
SQLite: xamarin/sqlite/[email protected]
Xamarin.Android Tools: xamarin/xamarin-android-tools/[email protected]
=== Microsoft OpenJDK for Mobile ===
Java SDK: /Users/hpadmin/Library/Developer/Xamarin/jdk/microsoft_dist_openjdk_1.8.0.25
1.8.0-25
Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL
=== Android SDK Manager ===
Version: 16.7.0.13
Hash: 8380518
Branch: remotes/origin/d16-7~2
Build date: 2020-09-16 05:12:24 UTC
=== Android Device Manager ===
Version: 16.7.0.24
Hash: bb090a3
Branch: remotes/origin/d16-7
Build date: 2020-09-16 05:12:46 UTC
=== Build Information ===
Release ID: 807080004
Git revision: 9ea7bef96d65cdc3f4288014a799026ccb1993bc
Build date: 2020-09-16 17:22:54-04
Build branch: release-8.7
Xamarin extensions: 9ea7bef96d65cdc3f4288014a799026ccb1993bc
=== Operating System ===
Mac OS X 10.15.6
Darwin 19.6.0 Darwin Kernel Version 19.6.0
Thu Jun 18 20:49:00 PDT 2020
root:xnu-6153.141.1~1/RELEASE_X86_64 x86_64
Refer to the Apple doc:
https://developer.apple.com/documentation/foundation/nshttpcookiestorage
We will get that each app has its own cookie storage. We can't use this API to read all the cookies on the device which doesn't belong to the app itself.
And we can use NSHttpCookieStorage.GetSharedCookieStorage("groupContainerIdentifier");
to access the cookies in the same group.
However, we need to group them first:
https://developer.apple.com/documentation/bundleresources/entitlements/com_apple_security_application-groups
Answers
Refer to the Apple doc:
https://developer.apple.com/documentation/foundation/nshttpcookiestorage
We will get that each app has its own cookie storage. We can't use this API to read all the cookies on the device which doesn't belong to the app itself.
And we can use
NSHttpCookieStorage.GetSharedCookieStorage("groupContainerIdentifier");
to access the cookies in the same group.However, we need to group them first:
https://developer.apple.com/documentation/bundleresources/entitlements/com_apple_security_application-groups
Thanks @LandLu, that makes sense. I will add a WebView to my test app and see what I get.
Waiting for your update.
@LandLu , it worked great. I added a WebView and after visiting some websites, I was able to see all kinds of cookies. Thanks again for your timely, and relevant answer. You guys are the best!