Scenarios in which iOS interacts with H5

Source: Internet
Author: User

Scenarios in which iOS interacts with H5

The following are some of the scenarios in which iOS interacts with H5:

    • The first: There are many apps that interact directly with native in a webview proxy, usually by intercepting URL scheme to determine if it is the URL we need to intercept and what function it corresponds to. Any version is supported.
    • Second: IOS7 after the javascriptcore.framework for the interaction with JS, but does not support iOS6, for also need to support the IOS6 app, can not consider this. If you need to know, see the last recommended reading.
    • The third type: Webviewjavascriptbridge open Source Library use, in essence, it is also through the WebView proxy interception scheme, and then inject the corresponding JS.
    • The fourth kind: react-native.

Here's how Webviewjavascriptbridge is used on the iOS side.

First, make sure that you have an HTML file that is well-equipped.

1. Initialize a webview (viewdidload)

uiwebview* WebView = [[initWithFrame:self.view.bounds];    [Addsubview:webview];  

2. Associate this webview with the Webviewjavascriptbridge (viewdidload)

Return }  [Webviewjavascriptbridge enableLogging];     _bridge = [Webviewjavascriptbridge bridgeforwebview:webview webviewdelegate:self handler:^ (ID data, Wvjbresponsecallback responsecallback) { NSLog (%@", data);  Responsecallback (@ "Response for message from OBJC"); }];        

PS: At this time your webview with JS on the bridge. The following is the mutual transfer of the method's mutual harmonic parameters.

(1) JS adjust OC method (can pass data to OC method value, use Responsecallback to return the value to JS)

[_bridge Registerhandler:@ "Testobjccallback" handler:^ (ID data, Wvjbresponsecallback responsecallback) {        NSLog (%@ ", data);        Responsecallback (@ "Response from Testobjccallback");}];    

Note Here the testobjccallback of this method. The HTML side of the name will be the same as iOS, can be transferred to this method. Of course, this name can be discussed on both sides of the custom. Simple and clear.

(2) OC Tuning JS Method (through the data can be passed the value, through the response can accept JS over the return value)

@ "GREETINGFROMOBJC@" Hi there, js! "};    [_bridge Callhandler:@ "Testjavascripthandler" Data:data responsecallback:^ (ID response) {        NSLog (%@ " , response); }];

Note that the Testjavascripthandler here is also a method indicator.

(3) OC to JS value (accept return value by response)

Send:@ "A string sent from OBJC to JS" responsecallback:^ (ID response) {        NSLog (%@ ", response);    }] ;

(4) OC to JS Pass value (no return value)

[_bridge send:@ "A string sent from OBJC after Webview have loaded."] ;

Scenarios in which iOS interacts with H5

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.