Advertisement
Guest User

Untitled

a guest
Apr 19th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. //
  2. // SafariKeychainManager.swift
  3. // UltraMotivator
  4. //
  5. // Created by Shehryar Hussain on 04/15/16.
  6. // Copyright (c) 2014 Shehryar Hussain. All rights reserved.
  7. //
  8.  
  9. import Foundation
  10.  
  11. @objc class SafariKeychainManager: NSObject {
  12.  
  13. @objc class func checkSafariCredentialsWithCompletion(completion: ((error: NSError?, username: String?, password: String?) -> Void)) {
  14.  
  15. let domain: CFString = "classpass.com"
  16.  
  17. SecRequestSharedWebCredential(domain, .None, {
  18. (credentials: CFArray?, error: CFError?) -> Void in
  19.  
  20. if let error = error {
  21. print("error: \(error)")
  22. completion(error: error as NSError, username: nil, password: nil)
  23. } else if CFArrayGetCount(credentials) > 0 {
  24.  
  25. let dict = unsafeBitCast(CFArrayGetValueAtIndex(credentials, 0), CFDictionaryRef.self) as NSDictionary
  26. let username = dict[kSecAttrAccount as String] as! String
  27. let password = dict[kSecSharedPassword as String] as! String
  28.  
  29. dispatch_async(dispatch_get_main_queue()) {
  30. completion(error: nil, username: username, password: password)
  31. }
  32. } else {
  33. dispatch_async(dispatch_get_main_queue()) {
  34. completion(error:nil, username: nil, password: nil)
  35. }
  36. }
  37. });
  38. }
  39.  
  40. @objc class func updateSafariCredentials(username: String, password: String) {
  41.  
  42. let domain: CFString = "classpass.com"
  43.  
  44. SecAddSharedWebCredential(domain,
  45. username as CFString,
  46. password.characters.count > 0 ? password as CFString : .None,
  47. {(error: CFError?) -> Void in
  48. print("error: \(error)")
  49. });
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement