NSHTTPCookieStorage - lost cookies in app termination (SIGKILL) iOS 8

892 views Asked by At

In my app registration i am getting & setting cookies from the server for user authentication, this are the header fields I'm getting from the server:

[Content-Type: application/json, Pragma: no-cache, X-Powered-By: PHP/5.5.29-1+deb.sury.org~precise+1, Set-Cookie: PHPSESSID_NEW=u3j95qff7100d87pimd56mfc37; expires=Fri, 12-Apr-2024 10:31:46 GMT; Max-Age=259200000; path=/, Server: nginx/1.1.19, Content-Encoding: gzip, Expires: Thu, 19 Nov 1981 08:52:00 GMT, Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0, Date: Mon, 25 Jan 2016 10:31:46 GMT, Content-Length: 439, Connection: keep-alive, X-CDN: Incapsula, Vary: Accept-Encoding, X-Iinfo: 8-3374242-3308240 SNNY RT(1453717901817 3717) q(0 0 0 0) r(5 5) U5]

I have tried two ways to set those fields with NSHTTPCookieStorage:

  func setCookies(allHeaderFields:[String : String], url:NSURL){

        let cookies = NSHTTPCookie.cookiesWithResponseHeaderFields(allHeaderFields, forURL: url)

        NSHTTPCookieStorage.sharedHTTPCookieStorage().setCookies(cookies, forURL: url, mainDocumentURL: nil)
  }

OR:

func setCookies(allHeaderFields:[String : String], url:NSURL){

    let cookies = NSHTTPCookie.cookiesWithResponseHeaderFields(allHeaderFields, forURL: url)

    for cookie in cookies {

        var cookieProperties                  = Dictionary<String,AnyObject>()
        cookieProperties[NSHTTPCookieName]    = cookie.name
        cookieProperties[NSHTTPCookieValue]   = cookie.value
        cookieProperties[NSHTTPCookieDomain]  = cookie.domain
        cookieProperties[NSHTTPCookiePath]    = cookie.path
        cookieProperties[NSHTTPCookieVersion] = NSNumber(integer: cookie.version)
        cookieProperties[NSHTTPCookieExpires] = NSDate().dateByAddingTimeInterval(31536000)

        if cookie.value != "deleted"{

            let newCookie = NSHTTPCookie(properties: cookieProperties)
            NSHTTPCookieStorage.sharedHTTPCookieStorage().setCookie(newCookie!)
        }
    }
}

I have set another function to check & print whether the cookies are set properly and return an array of NSHTTPCookie after the registration & login for several purposes and its sets just fine:

func getCookies() -> [NSHTTPCookie]{

    var cookiesArray:[NSHTTPCookie] = []

    let cookieJar:NSHTTPCookieStorage = NSHTTPCookieStorage.sharedHTTPCookieStorage()

    for cookie in cookieJar.cookies!{

        print("name : \(cookie.name)")
        print("value : \(cookie.value)")
        print("expiresDate : \(cookie.expiresDate)")
        print("sessionOnly : \(cookie.sessionOnly)")

        cookiesArray.append(cookie)
    }

    return cookiesArray
}

On devices running on iOS 9 everything is working perfectly fine on both occasions, but on devices running iOS 8 most of the times that the app is getting terminated (SIGKILL) by the system or by the user the cookies that are holding the PHPSESSID_NEW key is deleted from the cookies storage and all server calls are not authenticated and therefore the app is getting an error responses. I know that on previous versions of iOS when the system was killing the app than it deleted cookies also, but in this case it happens also when the user is killing the app. Any ideas why it happens?

0

There are 0 answers