我正在尝试在
Swift中编写一些cookie,这样当我显示webview时,它将能够读取这些cookie并做出适当的反应.我找到了很多关于如何创建cookie和阅读Apple文档的例子,但我似乎无法获得有效的NSHTTPCookie对象.总是零.
这是我的代码:
let baseHost = "domain.com" let oneYearInSeconds = NSTimeInterval(60 * 60 * 24 * 365) func setCookie(key: String,value: AnyObject) { var cookieProps = [ NSHTTPCookieOriginURL: baseHost,NSHTTPCookiePath: "/",NSHTTPCookieName: key,NSHTTPCookieValue: value,NSHTTPCookieSecure: "TRUE",NSHTTPCookieExpires: NSDate(timeIntervalSinceNow: oneYearInSeconds) ] var cookie = NSHTTPCookie(properties: cookieProps) // This line fails due to the nil cookie NSHTTPCookieStorage.sharedHTTPCookieStorage().setCookie(cookie!) }
我的cookie变量是零.我尝试了很多属性的组合,包括NSHTTPCookieOriginURL和NSHTTPCookieDomain,有和没有NSHTTPCookieSecure,甚至没有NSHTTPCookieExpires.永远没有.
有没有人有任何想法我做错了什么?