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      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.