第三方SDK相关

作者: A_rcher34 | 来源:发表于2020-07-07 11:45 被阅读0次

    苹果登录

    适用范围:
    对于专门使用第三方或社交登录服务(如 Facebook、谷歌、 Twitter、Linkedln或亚马逊等)在应用中设置或验证用户主要帐户的应用而言,必须向用户提供“以苹果账号登录”服务的选项。
    如果满足以下条件,则不需要使用“以苹果账号登录”功能(以下4种情况可以不用苹果登录方式):

    应用仅使用开发者公司自己的账号设置和登录系统;
    应用归类为教育、企业或商业应用,要求用户使用现有的教育或企业账号登录;
    应用使用政府或行业支持的公民身份识别系统或电子D对用户进行身份验证;
    应用是特定第三方服务的客户端,用户需要直接登录其电子邮件、社交媒体或其他第三方账号才能访问内容。

    添加apple登录
    1.在苹果开发者网站,打开sign in with apple选项



    注:右边编辑选项为分组功能,如果设置了分组可以使用一下。
    2.在项目工程中添加响应的权限功能设置。点击+Capability,将sign in with apple拖入。想删除掉点击右边的x即可。



    3.代码集成
    3.1导入头文件
    import AuthenticationServices
    

    3.2添加UI

    @available(iOS 13.0, *)
    lazy var appleLoginButton_p_DP: ASAuthorizationAppleIDButton = {
        let btn = ASAuthorizationAppleIDButton.init(authorizationButtonType: .signIn, authorizationButtonStyle: ASAuthorizationAppleIDButton.Style.white)
        btn.frame = CGRect.init(x: 19, y: self.frame.height - 100, width: self.bounds.width - 38, height: 50)
        return btn
    }()
    // 之后在需要的地方分别 addSubview 和 addTarget 
    

    3.3遵循代理

    @available(iOS 13.0, *)
    extension DPLoginViewController: ASAuthorizationControllerDelegate {
        
        /// 处理数据回调
        func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
            if let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential {
                
                let openid = appleIDCredential.user
                let nickName = (appleIDCredential.fullName?.givenName ?? "") + (appleIDCredential.fullName?.familyName ?? "")
                let email = appleIDCredential.email
                
                // 用于后台像苹果服务器验证身份信息
                DispatchQueue.main.async {
                    self.dp_f_signinByThird(openid, nickName, email ?? "", "ai")
                }
            } else if let passwordCredential = authorization.credential as? ASPasswordCredential {
                // Sign in using an existing iCloud Keychain credential.
                let username = passwordCredential.user
                let password = passwordCredential.password
                
                // For the purpose of this demo app, show the password credential as an alert.
                DispatchQueue.main.async {
                    let message = "The app has received your selected credential from the keychain. \n\n Username: \(username)\n Password: \(password)"
                    let alertController = UIAlertController(title: "Keychain Credential Received",
                                                            message: message,
                                                            preferredStyle: .alert)
                    alertController.addAction(UIAlertAction(title: "Dismiss", style: .cancel, handler: nil))
                    self.present(alertController, animated: true, completion: nil)
                }
            }
        }
        
        /// 回调失败
        func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
            DPLog.debug("苹果登录回调失败:\(error)")
        }
        
    }
    
    @available(iOS 13.0, *)
    extension DPLoginViewController: ASAuthorizationControllerPresentationContextProviding {
        
        // 设置上下文,管理视图弹出在哪里
        func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
            return self.view.window!
        }
        
    }
    

    3.4监听苹果登录授权状态
    举例叙述一种监听方案

    // 在首页viewDidLoad注册通知,并主动检查一次状态
    if #available(iOS 13.0, *) {
        NotificationCenter.default.addObserver(self, selector: #selector(handleSignInWithAppleStateChanged), name: ASAuthorizationAppleIDProvider.credentialRevokedNotification, object: nil)
        handleSignInWithAppleStateChanged()
    } else {
        // Fallback on earlier versions
    }
    
    // 接到通知后的执行方法。
    @objc private func handleSignInWithAppleStateChanged() {
        DispatchQueue_main_async_safe {
            // 去检查登录状态
            (UIApplication.shared.delegate as! AppDelegate).appleLoginStatusCheck()
        }
    }
    
    // 检查登录状态的方法。在此例中,是将方法写到了AppDelegate类。
    public func appleLoginStatusCheck() {
        if #available(iOS 13.0, *) {
            let user = DPUserDefaultsTool.dp_f_getUserInfo()
            if let userIdentifier = DPUserDefaultsTool.dp_f_getAppleLoginUserIdentifier() {
                if user?.loginMethod == DPUserInfoBean.LoginMethodType.Apple {
                    let appleIDProvider = ASAuthorizationAppleIDProvider()
                    appleIDProvider.getCredentialState(forUserID: userIdentifier) { (credentialState, error) in
                        switch credentialState {
                        case .authorized:
                            // The Apple ID credential is valid(授权状态有效)
                            break
                        case .revoked:
                            // Apple ID Credential revoked, handle unlink(上次使用苹果账号登录的凭据已被移除,需解除绑定并重新引导用户使用苹果登录)
                            DispatchQueue_main_async_safe {
                                let alert = UIAlertController.init(title: DPTool.dp_f_localizedString("login_byAppleID_revokeTitle"), message: nil, preferredStyle: .alert)
                                alert.addAction(UIAlertAction.init(title: DPTool.dp_f_localizedString("login_byAppleID_keep"), style: .cancel, handler: { (_) in
                                    
                                }))
                                alert.addAction(UIAlertAction.init(title: DPTool.dp_f_localizedString("login_byAppleID_logout"), style: .default, handler: { (_) in
                                    if self.viewcontroller != nil {
                                        DPSettingHomeViewController.dp_f_logout(self.viewcontroller)
                                    }
                                }))
                                self.viewcontroller.present(alert, animated: true, completion: nil)
                            }
                            break
                        case .notFound:
                            // Credential not found, show login UI(未登录授权,直接弹出登录页面,引导用户登录)
                            // 触发情况:登出apple账号
                            break
                        default:
                            break
                        }
                    }
                }
            }
        }
    }
    

    参考文献:
    iOS sign in with Apple 苹果ID登录
    Sign in with Apple 登录详解

    谷歌登录

    Facebook登录

    微信登录

    相关文章

      网友评论

        本文标题:第三方SDK相关

        本文链接:https://www.haomeiwen.com/subject/zfjbtktx.html