尝试使用Apple的KeychainItemWrapper“翻译”到Swift

叹了口气,整个下午我一直在努力……这是我的噩梦:

我正在尝试使用Apple制作的KeychainItemWrapper.但我将其Objective-C代码“翻译”为Swift

import Foundation
import Security
class MyKeychainItemWrapper: NSObject {
var keychainItemData: NSMutableDictionary?
var genericPasswordQuery: NSMutableDictionary = NSMutableDictionary()

init(identifier: String, accessGroup: String?) {

    super.init()

    // Begin Keychain search setup. The genericPasswordQuery leverages the special user
    // defined attribute kSecAttrGeneric to distinguish itself between other generic Keychain
    // items which may be included by the same application.
    genericPasswordQuery.setObject(kSecClassGenericPassword, forKey: kSecClass)
    genericPasswordQuery.setObject(identifier, forKey: kSecAttrGeneric)

    // The keychain access group attribute determines if this item can be shared
    // amongst multiple apps whose code signing entitlements contain the same keychain access group.
    println(accessGroup)
    if (!(accessGroup == nil)) {
        genericPasswordQuery.setObject(accessGroup!, forKey: kSecAttrAccessGroup)
    }

    // Use the proper search constants, return only the attributes of the first match.
    genericPasswordQuery.setObject(kSecMatchLimitOne, forKey: kSecMatchLimit)
    genericPasswordQuery.setObject(kCFBooleanTrue, forKey: kSecReturnAttributes)

    var tempQuery: NSDictionary = NSDictionary(dictionary: genericPasswordQuery)

    var outDictionary: Unmanaged<AnyObject>? = nil

    var status: OSStatus = SecItemCopyMatching(tempQuery as CFDictionaryRef, &outDictionary)
    println(status == noErr)

    if (status == noErr) {
        // Stick these default values into keychain item if nothing found.
        resetKeychainItem()

        // Add the generic attribute and the keychain access group.
        keychainItemData!.setObject(identifier, forKey: kSecAttrGeneric)

        if (!(accessGroup == nil)) {
            keychainItemData!.setObject(accessGroup!, forKey: kSecAttrAccessGroup)
        }
    } else {
        // load the saved data from Keychain.
        keychainItemData = secItemFormatToDictionary(outDictionary?.takeRetainedValue() as NSDictionary)
    }
}

然后在我的应用程序的AppDelegate.swift中,我试图通过以下方式使用它:

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var passwordItem: MyKeychainItemWrapper = MyKeychainItemWrapper(identifier: "Password", accessGroup: nil)
...

因此,初始化程序被调用,但但不知何故,我总是,总是得到

Thread 1: EXC_BREAKPOINT (code=EXC_ARM_BREAKPOINT, subcode=0xe7ffdefe)

我已经尝试评论问题行,然后我在另一个if()得到这个错误:

我甚至尝试过:

var mmm: Bool = (accessGroup == nil)
if (!mmm) {
  genericPasswordQuery.setObject(accessGroup!, forKey: kSecAttrAccessGroup)
}

但是在同一个地方出现同样的错误,即if(..)

我现在很困惑.我在这里错过了什么吗?

环境:Xcode6-beta6,iOS 8 beta 5在非越狱iPhone 5上.

斯威夫特3

import UIKit
import Security

let kSecClassGenericPasswordValue = String(format: kSecClassGenericPassword as String)
let kSecClassValue = String(format: kSecClass as String)
let kSecAttrServiceValue = String(format: kSecAttrService as String)
let kSecValueDataValue = String(format: kSecValueData as String)
let kSecMatchLimitValue = String(format: kSecMatchLimit as String)
let kSecReturnDataValue = String(format: kSecReturnData as String)
let kSecMatchLimitOneValue = String(format: kSecMatchLimitOne as String)
let kSecAttrAccountValue = String(format: kSecAttrAccount as String)

struct KeychainAccess {

    func setPasscode(identifier: String, passcode: String) {
        if let dataFromString = passcode.data(using: String.Encoding.utf8) {
            let keychainQuery = [
                kSecClassValue: kSecClassGenericPasswordValue,
                kSecAttrServiceValue: identifier,
                kSecValueDataValue: dataFromString
            ] as CFDictionary
            SecItemDelete(keychainQuery)
            print(SecItemAdd(keychainQuery, nil))
        }
    }

    func getPasscode(identifier: String) -> String? {
        let keychainQuery = [
            kSecClassValue: kSecClassGenericPasswordValue,
            kSecAttrServiceValue: identifier,
            kSecReturnDataValue: kCFBooleanTrue,
            kSecMatchLimitValue: kSecMatchLimitOneValue
        ] as  CFDictionary
        var dataTypeRef: AnyObject?
        let status: OSStatus = SecItemCopyMatching(keychainQuery, &dataTypeRef)
        var passcode: String?
        if (status == errSecSuccess) {
            if let retrievedData = dataTypeRef as? Data,
                let result = String(data: retrievedData, encoding: String.Encoding.utf8) {
                passcode = result as String
            }
        }
        else {
            print("Nothing was retrieved from the keychain. Status code \(status)")
        }
        return passcode
    }
}

斯威夫特2

import UIKit;
import Security;


let kSecClassGenericPasswordValue = NSString(format: kSecClassGenericPassword);
let kSecClassValue = NSString(format: kSecClass);
let kSecAttrServiceValue = NSString(format: kSecAttrService);
let kSecValueDataValue = NSString(format: kSecValueData);
let kSecMatchLimitValue = NSString(format: kSecMatchLimit);
let kSecReturnDataValue = NSString(format: kSecReturnData);
let kSecMatchLimitOneValue = NSString(format: kSecMatchLimitOne);
let kSecAttrAccountValue = NSString(format: kSecAttrAccount);


class KeychainAccess: NSObject {

func setPasscode(identifier: String, passcode: String) {
    let dataFromString: NSData = passcode.dataUsingEncoding(NSUTF8StringEncoding)!;
    let keychainQuery = NSDictionary(
    objects: [kSecClassGenericPasswordValue, identifier, dataFromString],
    forKeys: [kSecClassValue, kSecAttrServiceValue, kSecValueDataValue]);
    SecItemDelete(keychainQuery as CFDictionaryRef);
    let status: OSStatus = SecItemAdd(keychainQuery as CFDictionaryRef, nil);
}


func getPasscode(identifier: String) -> NSString? {
    let keychainQuery = NSDictionary(
    objects: [kSecClassGenericPasswordValue, identifier, kCFBooleanTrue, kSecMatchLimitOneValue],
    forKeys: [kSecClassValue, kSecAttrServiceValue, kSecReturnDataValue, kSecMatchLimitValue]);
    var dataTypeRef: AnyObject?
    let status: OSStatus = SecItemCopyMatching(keychainQuery, &dataTypeRef)
    var passcode: NSString?;
    if (status == errSecSuccess) {
        let retrievedData: NSData? = dataTypeRef as? NSData
        if let result = NSString(data: retrievedData!, encoding: NSUTF8StringEncoding) {
            passcode = result as String
        }
    }
    else {
        print("Nothing was retrieved from the keychain. Status code \(status)")
    }
    return passcode;
   }
}

然后从任何地方简单地调用

func setPasscode(passcode: String) {
    let keychainAccess = KeychainAccess();
    keychainAccess.setPasscode("YourAppIdentifier", passcode:passcode);
}


func getPasscode() -> NSString {
    let keychainAccess = KeychainAccess();
    return keychainAccess.getPasscode("YourAppIdentifier")!;
}


func deletePasscode() {
    let keychainAccess = KeychainAccess();
    keychainAccess.setPasscode("YourAppIdentifier", passcode:"");
}
相关文章
相关标签/搜索