JavaScriptCore Framework in Practice

The following interaction approach was used to achieve a unified interface with Android. Background: Those familiar with Android's will know that in Android you simply annotate a method with , then add a to the WebView (), and from the JS side you can directly call Java methods using . I replicated this pattern using — associating with the page's via — thereby achieving essentially the same JS interaction approach as Android. See the code below: - conforms to the protocol it defines, and implements the corresponding methods in . - Note that methods beginning with require method name remapping using . Of course, you can also use to convert the awkward Objective-C method naming style into something more familiar for JS and Java. - A cannot be created directly; it must be retrieved from the WebView: . - During debugging, you can define a method and call from your JS file to debug. You can also replace with the method, but be prepared for a flood of output. Notes on Loading HTML: - When adding resource files, use — the folder icon will appear blue. - Both and can load HTML files and their corresponding JS and CSS resources from the main bundle. If they fail, check whether the HTML file is in UTF-8 encoding and review the point above. Also, when using the resource loading approach described above, remember to add relative paths. Code: All code from this post can be found on my GitHub . References: wiki-JavaScriptCore