Let’s change the subject: this time no more talks about memory but always on
UIWebView component. When we use this component for something else than just
The first move is easily done with the following piece of code:
doesn’t offer any native way to do this! So we have to use any king of hack to
do this …
What’s wrong with UIWebViewDelegate, shouldStartLoadWithRequest and location change ?
There is weird but apprehensible bugs with this practice:
- All setInterval and setTimeout immediatly stop on location change
- Every innerHTML won’t work after a canceled location change!
- You may get other really weird bugs, really hard to diagnose …
Key files of this example:
- MyWebview.m: Objective-c part, that inherit from UIWebView. Set the UIWebViewDelegate and catch requests in shouldStartLoadWithRequest selector.
- webview-script.js: Test case script, that highlight these bugs.
In webview-script.js: InnerHTML stop working whereas textContent continues to …
document.getElementById("count").innerHTML = i; document.getElementById("count2").textContent = i;
But we can’t charge Apple on this bug. I mean we try to load another location
in the document we are working on! The webview component may start doing stuff
before the delegate call, which cancel the load …
We have to find alternative way to communicate with the native code!
Better way to call Objective-C
document location, we create an IFrame and set its location to a value that
trigger the shouldStartLoadWithRequest method.
var iframe = document.createElement("IFRAME"); iframe.setAttribute("src", "js-frame:myObjectiveCFunction"; document.documentElement.appendChild(iframe); iframe.parentNode.removeChild(iframe); iframe = null;
Here is another sample application, with exactly the same structures and test
Finally I provide the communication library under LGPL licence so it can ease
your work on iphone platform! As I know that it’s really not easy ;-)
The code is full of comment, so you may easily use and tweak it!