capacitor: bug: 3rd party cookies not working on capacitor iOS
Bug Report
Capacitor Version
Latest Dependencies:
@capacitor/cli: 4.6.3
@capacitor/core: 4.6.3
@capacitor/android: 4.6.3
@capacitor/ios: 4.6.3
Installed Dependencies:
@capacitor/cli: 4.6.3
@capacitor/core: 4.6.3
@capacitor/android: 4.6.3
@capacitor/ios: 4.6.3
Platform(s)
ios
Current Behavior
When setting CapacitorConfig.server.hostname to localhost
in capacitor config (default value, recommended to keep it that way in the doc), 3rd party cookies are not working.
other settings:
CapacitorConfig.ios.limitsNavigationsToAppBoundDomains: true
localhost
+ 3rd party domain in Info.plist > WKAppBoundDomains
The doc of capacitor cookies suggest that 3rd party cookies are supposed to work
https://capacitorjs.com/docs/apis/cookies#third-party-cookies-on-ios
Expected Behavior
3rd party cookies should be set and used correctly by the capacitor webview
Code Reproduction
hard to create a reproduction setup as it involves a backend service sending cookies to set
About this issue
- Original URL
- State: open
- Created a year ago
- Reactions: 9
- Comments: 15
I can confirm this is not working as advertised in Capacitor 5.0.4 and iOS 14.
Hacking
capacitor.config.json
does work:But this is not recommended. Leaving it as
localhost
and addingWKAppBoundDomains
(as advised by https://capacitorjs.com/docs/apis/cookies) does not work.I’m sure the above workarounds (hack a script to replace
capacitor.config.json
and/or usecapacitor.config.ts
so that you can update iOS but not Android) work. However here’s another option in case both of those aren’t suitable for you (they weren’t for me):This can be applied as instructed here https://capacitorjs.com/docs/ios/viewcontroller
A real fix would be really appreciated!
We are facing the same problem. We use a Session-Cookie which gets issued by our backend running on a custom domain which should be appended on any subsequent requests. Our WebView is running on localhost and the option to change it to the backend URL is not feasible, because we are relying on other secure APIs (like UserMedia).
We use the following settings:
Third party cookies seems to not get appended, although the docs seem to imply that (though they are not very detailed)
The only thing that helps is adding the NSCrossWebsiteTrackingUsageDescription to the Info.plist and allowing tracking in the iOS settings for our app. But this is of course not a suitable production solution.
Is there any other way we can get our third party cookie appended?
Update: I just used the XCode Network Instrument to take a look at the requests and it seems like our Session-Cookie does get appended. Nonetheless, the image we embed from the server cannot be displayed in the WebView. It seems to be another problem then, but it certainly has to do with Cross-site-tracking because enabling this in the IOS settings fixes the problem.
I never used my live reload on an iOS device, we usually develop web-based and then do the testing on the mobile devices. We don’t set the server.hostname until the build, and remove it during the capacitor copy step for android builds (as they don’t have the problem). Edit: Eventhough its a hard decision, and it pushes devs to use other authentication or communication paths, lets try to make the world a third party cookie free world. (I work in legacy, so it’s hard to solve it for me as well, but I do support the decision).
Setting the hostname makes the app think its that site. Doing this makes the cookie not “thirdparty” anymore. Because the cookie was issued for api.example.com, and the hostname is also api.example.com. Keep in mind this only works for iOS, on android XHR calls will fail because it tries to resolve your requests against the device itself.
On Mon, 15 Apr 2024, 23:29 Anton Begehr, @.***> wrote:
Hello, guys, same behavior here:
CapacitorCookies
enabled incapacitor.config.ts
.WKAppBoundDomains
set inInfo.plist
withlocalhost
andapi.service.com
.limitsNavigationsToAppBoundDomains
set totrue
also incapacitor.config.ts
.However, the
3rd party cookie
is not being sent untilserver: { host: api.service.com }
is set incapacitor.config.ts
. According to the docs, this last step shouldn’t be needed, right?Are we missing something else? 🤔
@gabides When we ran into this problem ( Januari 2022, close to the release of ionic 6 ) This was still a big issue because iOS didnt allow saving the cookies from 3rd party cookies.
Multiple issues were on at that moment, (I am reacting cause I am also curious if this is done nowadays). One of the issues was that cookie setting was done async (talking about capacitor) and therefor could not be guaranteed to be ready at time of using the other requests. (This is all old information).
As changing the localhost was not recommended, we went ahead and did it anyway (I know there are things like geolocation and media functions that have issues with it, but we didn’t use them. Our apps are being accepted and approved, sadly the capacitorconfig didnt use to have a seperate flag for ios hostname and android hostname (our issues were only present on ios). So if you go that road, keep in mind that changing the hostname of android will make it NAVIGATE to the hostname url. using the following code (depending on youyr buildsystem) you can make sure to remove the key: value pair from the capacitor config file after cap sync.
with
"capacitor:sync:after": "scripts/capacitor-sync-after.sh",
in your package.json.Just sharing what we produced back then, wondering if it is not the case any more 😃