Created
April 19, 2016 20:29
-
-
Save Shehryar/dc0a495575f2e51aca8cf7d93f694e7f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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