dhaval123
dhaval123

Reputation: 31

Sign in with Apple in realm SwiftUI exception: unrecognized selector sent to instance

I am trying to implement sign in with apple to Realm. I already did the configuration from the docs: https://www.mongodb.com/docs/atlas/app-services/authentication/apple/#std-label-apple-id-authentication

Now I am trying to add the sign in with Apple button and the flow.

The login button on SwiftUI:

SignInWithAppleButton(.signIn, onRequest: { request in
    isLoggingIn = true
    request.requestedScopes = [.email]
}, onCompletion: { result in
    switch result {
    case .success(let authResults):
        guard let credentials = authResults.credential as? ASAuthorizationAppleIDCredential,
              let identityToken = credentials.identityToken,
              let identityTokenString = String(data: identityToken, encoding: .utf8) else { return }
        
        repo.loginwithApple(appleTokenIDString: identityTokenString) { user, error in
            
        }
    case .failure(let error):
        isLoggingIn = false
    }
})

repo.loginwithApple calls the method from the shared kotlin repository:

suspend fun loginwithApple(appleTokenIDString: String): User {
    var credentials = Credentials.apple(idToken = appleTokenIDString)

    return appService.login(credentials = credentials)
}

Whenever I try to do the login with the apple sign in button I get this exception:

libc++abi: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SharedRealmRepo loginwithAppleAppleTokenIDString:completionHandler:]: unrecognized selector sent to instance 0x281fe4960'
terminating with uncaught exception of type NSException

Any idea why the exception happens?

Upvotes: 0

Views: 141

Answers (2)

user20810748
user20810748

Reputation:

It looks like removing the Output Files from XCode will solve the issue, but I do not know why.

enter image description here

Maybe someone has a explanation of why this happens. The output files will not let the realm repo re-build?

Upvotes: 0

Diego Freniche
Diego Freniche

Reputation: 5414

The error unrecognized selector sent to instance means that the object repo does not have that method, that is, loginwithApple, with that signature, is not in your Kotlin code. This happens most probably because:

  • the method is not there at all (doesn't seem to be your problem here, but always double check, save all Kotlin files, regenerate the library and copy it again in your iOS project to be 100% sure you have a fresh copy). You can always check is a symbol is in a library using strings from the command line.
  • the method is there, but parameters are different. In this case Swift expects loginwithApple to receive both a String and a closure, and this closure to take user, error. In your Kotlin code I don't see that closure, although my Kotlin is not good.
  • method is there, parameters match, but is not public

I'd say is #2

Upvotes: 2

Related Questions