0

I'm using pretty much the this process oulined here to allow javascript to call objective c from my iPad app:

http://www.stevesaxon.me/posts/2011/window-external-notify-in-ios-uiwebview/

My javascript code that I'm injecting into the page is:

<script type="text/javascript">;
window.external =
{
'Set_A': function(s) { document.location = 'qms://Set_A?param=' + s; },
'Get_A': function(s) { document.location = 'qms://Get_A'; },

'Set_B': function(s) { document.location = 'qms://Set_B?param=' + s; },
'Get_B': function(s) { document.location = 'qms://Get_B'; },

'Set_C': function(s) { document.location = 'qms://Set_C?param=' + s; },
'Get_C': function(s) { document.location = 'qms://Get_C'; },

'Get_Version': function(s) { document.location = 'qms://Get_Version'; }
};
</script>;

And the javascript in the html code that calls the above method for initialization is:

<html>
<body>
...
<script type="text/javascript">

Get_Version();

Set_A(true);
Set_B(false);
Set_C(true);

<script>
</body>
</html>

In the above html the only method that gets called in Objective C is the last method Set_C(true);

Thanks.

ctrlspace
  • 519
  • 5
  • 15
  • I suspect you'll need to chain all the calls into one ... i.e. only the last document.location set in javascript will get forwarded to your app. – CSmith Nov 06 '12 at 16:21
  • 1
    You can try to call these functions asynchronously like `setTimeout(function() { Set_A(true); }, 1)`. I guess UIWebView does not stop executing your JS after each line and loads a new page. So only the last one has any effect. – dreame4 Nov 06 '12 at 16:30
  • all of the document.location get "set", that is I can click a link or a button on the html page after the load and call objective-c as long as it's only one function that gets called. – ctrlspace Nov 09 '12 at 18:22
  • I'm not familiar with the setTimeout js method. I'll brush up on it and give it a try. In the uiwebviewdelegate method webView shouldStartLoadWithRequest we return no after the initial page is loaded (whenever the js code is sent to objective-c). I'm not sure how to better trap these calls. – ctrlspace Nov 09 '12 at 18:26

1 Answers1

1

So I used this blog entry to make things work: http://blog.techno-barje.fr/post/2010/10/06/UIWebView-secrets-part3-How-to-properly-call-ObjectiveC-from-Javascript/

ctrlspace
  • 519
  • 5
  • 15