Skip to content

Instantly share code, notes, and snippets.

@barrault01
Last active December 3, 2018 10:39
Show Gist options
  • Select an option

  • Save barrault01/3757a5099839afd765161d785c890443 to your computer and use it in GitHub Desktop.

Select an option

Save barrault01/3757a5099839afd765161d785c890443 to your computer and use it in GitHub Desktop.
An HTTPCookie extension ta arquive a cookie inside a Data
//
// HTTPCookie+Arquiver.swift
//
//
// Created by Antoine Barrault on 17/01/2018.
//
import Foundation
extension HTTPCookie {
fileprivate func save(cookieProperties: [HTTPCookiePropertyKey : Any]) -> Data {
let data = NSKeyedArchiver.archivedData(withRootObject: cookieProperties)
return data
}
static fileprivate func loadCookieProperties(from data: Data) -> [HTTPCookiePropertyKey : Any]? {
let unarchivedDictionary = NSKeyedUnarchiver.unarchiveObject(with: data)
return unarchivedDictionary as? [HTTPCookiePropertyKey : Any]
}
static func loadCookie(using data: Data?) -> HTTPCookie? {
guard let data = data,
let properties = loadCookieProperties(from: data) else {
return nil
}
return HTTPCookie(properties: properties)
}
func arquive() -> Data? {
guard let properties = self.properties else {
return nil
}
return save(cookieProperties: properties)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment