【问题标题】:How to Sign Out of Apple After Being Authenticated通过身份验证后如何退出Apple
【发布时间】:2020-03-09 04:42:01
【问题描述】:

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

我尝试了以下但没有成功

    let request = ASAuthorizationAppleIDProvider().createRequest()

    request.requestedOperation = .operationLogout

    let authorizationController = ASAuthorizationController(authorizationRequests: [request])

    authorizationController.performRequests()

【问题讨论】:

    标签: ios swift apple-sign-in


    【解决方案1】:

    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
            }
        }
    

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

    【讨论】:

    • 上面的代码只是为了检查当前的状态,请您建议按照问题中的要求注销用户。
    • @MohitG。如前所述; Apple 没有可用于“使用 Apple 登录”的退出功能。目前,用户必须进入设置应用程序并撤销权限。如果应用程序检测到撤销状态;清除本地存储的任何本地 OAuth 或其他身份验证数据并要求重新身份验证。您还可以将本地存储的令牌清除到您的服务器,然后无论“使用 Apple 登录”状态如何都需要重新身份验证。
    • 这也让我感到困惑,所以在移动应用程序中,如果我使用 Apple 登录并且本质上想要“注销”我的应用程序,那只不过是重定向回登录页面(除了我需要为我自己的应用程序的注销过程执行的逻辑)?
    • @Reza 现在这是正确的。当您注销使用 Sign in with Apple 的应用程序时,他们只会删除其本地令牌以访问其服务器并再次向您显示登录页面(即使您在技术上仍通过设置通过 Apple 登录)。
    • 如果他们不打算这样做,那么当用户尝试重新授权时,他们需要提供姓名/电子邮件凭据。有一个非常常见的边缘情况,用户删除帐户,而企业在帐户删除后不归档用户数据。该应用程序要么必须告诉用户撤销权限,要么设置自定义流程来收集电子邮件和姓名,这违背了此功能的大部分目的。
    【解决方案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
        }
    

    【讨论】:

    • 它给了我以下错误 1. 类型 'KeychainItem' 没有成员 'keychainQuery' 2. 在范围内找不到 'KeychainError' 告诉我如何解决这两个错误
    • 我添加了 keychainQuery。
    猜你喜欢
    • 1970-01-01
    • 2015-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多