Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bridge is not working, Swift 2.3. #266

Closed
lukewakeford opened this issue Jan 24, 2017 · 2 comments
Closed

Bridge is not working, Swift 2.3. #266

lukewakeford opened this issue Jan 24, 2017 · 2 comments

Comments

@lukewakeford
Copy link

lukewakeford commented Jan 24, 2017

I've converted the example in to Swift but there doesn't appear to be any communication between swift and js at all.. Is there something I am doing wrong? - There are no errors reported.

I am loading the html content from a localhost url instead of including the html file, apart from that I don't think I've done anything different, here is my Swift file. (I did test including the html file in the project instead but I still did not see any communication)

import Foundation
import WebViewJavascriptBridge

class IntroWebView:UIViewController, UIWebViewDelegate {
    
    var bridge:WebViewJavascriptBridge!
    
    override func viewDidAppear(animated: Bool) {
        if let _ = self.bridge { return }
        let webView = UIWebView(frame: self.view.frame)
        self.view.addSubview(webView)
        WebViewJavascriptBridge.enableLogging()
        self.bridge = WebViewJavascriptBridge(forWebView: webView)
        self.bridge.setWebViewDelegate(self)
        self.bridge.registerHandler("testObjcCallback", handler: { (data:AnyObject!, responseCallback:WVJBResponseCallback!) in
            print("testObjcCallback called: \(data)")
            responseCallback("Response from testObjcCallback")
        })
        self.bridge.callHandler("testJavascriptHandler", data: ["foo","before ready"])
        renderButtons(webView)
        self.loadExamplePage(webView)
    
    }
    
    func webViewDidStartLoad(webView: UIWebView) {
        print("webViewDidStartLoad")
    }
    
    func webViewDidFinishLoad(webView: UIWebView) {
        print("webViewDidFinishLoad")
    }
    
    func renderButtons(webView:UIWebView) {
        let font = UIFont(name:"HelveticaNeue", size:11)
        let callbackButton = UIButton(frame: CGRect(x: 0, y: 400, width: 100, height: 35))
        callbackButton.setTitle("Call handler", forState: .Normal)
        callbackButton.addTarget(self, action: #selector(IntroWebView.callHandler(_:)), forControlEvents: .TouchUpInside)
        callbackButton.titleLabel?.font = font
        callbackButton.backgroundColor = UIColor.darkGrayColor()
        self.view.addSubview(callbackButton)
        
        let reloadButton = UIButton(frame: CGRect(x: 90, y: 400, width: 100, height: 35))
        reloadButton.setTitle("Reload webview", forState: .Normal)
        reloadButton.addTarget(webView, action: #selector(webView.reload), forControlEvents: .TouchUpInside)
        reloadButton.titleLabel?.font = font
        reloadButton.backgroundColor = UIColor.darkGrayColor()
        self.view.addSubview(reloadButton)
    }

    func callHandler(sender:UIButton) {
        let data = ["greetingFromObjC": "Hi there, JS!"]
        print(data)
        self.bridge.callHandler("testJavascriptHandler", data: data, responseCallback: { (response:AnyObject!) in
            print("response \(data)")
        })
    }

    func loadExamplePage(webView:UIWebView) {
        let url = "http://localhost:8888"
        let request = NSURLRequest(URL: NSURL(string:url)!)
        webView.loadRequest(request)
    }
    
}

And the html is an exact copy of the example code here:

<!doctype html>
<html><head>
    <meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0">
	<style type='text/css'>
		html { font-family:Helvetica; color:#222; }
		h1 { color:steelblue; font-size:24px; margin-top:24px; }
		button { margin:0 3px 10px; font-size:12px; }
		.logLine { border-bottom:1px solid #ccc; padding:4px 2px; font-family:courier; font-size:11px; }
	</style>
</head><body>
	<h1>WebViewJavascriptBridge Demo</h1>
	<script>
	window.onerror = function(err) {
		log('window.onerror: ' + err)
	}

    function setupWebViewJavascriptBridge(callback) {
        if (window.WebViewJavascriptBridge) { return callback(WebViewJavascriptBridge); }
        if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); }
        window.WVJBCallbacks = [callback];
        var WVJBIframe = document.createElement('iframe');
        WVJBIframe.style.display = 'none';
        WVJBIframe.src = 'https://__bridge_loaded__';
        document.documentElement.appendChild(WVJBIframe);
        setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0)
    }

    setupWebViewJavascriptBridge(function(bridge) {
		var uniqueId = 1
		function log(message, data) {
			var log = document.getElementById('log')
			var el = document.createElement('div')
			el.className = 'logLine'
			el.innerHTML = uniqueId++ + '. ' + message + ':<br/>' + JSON.stringify(data)
			if (log.children.length) { log.insertBefore(el, log.children[0]) }
			else { log.appendChild(el) }
		}

		bridge.registerHandler('testJavascriptHandler', function(data, responseCallback) {
			log('ObjC called testJavascriptHandler with', data)
			var responseData = { 'Javascript Says':'Right back atcha!' }
			log('JS responding with', responseData)
			responseCallback(responseData)
		})

		document.body.appendChild(document.createElement('br'))

		var callbackButton = document.getElementById('buttons').appendChild(document.createElement('button'))
		callbackButton.innerHTML = 'Fire testObjcCallback'
		callbackButton.onclick = function(e) {
			e.preventDefault()
			log('JS calling handler "testObjcCallback"')
			bridge.callHandler('testObjcCallback', {'foo': 'bar'}, function(response) {
				log('JS got response', response)
			})
		}
	})
	</script>
	<div id='buttons'></div> <div id='log'></div>
</body></html>

Here is a screenshot.

simulator screen shot 24 jan 2017 11 17 17

@liuwin7
Copy link
Contributor

liuwin7 commented Feb 16, 2017

Which version do you use in your project? if you followed the README.md, you will install the version 5.2.0 not the last version 6.0.2. Due to that the example in README.md is for 6.0.2, so the content in the function setupWebViewJavascriptBridge should be different. the line WVJBIframe.src = 'https://__bridge_loaded__'; should be WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__';.
I think the best approach is using the current version 6.0.2 is. use pod 'WebViewJavascriptBridge'.
I will pull request for this issue.

liuwin7 added a commit to liuwin7/WebViewJavascriptBridge that referenced this issue Feb 16, 2017
The 'Usage' describes the latest version 6.0.2 and the 'Installation with CocoaPods' installs the version 5.2.0. So, if someone follows the tutor,  the Objc will not connect with javascript. The reason is here marcuswestin#266 (comment) .
marcuswestin pushed a commit that referenced this issue Nov 8, 2017
The 'Usage' describes the latest version 6.0.2 and the 'Installation with CocoaPods' installs the version 5.2.0. So, if someone follows the tutor,  the Objc will not connect with javascript. The reason is here #266 (comment) .
@marcuswestin
Copy link
Owner

Please see working swift example in Example Apps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants