-2

I have been trying to implement both  sign in and Google sign in in a new app that I have been building. But I have been receiving this error. Can someone assist me on this please?

Here is the code for my Login.swift file.

import SwiftUI
import AuthenticationServices
import GoogleSignIn
import GoogleSignInSwift
import Firebase

struct Login: View {
   // @StateObject var loginModel: LoginViewModel = .init()
    @StateObject var loginModel = LoginViewModel()
    
    var body: some View {
        ScrollView(.vertical, showsIndicators: false){
            VStack(alignment: .leading, spacing: 5){
                Image(systemName: "person")
                //.clipShape(.Circle)
                    .resizable()
                    .aspectRatio(contentMode: .fill)
                    .frame(width: 75, height: 75)
                //.font(.system(size: 40))
                    .foregroundColor(.black)
                    .ignoresSafeArea()
                
                (Text("Welcome")
                    .foregroundColor(.black) +
                 Text("\n Login to Continue")
                    .foregroundColor(.secondary)
                )
                .font(.title)
                .fontWeight(.semibold)
                .lineSpacing(10)
                .padding(.top, 20)
                .padding(.trailing, 15)
                
                //Mark Custom Text Field
                CustomTextField(hint: "+16505551234", text: $loginModel.mobileNo)
                    .disabled(loginModel.showOTPField)
                    .opacity(loginModel.showOTPField ? 0.4 : 1)
                    .overlay(alignment: .trailing, content:{
                        Button("Change"){
                            withAnimation(.easeInOut){
                                loginModel.showOTPField = false
                                loginModel.otpCode = ""
                                loginModel.CLIENT_CODE = ""
                            }
                        }
                        .font(.caption)
                        .foregroundColor(.indigo)
                        .opacity(loginModel.showOTPField ? 1 : 0)
                        .padding(.trailing, 15)
                    })
                    .padding(.top, 40)
                
                CustomTextField(hint: "OTP Code", text: $loginModel.otpCode)
                    .disabled(!loginModel.showOTPField)
                    .opacity(!loginModel.showOTPField ? 0.4 : 1)
                    .padding(.top, 20)
                
                Button(action: loginModel.showOTPField ? loginModel.VerifyOTPCode: loginModel.getOTPCode) {
                    HStack(spacing: 15){
                        Text(loginModel.showOTPField ? "Verify Code" : "Get OTP")
                            .fontWeight(.semibold)
                            .contentTransition(.identity)
                        Image(systemName: "line.diagonal.arrow")
                            .font(.title3)
                            .rotationEffect(.init(degrees: 45))
                    }
                    .foregroundColor(.black)
                    .padding(.horizontal, 25)
                    .padding(.vertical)
                    .background{
                        RoundedRectangle(cornerRadius: 10, style: .continuous)
                            .fill(.black.opacity(0.05))
                        
                    }
                    .padding(.top, 10)
                }
                
                Text("(OR)")
                    .foregroundColor(.gray)
                    .frame(maxWidth: .infinity)
                    .padding(.top,10)
                    .padding(.bottom, 20)
                    .padding(.leading, -60)
                    .padding(.horizontal)
                
                HStack (spacing: 8){
                    
                    //Mark Space for Apple Sign in
                    CustomButton()
                        .overlay{
                            //Apple Sign in Button
                            SignInWithAppleButton{ (request) in
                                
                                //requesting parameter from apple login
                                loginModel.nonce = randomNonceString()
                                request.requestedScopes = [.email,.fullName]
                                request.nonce = sha256(loginModel.nonce)
                                
                            } onCompletion: { (result) in
                                switch result{
                                case .success(let user):
                                    print("Success")
                                    guard let credential = user.credential as? ASAuthorizationAppleIDCredential else{
                                        print("Error with Firebase")
                                        return
                                    }
                                    loginModel.appleAuthenticate(credential: credential)
                                case .failure(let error):
                                    print(error.localizedDescription)
                                }
                                
                            }
                            .signInWithAppleButtonStyle(.white)
                            .frame(height: 55)
                            .blendMode(.overlay)
                        }
                        .clipped()
                    
                    //Mark: Custom Google Sign in Button
       //comment from here
                    CustomButton(isGoogle: true)
                        .overlay{
                            if let clientID = FirebaseApp.app()?.options.clientID{
                                GoogleSignInButton{
                                    GIDSignIn.sharedInstance.signIn(with: .init(clientID: clientID ), presenting: UIApplication.shared.rootController()){
                                        user,error in
                                        if let error = error {
                                            print(error.localizedDescription)
                                            return
                                        }
                                        //Login Google user to firebase
                                        if let user
                                        {
                                            loginModel.logGoogleUser(user: user)
                                        }
                                    }
                                }
                                .blendMode(.overlay)
                            }
                        }
                        .clipped()
                    //comment ends here
                }
                .padding(.leading, -60)
                .frame(maxWidth: .infinity)
            }
            .padding(.leading,60)
            .padding(.vertical,15)
            // .blendMode(.overlay)
        }
        .alert(loginModel.errorMessage, isPresented: $loginModel.showError){
            
        }
    }
    
    @ViewBuilder
    func CustomButton(isGoogle: Bool = false)-> some View{
        HStack{
            Group{
                if isGoogle{
                    Image("Google")
                        .resizable()
                        .renderingMode(.template)
                }else{
                    Image(systemName: "applelogo")
                        .resizable()
                }
            }
            .aspectRatio(contentMode: .fit)
            .frame(width: 25, height: 25)
            .frame(height: 45)
            
            Text("\(isGoogle ? "Google" : "Apple") Sign In")
                .font(.callout)
                .lineLimit(1)
        }
        .foregroundColor(.white)
        .padding(.horizontal,15)
        .background{
            RoundedRectangle(cornerRadius: 10, style: .continuous)
                .fill(.black)
        }
       
    }
}
struct Login_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

I wanted to lay over  sign in and also Google sign in next to each other in HSTACK.

Let me know if you want to look at the whole code happy to share as it is already uploaded in the Git but on private at the moment.

  • 3
    Please post one issue per question. Which line is causing the error mentioned in the title? – HangarRash Apr 13 '23 at 05:39
  • Happy to get one answer which is primary for me. I can figure out the second one on my own though.. Thanks for understanding and will follow that in next posts. – Sathish Kumar V Apr 13 '23 at 05:42
  • You will likely have much better luck getting an answer if you post a [mre]. No one can run or try to compile the code you provided as is. – jnpdx Apr 13 '23 at 05:49
  • I have already acknowledged to the earlier comments not sure why you have to reiterate it again and again. I respect your view and will follow that going forward. If you want me to edit even this I am happy to do or even delete the question if needed, thanks! – Sathish Kumar V Apr 13 '23 at 06:00
  • Which line is causing the error mentioned in the title? Note in your `Login_Previews` you should be using `Login()`, not `ContentView()` – workingdog support Ukraine Apr 13 '23 at 06:02
  • Editing the question to reflect the comments here would be great – jnpdx Apr 13 '23 at 06:40
  • @jnpdx I have updated. – Sathish Kumar V Apr 13 '23 at 07:17
  • workingdogsupportUkraine It does not work in that way. I have updated the same still seeing the same issue. Suggestion not worked :( – Sathish Kumar V Apr 13 '23 at 07:17
  • not sure what you are referring to `It does not work in that way`, I was only asking (for the third time) on which line is the error mentioned in the title? – workingdog support Ukraine Apr 13 '23 at 07:25
  • I meant when you advised me to change from content preview to login()that is not working is what I mentioned. The error reported is in CustomButton(isGoogle: true) – Sathish Kumar V Apr 13 '23 at 07:34
  • why not tell us `on which line the error is`, where in `CustomButton` and show the full error message – workingdog support Ukraine Apr 13 '23 at 07:39
  • @jnpdx the function is already in the same code I mentioned. If you go down to the bottom of the code you can see what that custom button function is. I’m not calling this function outside of this login.swift file. Second when I copy pasted my code from Xcode I was under an impression that it will copy the error code but it didn’t I just realised it now. Hope this makes my query clear. Please let me know if you want to see any other file I can share my GitHub thanks! – Sathish Kumar V Apr 13 '23 at 07:45
  • @workingdogsupportUkraine Mark: Custom Google Sign in Button //comment from here CustomButton(isGoogle: true) sorry not in front of my computer but this is where I’m seeing – Sathish Kumar V Apr 13 '23 at 07:47

1 Answers1

-1

I resolved this issue by downgrading the google sign in package to 6.2.4 version. But ended up in having issue which I am working on. To downgrade first remove the package (google how to remove swift package, there are already tons of documents so not going to explain that), once done follow add package this time search for explicit package version and install to resolve the same.