Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by pranoyparimal · Jul 25, 2017 at 06:22 AM · unity 5communicationcallingwebpagewebview

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!()
         }
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

131 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges