I'm trying to access my backend API using a URLSessionDataTask
. Part of this includes accessing cookies stored using Safari.
For example, the php code in the backend for http://api.example.com/v1/getCookies
is
echo $_COOKIE["myCookieName"];
The cookies do exist on iOS Safari, yet when a URLSessionDataTask
request is made to the website
let url: URL = URL(string: "http://api.example.com/v1/getCookies")!
let task = Foundation.URLSession(
configuration: URLSessionConfiguration.default,
delegate: self,
delegateQueue: OperationQueue.main
).dataTask(with: url)
task.resume()
Instead of the value of the cookie being sent as a response to the request, the response is that the cookie is unset. It seems as if cookies from URLSessions are separate than cookies in Safari.
The cookie is stored under the domain .example.com
, and the path /
.
Is there any way that I can access cookies set on Safari from my app? For example, I'd like something like this to work (I'm doing something more complicated, but this essentially shows what I would like to work)
- User visits example.com and chooses a nickname
- The user's chosen nickname is stored in a cookie
- The user decides to download the example.com mobile app
- The app makes a request to example.com, and the nickname that is stored in the cookie is returned
Is there any way to do something like this? neither HTTPCookie.cookies
nor HTTPCookieStorage.shared.cookies
seem to work.