Search code examples
iosuibuttonapple-sign-in

Swift - Button's action doesn’t get triggered unless long tapped


I've added a Sign in with Apple button in a UIScrollView, the way Apple's documentation suggest;

let signInWithAppleButton = ASAuthorizationAppleIDButton(type: .default, style: .white)
            
signInWithAppleButton.addTarget(self, action: #selector(loginWithApple(_:)), for: .touchUpInside)
signInWithAppleButton.cornerRadius = 12
            
scrollView.addSubview(signInWithAppleButton)

The thing is, the button only responds to very long presses instead of simple taps. I've tried putting it outside the UIScrollView and it worked there!

Here's my UIScrollView's and button's setup;

fileprivate func setupScrollViewConstraints() {
    scrollView.translatesAutoresizingMaskIntoConstraints = false
    
    NSLayoutConstraint.activate([
        scrollView.topAnchor.constraint(equalTo: self.view.topAnchor),
        scrollView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
        scrollView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
        scrollView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor)
    ])
}

func setupSignInWithAppleButton() -> UIControl? {
    if #available(iOS 13.0, *) {
        let signInWithAppleButton = ASAuthorizationAppleIDButton(type: .default, style: .white)
        
        signInWithAppleButton.addTarget(self, action: #selector(loginWithApple(_:)), for: .touchUpInside)
        signInWithAppleButton.cornerRadius = 12
        
        scrollView.addSubview(signInWithAppleButton)
        
        signInWithAppleButton.translatesAutoresizingMaskIntoConstraints = false
        
        NSLayoutConstraint.activate([
            signInWithAppleButton.topAnchor.constraint(equalTo: accountLabel.bottomAnchor, constant: 8),
            signInWithAppleButton.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor),
            signInWithAppleButton.widthAnchor.constraint(equalToConstant: 210),
            signInWithAppleButton.heightAnchor.constraint(equalToConstant: 45)
        ])
        
        return signInWithAppleButton
    }
    
    return nil
}

Any idea what's breaking it?

EDIT; This is my handler;

@objc private func loginWithApple(_ sender: Any) {
    let appleIDProvider = ASAuthorizationAppleIDProvider()
        
    let request = appleIDProvider.createRequest()
    request.requestedScopes = [.fullName, .email]
        
    let authorizationController = ASAuthorizationController(authorizationRequests: [request])
        
    authorizationController.delegate = self
    authorizationController.presentationContextProvider = self
    authorizationController.performRequests()
}

I have put a breakpoint inside it, but it only gets activated if I long press the button! Just a reminder, the button works as expected if I place it inside the controller's view!


Solution

  • I FINALLY FIGURED IT OUT!!! I had added a tap gesture on my scroll view, so I could dismiss the keyboard when tapped! For some reason it only broke the Apple's sign in button touch events!