Skip to content

Instantly share code, notes, and snippets.

@Shehryar
Created April 19, 2016 20:29
Show Gist options
  • Save Shehryar/dc0a495575f2e51aca8cf7d93f694e7f to your computer and use it in GitHub Desktop.
Save Shehryar/dc0a495575f2e51aca8cf7d93f694e7f to your computer and use it in GitHub Desktop.
//
// SafariKeychainManager.swift
// UltraMotivator
//
// Created by Shehryar Hussain on 04/15/16.
// Copyright (c) 2014 Shehryar Hussain. All rights reserved.
//
import Foundation
@objc class SafariKeychainManager: NSObject {
@objc class func checkSafariCredentialsWithCompletion(completion: ((error: NSError?, username: String?, password: String?) -> Void)) {
let domain: CFString = "classpass.com"
SecRequestSharedWebCredential(domain, .None, {
(credentials: CFArray?, error: CFError?) -> Void in
if let error = error {
print("error: \(error)")
completion(error: error as NSError, username: nil, password: nil)
} else if CFArrayGetCount(credentials) > 0 {
let dict = unsafeBitCast(CFArrayGetValueAtIndex(credentials, 0), CFDictionaryRef.self) as NSDictionary
let username = dict[kSecAttrAccount as String] as! String
let password = dict[kSecSharedPassword as String] as! String
dispatch_async(dispatch_get_main_queue()) {
completion(error: nil, username: username, password: password)
}
} else {
dispatch_async(dispatch_get_main_queue()) {
completion(error:nil, username: nil, password: nil)
}
}
});
}
@objc class func updateSafariCredentials(username: String, password: String) {
let domain: CFString = "classpass.com"
SecAddSharedWebCredential(domain,
username as CFString,
password.characters.count > 0 ? password as CFString : .None,
{(error: CFError?) -> Void in
print("error: \(error)")
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment