16

我的应用程序具有“使用 Apple 登录”帐户功能。我想知道Apple帐户是否有退出功能。

我尝试了以下但没有成功

    let request = ASAuthorizationAppleIDProvider().createRequest()

    request.requestedOperation = .operationLogout

    let authorizationController = ASAuthorizationController(authorizationRequests: [request])

    authorizationController.performRequests()
4

2 回答 2

12

Apple 目前仅允许用户执行注销 (iOS/watchOS/tvOS) 或显示为对我们的权限撤销。他们建议您在使用之前获取凭据的状态以检查撤销,如果发生这种情况,请删除任何本地信息(删除您存储的用户标识符)(如果需要,可能更改 UI;例如显示登录看法)。

        let appleIDProvider = ASAuthorizationAppleIDProvider()
    appleIDProvider.getCredentialState(forUserID: KeychainItem.currentUserIdentifier) { (credentialState, error) in
        switch credentialState {
        case .authorized:
            // The Apple ID credential is valid.
            break
        case .revoked:
            // The Apple ID credential is revoked.
            break
        case .notFound:
            // No credential was found, so show the sign-in UI.
            break
        default:
            break
        }
    }

您可以在注销时向用户提供提示,指导他们撤销设备设置并听取更改通知。

于 2019-11-13T16:40:11.697 回答
2

您需要从钥匙串中删除现有项目。

这是我的示例代码,使用 Apple 示例代码。
您可以从Apple获取示例代码

Apple 建议您在使用前获取凭据的状态以检查是否撤销,如果发生这种情况,请删除任何本地信息

struct KeychainItem {

    
    init(service: String, account: String, accessGroup: String? = nil) {
        self.service = service
        self.account = account
        self.accessGroup = accessGroup
    }

    static func deleteUserIdentifierFromKeychain() {
        do { //please change service id to your bundle ID 
            try KeychainItem(service: "com.example.apple-samplecode", account: "userIdentifier").deleteItem()
        } catch {
            print("Unable to delete userIdentifier from keychain")
        }
    }

   func deleteItem() throws {
        // Delete the existing item from the keychain.
        let query = KeychainItem.keychainQuery(withService: service, account: account, accessGroup: accessGroup)
        let status = SecItemDelete(query as CFDictionary)
        
        // Throw an error if an unexpected status was returned.
        guard status == noErr || status == errSecItemNotFound else { throw KeychainError.unhandledError }
    }

钥匙串查询

keychainQuery 来自苹果示例代码。

    
    private static func keychainQuery(withService service: String, account: String? = nil, accessGroup: String? = nil) -> [String: AnyObject] {
        var query = [String: AnyObject]()
        query[kSecClass as String] = kSecClassGenericPassword
        query[kSecAttrService as String] = service as AnyObject?
        
        if let account = account {
            query[kSecAttrAccount as String] = account as AnyObject?
        }
        
        if let accessGroup = accessGroup {
            query[kSecAttrAccessGroup as String] = accessGroup as AnyObject?
        }
        
        return query
    }
于 2021-07-27T04:33:44.080 回答