here is my code
let cookieProperties = [
NSHTTPCookieOriginURL: Constants.baseUrl,
NSHTTPCookiePath: "/",
NSHTTPCookieName: "device_id",
NSHTTPCookieValue: Constants.deviceId
]
let cookiePropertiesVersion = [
NSHTTPCookieOriginURL: Constants.baseUrl,
NSHTTPCookiePath: "/",
NSHTTPCookieName: "app_version_code",
NSHTTPCookieValue: "50"
]
let newCookie = NSHTTPCookie(properties: cookieProperties)
let newCookieVersion = NSHTTPCookie(properties: cookiePropertiesVersion)
cookieStorage.setCookie(newCookie!)
cookieStorage.setCookie(newCookieVersion!)
Error is at line
cookieStorage.setCookie(newCookie!)
newCookie is nil and
unexpectedly found nil while unwrapping an Optional value
error comes
Try this code. This may help you. This happens due because
NSHTTPCookie(properties: cookieProperties)
is returns nil asnewCookie
, and you are trying to setnil
incookieStorage
.