How to call Objective-C from Javascript?

前端 未结 8 1576
误落风尘
误落风尘 2020-11-22 17:38

I have a WebView, and I want to call a view in Objective-C from JavaScript. Does someone know how I can do this?


I have this code in my ViewController:

相关标签:
8条回答
  • 2020-11-22 17:38

    Although this is a very old question now, it keeps getting returned by Google and there is a good answer now: the WebScripting informal protocol. It allows you to expose an objective C object to Javascript.

    http://developer.apple.com/library/safari/documentation/appleapplications/Conceptual/SafariJSProgTopics/Tasks/ObjCFromJavaScript.html#//apple_ref/doc/uid/30001215-BBCBFJCD

    0 讨论(0)
  • 2020-11-22 17:40

    I had an issue with this approach: I wanted to send several messages to the iphone device, but it seemed that they were "overlaped" as they could not process all of them sequentially.

    Example: when executing this code:

    window.location = "app://action/foo";
    window.location = "app://action/bar";
    

    The action foo was never executed.

    What I had to do was the following:

    waitingForMessage = false;
    
    function MsgProcessed(){
        waitingForMessage = false;
    }
    
    function SyncLaunchURL(url){
        if (waitingForMessage){
            setTimeout(function(){SyncLaunchURL(url)},100);
        }else{
            window.location = url
            waitingForMessage = true;   
        }
    }
    
    SyncLaunchURL("app://action/foo");
    SyncLaunchURL("app://action/bar");
    

    With this approach, the iphone has to call MsgProcessed() after processing the call. This way works for me, and maybe helps someone with the same problem!

    0 讨论(0)
  • 2020-11-22 17:43

    Like people said here, you have to use the method webView:shouldStartLoadWithRequest:navigationType: from the UIWebviewDelegate.

    This api http://code.google.com/p/jsbridge-to-cocoa/ does it for you. It is very lightweight. You can pass images, strings and arrays from javascript to objective-C.

    0 讨论(0)
  • 2020-11-22 17:47

    Assuming you're doing an app, you can look at how PhoneGap implements that (or even use it). It's a library that supports back-and-forth communication between JS and OBJ-C. There are other libraries and solutions, as well.

    If you're talking about a web app (something the user gets to from Mobile Safari), you can't get to Objective-C from there.

    0 讨论(0)
  • 2020-11-22 17:48

    Check this one - understanding XMLHttpRequest responses using this (or other javascript) functions?, it's using objective C to call ajax js function, and get the response after it's done, you know the trick is that webview will be triggered when you change the location in javascript, so you can check the location to know its your javascript call or the real request.

    0 讨论(0)
  • 2020-11-22 18:01

    Obliviux,

    Your code seems to be perfect.

    The reason for the problem is that you must have missed to map the delegate.

    Either

    1. Connect the delegate of the webView to the file owner in the .xib file

    or

    1. Use webView.delegate = self;

    in your viewDidLoad.

    Thanks

    0 讨论(0)
提交回复
热议问题