简体   繁体   中英

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

I'm having trouble separating ASAuthorizationControllerDelegate from iOS12 or below. 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.

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 . if #available is used to execute specific code paths on specific iOS versions. To declare a specific type or extension to be available on iOS 13 only, you need @available .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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