首先,我们正在开发一个使用Swift/Xcode 6.1GM 2进行开发的iOS应用程序。
在分发应用程序时,我们遇到了一些令人困惑的密钥链访问问题,并且在查找原因方面也有问题。所有的配置文件都与我们的应用程序的包名相匹配。我们使用TestFlight进行分发,尽管我不认为这是问题所在。
我们只是设法让它在iOS 7设备上工作,而这些设备之前并没有安装应用程序。没有一个iOS 8设备是临时工作的。我们在开始时得到的错误是25300 (errSecItemNotFound),而现在在重置配置文件之后,我们得到了一个普通的0(无论是在加载时保存数据,还是无法检索数据)。当从Xcode部署dev生成时,一切都很完美。
我已经分离了我们使用的密钥链包装器的代码:
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
}
}发布于 2014-10-20 14:24:12
供应配置文件和密钥链代码中的所有内容似乎都很好。问题是Swift编译器中的设置..。将“发布”的优化级别从“最快的”更改为“无”,这似乎解决了问题

发布于 2014-10-17 23:13:25
确保您还为kSecAttrAccessible指定了值。还可以为kSecAttrAccessControl指定值,即在iOS8中添加。
https://stackoverflow.com/questions/26401080
复制相似问题