has driven the wide application of HTML5 technology a little exaggerated, but it did fire a bit.
Learn how iOS interacts with H5 now.
Part-one :
First, the principle
1.h5 to interact with iOS, first build Webviewbridge, communication bridge;
2. The second is the response of both parties.
A. First register the name of a response on the H5 side.
B.oc can call H5 by this name.
C.oc send a message to H5 in two cases, one is to require H5 to return parameters, and the other is to not require H5 to return data
Part-two
Second, use
The more useful third library Webviewjavascriptbridge
First step: register a UIWebView
uiwebview* WebView = [[UIWebView alloc] initwithframe:self.view.bounds];
[Self.view Addsubview:webview];
Step Two: Register "bridge"
-(void) Viewdidload {
if ( Self.bridge = = nil) {return;} [Webviewjavascriptbridge enableLogging]; Self.bridge = [Webviewjavascriptbridge bridgeforwebveiw:webviewdelegate : Self handler:^ (id data, Wvjbresponsecallback responsecallback) {
NSLog (@ "OBJC received message from JS:%@", data); }
Responsecallback (@ "Response for message from OBJC"); }];
}
The third step: JS Call oc Method
[_bridge Registerhandler:@ "Testobjccallback" handler:^ (ID data, Wvjbresponsecallback responsecallback) { NSLog (%@ ", data); Responsecallback (@ "Response from Testobjccallback");}];
Fourth step: OC Call JS method
[_bridge callhandler:@ "Testobjccallback" Data:data responsecallback:^ (ID response) {
Response (@ "response from Testobjccallback");
}
Fifth:
No parameter return
[_bridge Send: @ "This is a message from OC"];
Returned with a reference
[_bridge send:@ "This a message from OC, please answer the message when u see it" responsecallback (^id response) {
NSLog (@ "The answer from JS");
}];
Native vs HTML5