简体   繁体   English

CFDictionaryGetValue无法使用CFStringRef调用

[英]CFDictionaryGetValue can't invoke with CFStringRef

I am trying to implement the CFDictionaryGetValue() as shown in this appledoc listing 3: 我正在尝试实现CFDictionaryGetValue(), 如此appledoc列表3中所示:

server = CFDictionaryGetValue(credentialDict, kSecAttrServer);
userName = CFDictionaryGetValue(credentialDict, kSecAttrAccount);
password = CFDictionaryGetValue(credentialDict, kSecSharedPassword);

and I implemented like this 我这样实施了

let userName = CFDictionaryGetValue(credentialDict, kSecAttrAccount)
let password = CFDictionaryGetValue(credentialDict, kSecSharedPassword)

but I get the error "Cannot invoke "CFDictionaryGetValue" with argument list of type "(CFDictionaryRef, CFStringRef)" 但我得到错误“无法调用”CFDictionaryGetValue“与类型的参数列表”(CFDictionaryRef,CFStringRef)“

I dont understand how this is different from how the apple doc implemented it. 我不明白这与苹果doc如何实现它有何不同。

thanks 谢谢

Yes there is an issue with that code mentioned in radar here 是的,有在雷达提到代码的问题在这里

I found a workaround for you: 我找到了一个解决方法:

  let unsafeCred = CFArrayGetValueAtIndex(credentials, 0)
  let credential: CFDictionaryRef = unsafeBitCast(unsafeCred, CFDictionaryRef.self)
  let dict: Dictionary<String, String> = credential as! Dictionary<String, String>
  let username = dict[kSecAttrAccount as String]
  let password = dict[kSecSharedPassword.takeRetainedValue() as! String]

UPDATE UPDATE

SecRequestSharedWebCredential("www.reddit.com", .None)  {
  credentials, error in 
    if CFArrayGetCount(credentials) > 0 {
      let dict = unsafeBitCast(
               CFArrayGetValueAtIndex(credentials, 0), 
               CFDictionaryRef.self) as Dictionary
    let username = dict[kSecAttrAccount as String]
    let password = dict[kSecSharedPassword as String]
    login(username, password)
  }
}

Please mind the difference here: 请注意这里的区别:

iOS8 iOS8上

var kSecSharedPassword: Unmanaged<AnyObject>!
func SecRequestSharedWebCredential(_ fqdn: CFString!,
                                   _ account: CFString!,
                                   _ completionHandler: ((CFArray!,
                                            CFError!) -> Void)!)

iOS9 iOS9

let kSecSharedPassword: CFString
func SecRequestSharedWebCredential(_ fqdn: CFString?, 
                                   _ account: CFString?, 
                                   _ completionHandler: (CFArray?, CFError?) -> Void)

Here are all the changes for swift2/iOS9/xcode7 以下是swift2 / iOS9 / xcode7的所有更改

Updated answer for XCode 7.3 and Swift 2.2 更新了XCode 7.3和Swift 2.2的答案

      if CFArrayGetCount(credentials) > 0 {
            let dict = unsafeBitCast(CFArrayGetValueAtIndex(credentials, 0), CFDictionaryRef.self) as NSDictionary
            let username = dict[kSecAttrAccount as String] as! String
            let password = dict[kSecSharedPassword as String] as! String
            dispatch_async(dispatch_get_main_queue()) {
                completion(error: nil, username: username, password: password)
            }

Remember to be careful when returning the values for username and password as it has to be on the main thread 请记住在返回用户名和密码的值时要小心,因为它必须在主线程上

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM