简体   繁体   English

如何实现苹果登录到iOS12兼容项目

[英]How to implement sign in with apple to iOS12 compatible project

I tried to implement Sign in With Apple to my iOS project that supports iOS12.我尝试在支持 iOS12 的 iOS 项目中实现 Sign in With Apple。 Since AuthenticationServices related declarations are only valid from iOS13, i need to use #available(iOS 13.0, *)... to separate code for iOS13 or above and iOS12 and blow.由于 AuthenticationServices 相关声明仅从 iOS13 有效,我需要使用#available(iOS 13.0, *)...来分隔 iOS13 或更高版本和 iOS12 的代码并吹。

I'm having trouble separating ASAuthorizationControllerDelegate from iOS12 or below.我无法将 ASAuthorizationControllerDelegate 与 iOS12 或更低版本分开。 It seems #available(iOS 13.0, *)... can only be used within methods but not directly on to swift file so I cannot conform my LoginViewController to ASAuthorizationControllerDelegate only for iOS13 or above.似乎#available(iOS 13.0, *)...只能在方法中使用,但不能直接用于 swift 文件,所以我不能将我的 LoginViewController 与 ASAuthorizationControllerDelegate 仅用于 iOS13 或更高版本。

Any help?有什么帮助吗?

extension LoginViewController {
    
    @objc func didTapAppleButton() {
        
        guard #available(iOS 13.0, *) else { return }
        
        let provider: ASAuthorizationAppleIDProvider = .init()
        let request = provider.createRequest()
        request.requestedScopes = [.fullName, .email]
        
        let authController: ASAuthorizationController = .init(authorizationRequests: [request])
        authController.delegate = self
        authController.presentationContextProvider = self
        
        authController.performRequests()
    }
}

// This does not work.
// -> Error: Declaration is only valid at file scope
if #available(iOS 13.0, *) {
    
    extension LoginViewController: ASAuthorizationControllerDelegate {
        
        public func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
            
        }
        
        public func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
            
        }
    }

}

What you need is @available , not if #available .您需要的是@available ,而不是if #available if #available is used to execute specific code paths on specific iOS versions. if #available用于在特定 iOS 版本上执行特定代码路径。 To declare a specific type or extension to be available on iOS 13 only, you need @available .要声明仅在 iOS 13 上可用的特定类型或扩展,您需要@available

@available(iOS 13.0, *)
extension LoginViewController: ASAuthorizationControllerDelegate {
    
    public func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
        
    }
    
    public func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
        
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM