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