I am developing iOS app that loads a webpage using UIWebView
The web page sets four cookies:
- Two for Google Analytics, _ga and _gat
- One for my web application unique vistors counter
- One for my webpage to detect whether user has voted or not.
- If this cookie available then display the poll results.
From Firefox's Storage Inspector
I see all my cookies as expected, see screenshot:
However, from iOS I printed NSHTTPCookieStorage.sharedHTTPCookieStorage().cookies
from inside webViewDidFinishLoad
and there is only two of four expected cookies, also iOS users can vote as many times as they wanted because cookies are not detected (or not stored in iOS device):
if let cookies = NSHTTPCookieStorage.sharedHTTPCookieStorage().cookies { for c in cookies { print("*******************") print("name: \(c.name)") print("domain: \(c.domain)") print("value: \(c.value)") print("path: \(c.path)") print("expiresDate: \(c.expiresDate)") } }
Why my iOS app does not detect my custom cookies and only detects my Google Analytics ones?
NOTE 1: I have also added this code to my app:
func applicationDidBecomeActive(application: UIApplication) { NSHTTPCookieStorage.sharedHTTPCookieStorage().cookieAcceptPolicy = NSHTTPCookieAcceptPolicy.Always }
NOTE 2: My backend is SharePoint web application
2 Answers
Answers 1
A few ideas:
The device's Safari "Block Cookies" settings override any changes to the cookieAcceptPolicy
. The default policy of this setting on the device is "Allow from Websites I Visit," which depending on your iOS device's version, allows third-party cookies. Testing with iOS 9.3.x shows that third party cookies are persisted in a UIWebView
with the default settings, but do not persist with the settings "Allow from Current Website Only" or "Always Block."
Check if the device persists your cookie with the different Safari cookie settings.
Additionally, does your cookie have an expiration date set? I've found that cookies without an expiration date set are treated as session cookies in a UIWebView
, which only persist with the lifecycle of the web view that received it.
Answers 2
Changing the cookie acceptance policy in an app affects the cookie acceptance policy for all other running apps. When some other app changes the cookie storage or the cookie acceptance policy, NSHTTPCookieStorage
notifies an app by posting the NSHTTPCookieManagerCookiesChangedNotification
and NSHTTPCookieStorageAcceptPolicyChangedNotification
notifications. You can use these two to track if any other app has changed the cookie acceptance policy
or
You can use the function cookiesForURL
to retrieve the session cookies from a particular URL
let cookieStorage = NSHTTPCookieStorage.sharedHTTPCookieStorage() let yourURL = NSURL(string: URL) if let cookies = NSHTTPCookieStorage.cookiesForURL(cookieStorage)(yourURL!) { // print your cookie details }
0 comments:
Post a Comment