使用Swift查询iOS钥匙串

前端之家收集整理的这篇文章主要介绍了使用Swift查询iOS钥匙串前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我被卡通转换钥匙串查询结果使用Swift。

我的要求似乎在工作:

let queryAttributes = NSDictionary(objects: [kSecClassGenericPassword,"MyService","MyAccount",true],forKeys: [kSecClass,kSecAttrService,kSecAttrAccount,kSecReturnData])


dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT,0),{
    var dataTypeRef : Unmanaged<AnyObject>?
    let status = SecItemCopyMatching(queryAttributes,&dataTypeRef);

    let retrievedData : NSData = dataTypeRef!.takeRetainedValue() as NSData
    *** ^^^^can't compile this line^^^^
})

我的问题是,代码不会编译:

Bitcast requires both operands to be pointer or neither
  %114 = bitcast %objc_object* %113 to %PSs9AnyObject_,!dbg !487
Bitcast requires both operands to be pointer or neither
  %115 = bitcast %PSs9AnyObject_ %114 to i8*,!dbg !487
LLVM ERROR: Broken function found,compilation aborted!

我不知道如何转换非托管的< AnyObject>到NSData。

有任何想法吗?

使用withUnsafeMutablePointer函数和UnsafeMutablePointer结构来检索数据,如下所示:
var result: AnyObject?
var status = withUnsafeMutablePointer(&result) { SecItemCopyMatching(queryAttributes,UnsafeMutablePointer($0)) }

if status == errSecSuccess {
    if let data = result as NSData? {
        if let string = NSString(data: data,encoding: NSUTF8StringEncoding) {
            // ...
        }
    }
}

它发布(Fastest [-O])构建工作正常。

原文链接:https://www.f2er.com/swift/320334.html

猜你在找的Swift相关文章