How to call Objective-C from Javascript?

前端 未结 8 1677
误落风尘
误落风尘 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: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!

提交回复
热议问题