IOS: listening for JavaScript events (JavaScript communicating back with objective-C Code) in object-C)

Source: Internet
Author: User

In objective-C interaction between iOS development and JavaScript, we can useStringbyevaluatingjavascriptfromstring to obtain relevant node attributes in obj-C and add JavascriptCode. But how can we monitorWhat about JavaScript response events. Effective API implementation in Mac OS, But iPhone does not, but we have a way to do this:

The general idea is to setDocument. LocationThis will lead to a delegate method of webview, so as to achieve the effect of sending notifications, that is, to achieve the purpose of listening.

1. Define a protocol agreement between JavaScript and webview:

MyApp: myfunction: myparam1: myparam2

2. Add code in javascript:

 
Document. Location = "MyApp:" + "myfunction:" + param1 + ":" + param2;

3. Delegate method in webviewWebview: shouldstartloadwithrequest: navigationtype:Add

-(Bool) webview :( uiwebview * ) Webview2 shouldstartloadwithrequest :( nsurlrequest * ) Request navigationtype :( uiwebviewnavigationtype) navigationtype {nsstring * Requeststring = [[Request URL] absolutestring]; nsarray * Components = [requeststring componentsseparatedbystring: @"  :  "  ];  If ([Components count]>1 && [(Nsstring *) [Components objectatindex: 0 ] Isequaltostring: @"  MyApp  "  ]) {  If ([(Nsstring *) [components objectatindex: 1 ] Isequaltostring: @"  Myfunction  "  ]) {Nslog ([components objectatindex: 2 ]); //  Param1 Nslog ([components objectatindex: 3 ]); //  Param2  //  Call your method in objective-C method using the above...  }  Return  No ;}  Return Yes; // Return YES to make sure regular navigation works as expected. }

 

 

Check: http://stackoverflow.com/questions/5671742/send-a-notification-from-javascript-in-uiwebview-to-objectivec

Http://www.codingventures.com/2008/12/using-uiwebview-to-render-svg-files/

 

 

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.