1
votes

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

1

1 Answers

1
votes

Try this code. This may help you. This happens due because NSHTTPCookie(properties: cookieProperties) is returns nil as newCookie, and you are trying to set nil in cookieStorage.

   if let newCookie = NSHTTPCookie(properties: cookieProperties){
        cookieStorage.setCookie(newCookie!)
    }
    let newCookieVersion = NSHTTPCookie(properties: cookiePropertiesVersion)