- Home /
How do we use AddJavaScriptInterface of WebView in any game for iOS & Android to call a function at the Unity end from the web page end?
There is a WebPage hosted in a server. This page contains several buttons in it. I am opening this webpage within Unity in Android & iOS using UniWebView plugin. When this webpage is opened within UniWebView's webview, several buttons are displayed within the page. I want, when a user clicks a button in the web page, a function will get executed at the game end at the mobile end.
Moreover, I have seen a project in XCode, where UIWebView is being used. While loading the page in the webview, an object of a class is being sent with a key named "Native". The name of the class is JSInterface, which implements a JSExport type of interface. This JSInterface class implements all the functions of the JSExport interface class. When the user clicks a particular button within the web page, one of the functions in the JSInterface class gets called. This way a function at the native iOS end gets fired from within the web page.
Can anyone suggest me, how we can do the same in Unity?
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var baseWebView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let loadRequest = "https://staging.officialgeorgestraitcasino.com/event_handler/index.html"//TEST_URL//
let request = URLRequest(url: URL(string: loadRequest)!) as URLRequest
baseWebView.loadRequest(request)
let jsInterface = JSInterface()
baseWebView.addJavascriptInterface(object: jsInterface, forKey: "Native")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
import Foundation
import JavaScriptCore
import UIKit
@objc protocol MyExport : JSExport
{
// --------- Testing index.html -----------
func check(_ message : String)
func sayGreeting(_ message: String, _ name: String)
func sayGreeting()
func anotherSayGreeting(_ message: String, name: String)
func showDialog(_ title: String, _ message : String)
// ----------------------------------------
// func onLogin_voucher_ButtonClick(_ pageName: String)
func onLogin_voucher_ButtonClick(_ pageName: String,_ back: String)
func onClassicWebsiteClick()
func onLogoutSuccess()
func onSessionInvalid()
}
class JSInterface : NSObject, MyExport
{
var loginOrVoucherClicked: ((_ page: String, _ successOrBack: String) ->())?
var classicWebsiteClicked: (() ->())?
var logoutSuccess: (() ->())?
var sessionInvalid: (() ->())?
// --------- Testing index.html -----------
func check(_ message: String) {
debugLog("JS Interface works!")
}
func sayGreeting(_ message: String, _ name: String)
{
debugLog("sayGreeting: \(message): \(name)")
}
func sayGreeting()
{
debugLog("sayGreeting")
}
func anotherSayGreeting(_ message: String, name: String)
{
debugLog("anotherSayGreeting: \(message): \(name)")
}
func showDialog(_ title: String, _ message : String)
{
DispatchQueue.main.async {
UIAlertView(title: title, message: message, delegate: nil, cancelButtonTitle: "OK").show()
}
}
// ----------------------------------------
// func onLogin_voucher_ButtonClick(_ pageName: String){
// if loginOrVoucherClicked != nil {
// loginOrVoucherClicked!(pageName)
// }
// }
func onLogin_voucher_ButtonClick(_ pageName: String,_ back: String) {
debugLog("\(pageName)::::\(back)")
if loginOrVoucherClicked != nil {
loginOrVoucherClicked!(pageName, back)
}
}
func onClassicWebsiteClick(){
if classicWebsiteClicked != nil {
classicWebsiteClicked!()
}
}
func onLogoutSuccess(){
if logoutSuccess != nil {
logoutSuccess!()
}
}
func onSessionInvalid() {
if sessionInvalid != nil {
sessionInvalid!()
}
}
}
Your answer
Follow this Question
Related Questions
How to open a webview in unity 5 UI, compiling for android? 0 Answers
Connecting an html web page with unity web player 0 Answers
how can i recieve response from unity web view in unity c# Code 0 Answers
Questions about the fixed frame rate of Unity 1 Answer
ExternalCall in WebGL not working 0 Answers