UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How to have an Apple icon on sign-in with the Apple button?

Forums > SwiftUI

I'm using the Apple sign in button with custom frames but the thing is I couldnt add icon on it. Here is the code:

 SignInWithAppleButton(

                        //Request
                        onRequest: { request in
                            let nonce = randomNonceString()
                            currentNonce = nonce
                            request.requestedScopes = [.fullName, .email]
                            request.nonce = sha256(nonce)
                        },

                        //Completion
                        onCompletion: { result in
                            switch result {
                                case .success(let authResults):
                                    switch authResults.credential {
                                        case let appleIDCredential as ASAuthorizationAppleIDCredential:

                                                guard let nonce = currentNonce else {
                                                  fatalError("Invalid state: A login callback was received, but no login request was sent.")
                                                }
                                                guard let appleIDToken = appleIDCredential.identityToken else {
                                                    fatalError("Invalid state: A login callback was received, but no login request was sent.")
                                                }
                                                guard let idTokenString = String(data: appleIDToken, encoding: .utf8) else {
                                                  print("Unable to serialize token string from data: \(appleIDToken.debugDescription)")
                                                  return
                                                }

                                                //Creating a request for firebase
                                                let credential = OAuthProvider.credential(withProviderID: "apple.com",idToken: idTokenString,rawNonce: nonce)

                                                //Sending Request to Firebase
                                                Auth.auth().signIn(with: credential) { (authResult, error) in
                                                    if (error != nil) {
                                                        // Error. If error.code == .MissingOrInvalidNonce, make sure
                                                        // you're sending the SHA256-hashed nonce as a hex string with
                                                        // your request to Apple.
                                                        print(error?.localizedDescription as Any)
                                                        return
                                                    }
                                                    // User is signed in to Firebase with Apple.
                                                    print("you're in")
                                                }

                                            //Prints the current userID for firebase
                                            print("\(String(describing: Auth.auth().currentUser?.uid))")
                                    default:
                                        break

                                            }
                                   default:
                                        break
                                }

                        }
                    ).frame(width: UIScreen.main.bounds.width * 0.18, height: UIScreen.main.bounds.height * 0.08).signInWithAppleButtonStyle(colorScheme == .dark ? .white : .black).cornerRadius(16)

2      

Apple have very strict guidelines with using Sign In With Apple.

You should go to Sign in with Apple Button and download the files that you will need.

2      

@NigelGee I know that Apple has strict guidlines but how will I add the icon in code?

2      

Try using this modiflier .signInWithAppleButtonStyle(.black). Think you need to do a frame as it seem to take up the whole view

struct ContentView: View {
    var body: some View {
        VStack {
            SignInWithAppleButton(.signIn,              //1 .signin, or .continue or .signUp for button label
                  onRequest: { (request) in             //2
                    //Set up request
                  },
                  onCompletion: { (result) in           //3
                    switch result {
                    case .success(let authorization):
                        //Handle authorization
                        break
                    case .failure(let error):
                        //Handle error
                        break
                    }
                  })
                .frame(width: 200, height: 50)
                .signInWithAppleButtonStyle(.black) // .black, .white and .whiteOutline
        }
    }
}

PS you might want to add this as button does not change with light/dark mode

@Environment(\.colorScheme) var colorScheme
// … code
.signInWithAppleButtonStyle(colorScheme == .light ? .black : .white)

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.