Communicating with a REST API and saving API token in iOS's keychain. But the keychain code is throwing a nil error.
KeychainAccess.swift:
public class func passwordForAccount(account: String, service: String = "keyChainDefaultService") -> String? {
let queryAttributes = NSDictionary(objects: [secClassGenericPassword(), service, account, true], forKeys: [secClass(), secAttrService(), secAttrAccount(), secReturnData()])
var retrievedData: NSData?
var extractedData: AnyObject?
let status = SecItemCopyMatching(queryAttributes, &extractedData)
if (status == errSecSuccess) {
retrievedData = extractedData as? NSData
}
let password = NSString(data: retrievedData!, encoding: NSUTF8StringEncoding)
return (password as! String)
}
In the above code, retrievedData is nil. If I do print(status), I get -25300. This function is being called from a view controller:
// check if API token has expired
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let userTokenExpiryDate : String? = KeychainAccess.passwordForAccount("Auth_Token_Expiry", service: "KeyChainService")
let dateFromString : NSDate? = dateFormatter.dateFromString(userTokenExpiryDate!)
let now = NSDate()
I am not sure where I'm going wrong here. Any pointers?
accountorserviceare different when serving? E.g. different lowercase/uppercase letters? - Sulthan