Skip to content

gomo/JsNoty

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JsNoty

This is a module that communicates javascript and Swift on the UIWebView.

Usage

Dependency

Install

Add JsNoty.swift to your project, and load JsNoty.js to you web page.

Swift side

Initialize and prepare

class ViewController: UIViewController, UIWebViewDelegate, JsNotyDelegate {
    private var jsNoty:JsNoty!;
    override func viewDidLoad() {
        super.viewDidLoad()

        self.jsNoty = JsNoty(webView: self.webView)
        self.jsNoty.delegate = self

        let url = NSURL(string: "http://www.expample.com/path/to/page.html")
        let request = NSURLRequest(URL: url!)
        self.webView.loadRequest(request)
    }

    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        if(self.jsNoty.receive(request)){
            return false;
        }

        return true
    }

    //JsNotyDelegate. Recieve event from javascript.
    func didRecieveJsNotification(name:String, data:JSON?){
        println(name, data)
    }

Notify to inside webView javascript

self.jsNoty.notify("eventName");

//send json object
self.jsNoty.notify("eventNameWithJson", data: JSON(["foo": "bar"]));

javascript side

You must call JsNoty.nofify() after window onload

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <script src="/js/js-noty/JsNoty.js"></script>
  <script>
    //recieve JsNoty.notify in Swift
    JsNoty.on('eventName', function(e){
      //do somthing
    });
    JsNoty.on('secondFromSwift', function(e, data){
      //do somthing using data
    });

    window.onload = function(){
      JsNoty.nofify("noparams");
      JsNoty.nofify("second", {foo: "bar", integer: 1, arr: ["1", 2, "hoge"]});
    };

  </script>
</head>
<body>
  <h1>JsNoty Test</h1>
</body>
</html>

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published