参考网址:https://www.wangquanwei.com/560.html
配置设置:
1.在 Xcode 里的 Signing & Capabilities 开启 Sign in with Apple 功能
2.添加完 Sign In With Apple
国际化配置(针对 ASAuthorizationAppleIDButton)
本地化中文显示info设置:
核心代码:
登录按钮:建议使用苹果提供的,以免格式不符合,审核被拒
需要导入头文件 AuthenticationServices
import AuthenticationServices
if #available(iOS 13.0, *) {
let apLoginBtn = ASAuthorizationAppleIDButton(authorizationButtonType: .signIn, authorizationButtonStyle: .whiteOutline)
self.view.addSubview(apLoginBtn)
apLoginBtn.addTarget(self, action: #selector(appleLogin), for: .touchUpInside)
apLoginBtn.snp.makeConstraints { (maker) in
maker.bottom.equalTo(otherLoginBtn!.snp.top).offset(-25)
maker.centerX.equalToSuperview()
maker.width.equalTo(300)
maker.height.equalTo(50)
}
apLoginBtn.layoutIfNeeded()
apLoginBtn.cornerRadius = 25
}
代理方法:
//TODO: 苹果账号登录
@available(iOS 13.0, *)
extension HLoginViewController: ASAuthorizationControllerDelegate, ASAuthorizationControllerPresentationContextProviding {
@objc func appleLogin() {
let appleIdProvider = ASAuthorizationAppleIDProvider()
let request = appleIdProvider.createRequest()
request.requestedScopes = [.email, .fullName]
let authVC = ASAuthorizationController(authorizationRequests: [request])
authVC.delegate = self
authVC.presentationContextProvider = self
authVC.performRequests()
}
//ASAuthorizationControllerPresentationContextProviding 主要是告诉 ASAuthorizationController 在哪个 window 上显示。
func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
self.view.window ?? ((UIApplication.shared.delegate?.window)!)!
}
// 授权成功
func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
if let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential {
///将返回得到的user 存储起来
// 苹果用户唯一标识符,该值在同一个开发者账号下的所有 App 下是一样的,开发者可以用该唯一标识符与自己后台系统的账号体系绑定起来。
let userIdentifier = appleIDCredential.user
// 苹果用户信息 如果授权过,可能无法再次获取该信息
let fullName = appleIDCredential.fullName
let name = (fullName?.familyName ?? "") + (fullName?.givenName ?? "")
let email = appleIDCredential.email
// 服务器验证需要使用的参数
// var authorizationCode: String?
// if let code = appleIDCredential.authorizationCode {
// authorizationCode = String(data: code, encoding: .utf8)
// }
var identityToken: String?
if let token = appleIDCredential.identityToken {
identityToken = String(data: token, encoding: .utf8)
}
// 用于判断当前登录的苹果账号是否是一个真实用户,取值有:unsupported、unknown、likelyReal
let realUserStatus = appleIDCredential.realUserStatus
var status = ""
switch realUserStatus {
case .unsupported:
status = "unsupported"
case .unknown:
status = "unknown"
case .likelyReal:
status = "likelyReal"
default:
status = ""
}
requestForAppleLogin(user: userIdentifier, name: name, email: email ?? "", status: status, token: identityToken ?? "")
}
}
// 授权失败
func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
var errorMsg = ""
let err = error as NSError
switch err.code {
case ASAuthorizationError.canceled.rawValue:
errorMsg = "用户取消了授权请求"
case ASAuthorizationError.failed.rawValue:
errorMsg = "授权请求失败"
case ASAuthorizationError.invalidResponse.rawValue:
errorMsg = "授权请求响应无效"
case ASAuthorizationError.notHandled.rawValue:
errorMsg = "未能处理授权请求"
case ASAuthorizationError.unknown.rawValue:
errorMsg = "授权请求失败未知原因"
default:
break
}
self.showToast(text: errorMsg)
}
//调服务器提供的接口将登录信息传过去
func requestForAppleLogin(user: String, name: String, email: String, status: String, token: String) {
let params = ["userIdentifier": user,
"fullName": name,
"email": email,
"realUserStatus": status,
"identityToken": token]
HHttpManager.requestAsynchronous(url: "/v1/user/appleAuth", parameters: params) { [weak self] (result) in
if result.isSuccess {
if let user = HUser.toEntity(data: result.data as Any) {
HUserSession.session.cache(user: user)
HIAPManager.shared.startIAPObserver()
self?.get(babyInfo: { (babyInfo) in
self?.dismissHud()
guard let name = babyInfo?.name, name.count > 0 else {
let profileVC = HLoginProfileViewController()
profileVC.modalPresentationStyle = .fullScreen
self?.navigationController?.present(profileVC, animated: true, completion: {
})
return
}
HXY.appDelegate?.configRootViewController()
}) { (error) in
self?.dismissHud()
HXY.appDelegate?.configRootViewController()
}
} else {
self?.dismissHud()
self?.showToast(text: "用户数据错误")
}
} else {
self?.dismissHud()
self?.showToast(text: result.msg)
}
}
}
}
© 版权声明
特别提醒: 内容为用户自行发布,如有侵权,请联系我们管理员删除,邮箱:mail@xieniao.com ,在收到您的邮件后我们会在3个工作日内处理。
相关文章
暂无评论...