这个问题通常是由于没有将委托方法设置为 ViewController 的委托所导致的。在 ViewController 中实现登录委托方法:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let appleIDProvider = ASAuthorizationAppleIDProvider()
let request = appleIDProvider.createRequest()
request.requestedScopes = [.fullName, .email]
let authController = ASAuthorizationController(authorizationRequests: [request])
authController.presentationContextProvider = self
authController.delegate = self
authController.performRequests()
}
}
extension ViewController: ASAuthorizationControllerDelegate {
func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
// 委托被调用,处理登录返回结果
}
func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
// 委托被调用,处理登录错误
}
}
extension ViewController: ASAuthorizationControllerPresentationContextProviding {
func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
return self.view.window!
}
}
然后在您的登录按钮的操作中将 ViewController 实例设置为授权控制器的委托:
// 在您的登录按钮操作中设置委托
let authController = ASAuthorizationController(authorizationRequests: [request])
authController.presentationContextProvider = self
authController.delegate = self
authController.performRequests()
后续在登录成功或失败时,您实现的委托方法 authorizationController(controller:didCompleteWithAuthorization:) 和 authorizationController(controller:didCompleteWithError:) 将会被调用。