首先,一些细节:我们正在开发一个 iOS 应用程序,目前使用 Swift/Xcode 6.1 GM 2 进行开发。
在临时分发应用程序时,我们在钥匙串访问方面遇到了一些令人困惑的问题,并且在追踪原因方面遇到了问题。所有配置文件都与我们应用程序的捆绑包名称匹配。我们使用 TestFlight 进行分发,尽管我认为这不是问题。
我们只能让它在之前没有安装该应用程序的 iOS 7 设备上运行。没有一台 iOS 8 设备可以临时运行。我们一开始遇到的错误是 25300 (errSecItemNotFound),现在重置配置文件后,我们得到一个普通的 0(在加载时保存,但仍然无法检索数据)。从 Xcode 部署开发版本时,一切都运行良好。
我已经分离了我们使用的钥匙串包装器的代码:
import UIKit
import Security
let serviceIdentifier = "com.Test.KeychainTest"
let kSecClassValue = kSecClass as NSString
let kSecAttrAccountValue = kSecAttrAccount as NSString
let kSecValueDataValue = kSecValueData as NSString
let kSecClassGenericPasswordValue = kSecClassGenericPassword as NSString
let kSecAttrServiceValue = kSecAttrService as NSString
let kSecMatchLimitValue = kSecMatchLimit as NSString
let kSecReturnDataValue = kSecReturnData as NSString
let kSecMatchLimitOneValue = kSecMatchLimitOne as NSString
class KeychainManager {
class func setString(value: NSString, forKey: String) {
self.save(serviceIdentifier, key: forKey, data: value)
}
class func stringForKey(key: String) -> NSString? {
var token = self.load(serviceIdentifier, key: key)
return token
}
class func removeItemForKey(key: String) {
self.save(serviceIdentifier, key: key, data: "")
}
class func save(service: NSString, key: String, data: NSString) {
var dataFromString: NSData = data.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
// Instantiate a new default keychain query
var keychainQuery: NSMutableDictionary = NSMutableDictionary(objects: [kSecClassGenericPasswordValue, service, key, dataFromString], forKeys: [kSecClassValue, kSecAttrServiceValue, kSecAttrAccountValue, kSecValueDataValue])
// Delete any existing items
SecItemDelete(keychainQuery as CFDictionaryRef)
if data == "" { return }
// Add the new keychain item
var status: OSStatus = SecItemAdd(keychainQuery as CFDictionaryRef, nil)
var alertView = UIAlertView();
alertView.addButtonWithTitle("Ok");
alertView.title = "Status";
alertView.message = "Saving \(status)";
alertView.show();
}
class func load(service: NSString, key: String) -> NSString? {
// Instantiate a new default keychain query
// Tell the query to return a result
// Limit our results to one item
var keychainQuery: NSMutableDictionary = NSMutableDictionary(objects: [kSecClassGenericPasswordValue, service, key, kCFBooleanTrue, kSecMatchLimitOneValue], forKeys: [kSecClassValue, kSecAttrServiceValue, kSecAttrAccountValue, kSecReturnDataValue, kSecMatchLimitValue])
var dataTypeRef :Unmanaged<AnyObject>?
// Search for the keychain items
let status: OSStatus = SecItemCopyMatching(keychainQuery, &dataTypeRef)
var alertView = UIAlertView();
alertView.addButtonWithTitle("Ok");
alertView.title = "Status";
alertView.message = "Loading \(status)";
alertView.show();
let opaque = dataTypeRef?.toOpaque()
var contentsOfKeychain: NSString?
if let op = opaque? {
let retrievedData = Unmanaged<NSData>.fromOpaque(op).takeUnretainedValue()
// Convert the data retrieved from the keychain into a string
contentsOfKeychain = NSString(data: retrievedData, encoding: NSUTF8StringEncoding)
} else {
return nil
}
return contentsOfKeychain
}
}
- 有什么我们可能错过的吗?
- 解决此问题的最佳方法是什么?我们在日志/iPhone 配置实用程序中没有从钥匙串中收到任何错误。目前,我只是在代码中添加了一些简单的警报,以了解操作的状态。