Skip to content

ThirteeNov

My personal blog about coding and internet

Menu
  • About me
  • About Zofia Kreasi
  • Cart
  • Checkout
  • Making an airplane game from scratch in Unity
  • My account
  • Privacy Policy
  • Privacy Policy – zkLeaderboard
  • Sample Page
  • Shop
  • Tutorials on Learning JavaScript
  • ZKAccounts – Privacy Policy
Menu

Calling Swift function from JavaScript and JS function from Swift

Posted on December 23, 2017August 4, 2020 by Habibie

When we develop a web app for iOS devices, we need to know how to call JavaScript function from Swift and also how to call Swift function from JavaScript.

Here let me bring you full codes for ViewController.swift and the html file that I loaded it locally.

ViewController.swift

//
//  ViewController.swift
//  Kitten Faces
//
//  Created by Macbook Pro on 12/22/17.
//  Copyright © 2017 Zofia Kreasi. All rights reserved.
//

import UIKit

class ViewController: UIViewController, UIWebViewDelegate {

    @IBOutlet weak var webView: UIWebView!
    
    @IBOutlet weak var button: UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.webView.delegate = self
        self.loadHTMLFromBundle()
        
    }
    
    func loadHTMLFromBundle(){
        let url = Bundle.main.url(forResource: "index", withExtension: "html")
        let urlRequest:URLRequest = URLRequest(url:url!)
        self.webView.loadRequest(urlRequest)
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func changeP(_ sender: Any) {
        self.webView.stringByEvaluatingJavaScript(from: "changeP()")
    }
    
    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        if request.url?.scheme == "wakwak" {
            self.webView.stringByEvaluatingJavaScript(from: "changeP()")
        }
        
        return true
    }
}

index.html

And here is the html file

<!DOCTYPE html>
<html>
    <head>
        <script>
            function changeP(){
                document.getElementById("p").innerHTML = "Josss pak!"
            }
        function sendmsg(){
            window.location = "wakwak://kukkuukukuku";
        }
        </script>
    </head>
    <body>
        <p id="p">Halo bibol!</p>
        <button onclick="sendmsg()">Change</P>
    </body>
</html>

URL Schemes

Another thing that you must configure is URL Schemes. Look what I’ve set here:

Post Views: 531
ciihuy2020

Welcome!

  • My YouTube Channel
  • My GitHub Page
  • About me

Categories

  • 3DVista
  • Android
  • Apache
  • C#
  • Cordova
  • Electron & Node JS
  • HTML5, CSS & JavaScript
  • iOS
  • Let's Make Unity Games
  • Misc
  • Photoshop
  • PHP
  • Python
  • Uncategorized
  • Unity
  • WordPress

Recent Posts

  • Hover Reveal script for Unity to show and hide object on mouse hover
  • How to Prevent UI Clicks in Unity from “Bleeding Through” to 3D Objects Behind Them
  • Make objects like wires and cables easily in Unity using Ciihuy Curved Mesh
  • [SOLVED] Can’t Add Custom Domain to Blogger After Losing CNAME Verification
  • iOS App Icon Generator by CiihuyCom
© 2026 ThirteeNov | Powered by Superbs Personal Blog theme