Tanguy Epifanic
Tanguy Epifanic

Reputation: 225

Kotlin - Uncaught TypeError: X is not a function - JavascriptInterface with WebViews

I have a back application with a cshtml file that is basically used to get authentified.

Now, in my Android app in Kotlin, I'm using a WebView and a JavascriptInterface to call the URL that I need and to call my method in the back.

There are the useful code lines:

Login activity:

val myWebView: WebView = findViewById(R.id.connexionWebview)
        myWebView.setWebViewClient(object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
                view.loadUrl(url)
                return false
            }
        })
        myWebView.settings.javaScriptEnabled = true
        myWebView.addJavascriptInterface(AuthenticatedHandler(this), "AndroidAuthenticatedManager")
        myWebView.loadUrl("HiddenURL")

AuthenticatedHandler.kt:

class AuthenticatedHandler(private val mContext: Context): AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        @JavascriptInterface
        fun onAuthenticated(token: String, email: String, userId: Int) {
            val intent = Intent(this, Home::class.java)
            val extras = Bundle()
            extras.putString("token", token)
            startActivity(intent)
        }
    }
}`

In my back, Authenticated.cshtml:

AndroidAuthenticatedManager.onAuthenticated('@token.AccessToken', '@email', @userId);

And this is the error that I got:

I/chromium: [INFO:CONSOLE(14)] "Uncaught TypeError: AndroidAuthenticatedManager.onAuthenticated is not a function", source: http://HidenURL:5000/Account/Authenticated (14)

What do you think about it? I tried several little changes and searched about it on internet but nothing fixes it...

Thanks for your reading, Tanguy.

Upvotes: 0

Views: 1002

Answers (1)

Klemens Zleptnig
Klemens Zleptnig

Reputation: 1824

I think @JavascriptInterface fun onAuthenticated() should be outside of override fun onCreate(), not inside of it.

Upvotes: 1

Related Questions