首先,一些细节:我们正在开发一个iOS应用程序,当前使用Swift/Xcode 6.1 GM 2进行开发。

临时分发应用程序时,我们在钥匙串(keychain)访问方面遇到一些令人困惑的问题,并且在追查原因时也遇到了问题。所有供应配置文件都与我们应用程序的捆绑包名称匹配。尽管我认为不是问题,但我们使用TestFlight进行分发。

我们仅设法使其能够在之前未安装该应用的iOS 7设备上运行。没有一个iOS 8设备可以临时使用。我们一开始遇到的错误是25300(errSecItemNotFound),现在重置置备配置文件后,我们得到一个纯0(都保存在加载上,但仍然无法检索到数据)。从Xcode部署开发版本时,一切都可以完美运行。

我已经分离出了我们使用的钥匙串(keychain)包装器的代码:

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
}


}
  • 我们可能错过了什么吗?
  • 解决此问题的最佳方法是什么?在Logs/iPhone配置实用程序中,我们没有从钥匙串(keychain)中收到任何错误。目前,我只是在代码中添加了一些简单的警报,以了解操作的状态。
  • 最佳答案

    供应配置文件和钥匙串(keychain)代码中的所有内容似乎都很好。问题是Swift编译器中的一个设置...将“发布”的优化级别从“最快”更改为“无”,这似乎可以解决问题。

    10-08 05:23
    查看更多