Without any external framework we can do that.To achieve this like phone we have to use webView.
For example, If you need to call an objective c function when a button is clicked in the webview.When the onclick of your button in the html page calls a javascript, just add an iFrame which will include the function name of the objective C function.
eg:
var iframe = document.createElement("IFRAME");
iframe.setAttribute("src", "js-frame:" + functionName + ":" + callbackId);
document.documentElement.appendChild(iframe);
iframe.parentNode.removeChild(iframe);
iframe = null
;
This iFrame can be accesed in your webView Delegate implemented in your ParentView of the webView, like-
- (BOOL)webView:(UIWebView *)webView2
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
NSString *requestString = [[request URL] absoluteString];
if ([requestString hasPrefix:@"js-frame:"]) {
NSArray *components = [requestString componentsSeparatedByString:@":"];
NSString *function = (NSString*)[components objectAtIndex:1];
int callbackId = [((NSString*)[components objectAtIndex:2]) intValue];
stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
;
[self handleCall:function callbackId:callbackId];
return NO;
}
return YES;
}
The handleCall will have to handle the invoking of your intended method having name "function"