NEW: Learn to build amazing SwiftUI apps for macOS with my new book! >>

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)

1      

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.

1      

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

1      

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)

1      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Spend less time managing in-app purchase infrastructure so you can focus on building your app. RevenueCat gives everything you need to easily implement, manage, and analyze in-app purchases and subscriptions without managing servers or writing backend code.

Get Started

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

Reply to this topic…

You need to create an account or log in to reply.

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.