12

I need to set a cookie for a webview in swift. I found a solution, but it is for objective-c. How to do it in Swift?

Is it possible to set a cookie manually using sharedHTTPCookieStorage for a UIWebView?

Here is said solution.

Community
  • 1
  • 1
Jacob Smith
  • 803
  • 5
  • 13
  • 23
  • 1
    convert that code in swift –  May 10 '16 at 11:10
  • 1
    I have no knowledge of objective-c. Sometimes I successfully convert objective-c code to swift, but I could not convert the first part: [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways]; – Jacob Smith May 10 '16 at 11:11
  • NSHTTPCookieStorage.sharedHTTPCookieStorage().cookieAcceptPolicy = NSHTTPCookieAcceptPolicyAlways this is the conversion of that part in swift –  May 10 '16 at 11:13
  • var cookies:[NSHTTPCookie] = NSHTTPCookieStorage.sharedHTTPCookieStorage().cookies as [NSHTTPCookie] for cookie:NSHTTPCookie in cookies as [NSHTTPCookie] { if cookie.name as String == "CookieName" { var cookieValue : String = "CookieName=" + cookie.value! as String //if you want to add to your request youRequest.setValue(cookieValue, forHTTPHeaderField: "cookie") } } – Mihriban Minaz May 10 '16 at 11:14
  • I'd use WKWebView and not the to be deprecated UIWebView: *In apps that run in iOS 8 and later, use the WKWebView class instead of using UIWebView. Additionally, consider setting the WKPreferences property javaScriptEnabled to NO if you render files that are not supposed to run JavaScript.* --> https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIWebView_Class/ – OhadM May 10 '16 at 11:29
  • I decided to use WKWebView. However Does the method above still works? – Jacob Smith May 10 '16 at 11:53

1 Answers1

28

You can do something like below using NSHTTPCookie and NSHTTPCookieStorage to set cookie in swift:

let URL = "example.com"
let ExpTime = NSTimeInterval(60 * 60 * 24 * 365)

func setCookie(key: String, value: AnyObject) {
    var cookieProps = [
        NSHTTPCookieDomain: URL,
        NSHTTPCookiePath: "/",
        NSHTTPCookieName: key,
        NSHTTPCookieValue: value,
        NSHTTPCookieSecure: "TRUE",
        NSHTTPCookieExpires: NSDate(timeIntervalSinceNow: ExpTime)
    ]

    var cookie = NSHTTPCookie(properties: cookieProps)

    NSHTTPCookieStorage.sharedHTTPCookieStorage().setCookie(cookie!)
}

Swift 3 :

func setCookie(key: String, value: AnyObject) {
    let cookieProps: [HTTPCookiePropertyKey : Any] = [
        HTTPCookiePropertyKey.domain: URL,
        HTTPCookiePropertyKey.path: "/",
        HTTPCookiePropertyKey.name: key,
        HTTPCookiePropertyKey.value: value,
        HTTPCookiePropertyKey.secure: "TRUE",
        HTTPCookiePropertyKey.expires: NSDate(timeIntervalSinceNow: ExpTime)
    ]

    if let cookie = NSHTTPCookie(properties: cookieProps) {
        NSHTTPCookieStorage.sharedHTTPCookieStorage().setCookie(cookie)
    }
}

set cookieAcceptPolicy as follow:

NSHTTPCookieStorage.sharedHTTPCookieStorage().cookieAcceptPolicy = NSHTTPCookieAcceptPolicy.Always

Swift 3

HTTPCookieStorage.shared.cookieAcceptPolicy = HTTPCookie.AcceptPolicy.always

Note that this was NSHTTPCookieAcceptPolicyAlways in Objective-C and older versions of Swift.

Hope this helps:)

KIDdAe
  • 2,714
  • 2
  • 22
  • 29
Ronak Chaniyara
  • 5,335
  • 3
  • 24
  • 51
  • 2
    Per [Apple's documentation](https://developer.apple.com/reference/foundation/nshttpcookiestorage/1410415-cookieacceptpolicy?language=objc) ,"the default cookie accept policy is always" so if always is what you want there is no need to set it explicitly. – Tony Adams Jan 28 '17 at 21:32