3

很长一段时间以来,我一直在从事 iOS 自动填充凭据扩展工作。我查看了很多 iOS 文章和视频。但是我无法在快速类型栏上显示凭据,重置的东西已成功集成。任何人都可以给我快速帮助吗?

使用从苹果共享的此视频和 url: https ://developer.apple.com/videos/play/wwdc2018/721 https://developer.apple.com/documentation/authenticationservices
我正在使用以下代码将凭据保存到特定域的钥匙串。

    let keychain = Keychain(server: "instagram.com", protocolType: .https, authenticationType: .htmlForm)
    keychain["emailAddress"] = "Password"

并使用此代码保存域:

func savedomain(domain: String, account: String, password: String, completion: ((Bool, SharedWebCredentialsManagerError?) -> Void)? = nil) {
    SecAddSharedWebCredential(domain as CFString, account as CFString, password as CFString?) { error in
        guard let error = error else {
            completion?(true, nil)
            return
        }
        let errorDescription = CFErrorCopyDescription(error) as String
        let saveFailedError = SharedWebCredentialsManagerError.saveFailed(errorDescription)
        completion?(false, saveFailedError)
    }
}

我创建了自动填充扩展并获取保存的凭据,但无法在 safari for instagram.com 的快速类型栏上显示凭据

4

2 回答 2

2

我已经为所有社交网站实现了自动填充扩展,共享我的源代码以使用域保存 emailID-Password。

class func save(key: String, data: Data) -> OSStatus {
    let query = [
        kSecClass as String       : kSecClassGenericPassword as String,
        kSecAttrAccount as String : key,
        kSecValueData as String   : data ] as [String : Any]

    SecItemDelete(query as CFDictionary)

    return SecItemAdd(query as CFDictionary, nil)
}

在我在自定义KeyChainManager 类中编写的“保存”功能上方,我还在KeyChainManager 类中添加了以下代码,如下所示。

extension Data {

init<T>(from value: T) {
    var value = value
    self.init(buffer: UnsafeBufferPointer(start: &value, count: 1))
}

func to<T>(type: T.Type) -> T {
    return self.withUnsafeBytes { $0.load(as: T.self) }
}

}

我通过调用我们的KeyChainManager 类从 VC 保存我的数据,如下所示:

let email = (txtEmail?.text ?? "").trimmingCharacters(in: .whitespacesAndNewlines)
        let password = (txtPassword?.text ?? "").trimmingCharacters(in: .whitespacesAndNewlines)
        let domain = (txtDomain?.text ?? "").lowercased().trimmingCharacters(in: .whitespacesAndNewlines)
        
        let user = User(email: email, password: password, key: self.key, domain: domain, identifier: self.keyIdentifier)
        let data = (try? JSONEncoder().encode(user)) ?? Data()
        let _ = KeyChain.save(key: "\(self.keyIdentifier)", data: data)

这一切都是为了保存我们的凭据,现在重点是如何在我们扩展的CredentialProviderViewController.swift中列出所有保存的凭据。

为此,我在KeyChainManager 类中添加了以下方法:

class func load(key: String) -> Data? {
    
    let query = [
        kSecClass as String       : kSecClassGenericPassword,
        kSecAttrAccount as String : key,
        kSecReturnData as String  : kCFBooleanTrue!,
        kSecMatchLimit as String  : kSecMatchLimitOne ] as [String : Any]

    var dataTypeRef: AnyObject? = nil

    let status: OSStatus = SecItemCopyMatching(query as CFDictionary, &dataTypeRef)

    if status == noErr {
        return dataTypeRef as! Data?
    } else {
        return nil
    }
}

并从扩展的CredentialProviderViewController.swift调用这个函数,如下所示:

users.removeAll()
    for i in 0..<1000000 {
        if let encodedData = KeyChain.load(key: "\(i)")  {
            let user = try! JSONDecoder().decode(User.self, from: encodedData)
            if user.key == key && ((serviceIdentifier.contains(user.domain ?? "")) ) {
                users.append(user)
            }
        }else{
            break
        }
    }

我希望这个内容对你有帮助,因为我花了很多天来创建一个演示 :) :) 在下面评论,如果它对你有帮助 :) :)

谢谢,安贾利。

于 2021-02-25T08:12:53.577 回答
0

您需要填充 ASCredentialIdentityStore 以使快速输入栏工作。参见ASCredentialProviderViewController的描述:

可以选择将 ASPasswordCredentialIdentity 实例添加到共享的 ASCredentialIdentityStore 以使身份直接在 QuickType 栏中可用。

您参考的 WWDC 演示文稿中也对此进行了描述。

于 2020-04-20T19:50:50.110 回答