NSHTTPCookieStorage - 在app终止(SIGKILL)iOS 8中丢失了cookie

时间:2016-01-25 11:24:43

标签: ios swift xcode7 nshttpcookie nshttpcookiestorage

在我的应用程序注册中,我得到了&从服务器设置cookie以进行用户身份验证,这是我从服务器获取的标头字段:

  

[Content-Type:application / json,Pragma:no-cache,X-Powered-By:   PHP / 5.5.29-1 + deb.sury.org~precision + 1,Set-Cookie:   PHPSESSID_NEW = u3j95qff7100d87pimd56mfc37;到期= 2024年4月12日星期五   格林尼治标准时间10:31:46;最大年龄= 259200000; path = /,服务器:nginx / 1.1.19,   Content-Encoding:gzip,Expires:Thu,1981年11月19日08:52:00 GMT,   缓存控制:无存储,无缓存,必须重新验证,后检查= 0,   pre-check = 0,日期:星期一,25一月2016 10:31:46 GMT,Content-Length:439,   连接:保持活着,X-CDN:Incapsula,Vary:Accept-Encoding,   X-Iinfo:8-3374242-3308240 SNNY RT(1453717901817 3717)q(0 0 0 0)r(5   5)U5]

我尝试了两种使用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!)
        }
    }
}

我已经设置了另一个功能来检查&amp;打印是否正确设置了cookie并在注册后返回NSHTTPCookie数组。登录有几个目的,它的设置很好:

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
}

在iOS 9上运行的设备上,一切都在两种情况下都能正常运行,但是在运行iOS 8的设备上大多数情况下应用程序被系统或用户终止(SIGKILL) PHPSESSID_NEW 密钥已从Cookie存储中删除,并且所有服务器调用均未经过身份验证,因此应用程序收到错误响应。我知道在以前版本的iOS系统杀死应用程序时,它也会删除cookie,但在这种情况下,当用户杀死应用程序时也会发生这种情况。 任何想法为什么会发生?

0 个答案:

没有答案