HTML button to a native bridge iOS - iphone

I have an index.html page that has multiple buttons. I would like to be able to click on a button and it goes to the native side of the phone. To call it and do some action on the HTML page. I don't know how to do that?
In other words i would like to go from HTML to ios native actions and features, such as calling camera, gyro and so on.

You have to make some changes in your html page.
Use image instead of button.Then use anchor tag around image tag.
Set href as "btn://btn1" or what you want to identify your button.
Ex.
<img src="test.png" >
In ios side:
1.Assign delegate to your UIWebview.
2.Implement below method
//Method when bookmark button pressed
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if ([request.URL.scheme isEqualToString:#"btn"])
{
//Enter your Code here
}
return YES;
}
using above method you can identify witch button is clicked and according to that you can perform any action you want.

Related

how to perform an action in iphone application on click in webview

I need to implement a functionality such that I have there is a login screen which sends an http request packet, gets a json response(a boolean and a routing link) and opens a webview. I have done this successfully. But further what I need to do is that when I click sign out in the webview, I should get back to the native login screen.How can I achieve that?
Check this call back method
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
}
You get this call back when you click link in webview.

html select tag used in iOS app, but the multiple select not works

In my ios app, I use loadHTMLString to show a view. In this view, I create a select tag with multiple selection. I use the html code:
[HTML appendFormat:#"<select multiple=\"multiple\" onchange=\"updateSurveyObject();\" name=\"%#\" id=\"%#-0\" >",elementID,elementID];
When I click on one option, it will invoke web delegate:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
But the question is: if I click on that option again or click on the second option, it won't call that delegate any more. Even click on Done button, the delegate method is not called.
So, how should I do with the multiple selection?
Thanks!
I changed the "onchange" to "onblur". It's not perfect but it works. Do you have better idea?thx

UIWebViewNavigationTypeOther Vs UIWebViewNavigationTypeLinkClicked

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
//my code
}
Currently my above delegate method is called, once i touch inside in my webview.
So i want to know what is the difference between following 2 navigationtype: UIWebViewNavigationTypeOther & UIWebViewNavigationTypeLinkClicked.
Because some of the URL on selection provides navigation Type as UIWebViewNavigationTypeOther & some provide UIWebViewNavigationTypeLinkClicked.
From the apple document i cannot able to clear my self.
UIWebViewNavigationTypeLinkClicked happens if the user taps a <a href=""> style link, if the change is done from within Javascript (onclick event for example) UIWebViewNavigationTypeOther is used.
Furthermore UIWebViewNavigationTypeOther is used if you first load a page or you'll be redirected by a meta refresh or something

iphone notification from javascript to objective C

I've designed my main view using HTML, CSS and some images. It looks very nice. I'm using some images that are clickable in HTML code.
On click of some image, I want to load some view accordingly.
Is it possible? If yes, please suggest me how to do it.
If tutorial or some sample code is available please share it.
Implement the following UIWebView Delegate Method and call your Objective methods
(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSLog(#"LINK CLICKED......");
//return NO;
}
return YES;
}
You can look into PhoneGap for some sample code.
There are two parts. First you define a custom scheme for you application that is only used to communicate between the web page and the delegate. Could just be 'foo:' as it will be private to the page and host. You then handle this custom scheme in the delegate shouldStartLoadWithRequest method and return NO.
With a clickable link, you can just set the href to something that starts with your custom scheme. If you need to send messages at arbitrary times, you can use javascript to set the window.location to a url with your custom scheme.

iPhone architect choice for large text flow w/links

I am designing an app which will present large amounts of text that is interspersed with notes and references as clickable images. On a PC I'd use a control that shows HTML, but in the iPhone I am not able to intercept the touches of images and links too well using the UIWeb control.
Should I use a UIScroll and build the text as lables and UIImages perhaps?
Looking for the best way forward in my design phase.
I don't know what your requirements are obviously, but it is possible to capture the click on an link in a UIWebView and take some alternative action. In one of my Apps, I have a UIWebView with one particular link which I want to route differently, while I let all other links open as web pages in the UIWebView as normal. Here's the code snippet from the app which accomplishes this. It is within a UIViewController which loads the UIWebView:
- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = [ request URL ];
if( [[url path] isEqualToString:#"/the_special_link.html"] ) {
// Take some alternative action and then stop the page from loading...
// (code to take some special action goes here)
return NO;
}
else {
return YES;
}
}
This is a delegate method call, so when I set up the UIWebView, which I do programmatically in the Controller loadView method, I set the WebView's delegate to that same Controller:
myWebView.delegate = self;