Submit a form in UIWebView - iphone

I have a form in the UIWebView to interact with a web service, when I submit the form, the data gets sent to a web service and it will return the result to another UIWebView. The two UIWebViews are under a navigation controller, so when the use submits the form, it slides to another view to display the result. Can anyone point me out how I can achieve this? What do I fill in the "action" attribute in the form element? How can I get the user input back to the application so I can use the web service? I am new to this field, any help will be appreciated. Thanks!
Update:
Is there a way to save form data from web view back to my program? I think it's better to save form data back to the program (i.e. store params into nsstring) when I click submit button, and then start querying web service.

You can use the
stringByEvaluatingJavaScriptFromString
function for firing a javascript method from objective c and get a return value from that method.
For example
//Calling the getTheUsername in the javascript.
NSString *userName = [yourWebView stringByEvaluatingJavaScriptFromString:#"getTheUsername()"];
and in javascript
//Method in javascript
function getTheUsername()
{
return userNameVariable;
}
And I doubt that here you may wanna do vice-versa (Call obj-c method from javascript). This cannot be done directly here is the work around.
Set a UIWebViewDelegate, and implement the method webView:shouldStartLoadWithRequest:navigationType:. In your JavaScript code, navigate to some fake URL that encodes the information you want to pass to your app, like, say:
window.location = "someLink://yourApp/form_Submitted:param1:param2:param3";
In your delegate method, look for these fake URLs, extract the information you need, take whatever action is appropriate, and return NO to cancel the navigation.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
NSLog(#"%#",[[request URL] query]);
return YES;
}

If I understood your problem correctly, I think following steps will help you to provide the solution.
In the action of form, you need to write the URL to the web service.
In your form, the name of the input field should be matched with the name of the parameters expected for the web service.
In your first UIWebView Navigation control, Create the object of screen having second UIWebView and set the response of the web service to the instance member of the object.
In your first UIWebView Navigation control, you need to write
[self presentModalViewController:webView2Object animated:YES];
In the screen of having webView2, parse the response before loading the second UIWebView. and inject it with the displayed page using javascript.

Related

webview linkclicked

I have a webview loading a website with a html form. When the user has entered their login credentials and push submit-button the user is sent to a new view controller. I want to stop this from happening IF the user enters the wrong credentials. If the wrong credentials are entered the website adress remains the same. Can I somehow say:
if (navigationType == UIWebViewNavgiationTypeLinkClicked) {
if (url remains the same == alert view saying wrong credentials was entered.
if (url changes - perform segue to new view controller?
Let your controller implement UIWebViewDelegate. When you do that, a bunch of callbacks will be called at different phases of the web request. The one you probably want to implement is this one:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
// Be sure to use mainDocumentURL, because any iframes on your form page will
// also trigger this callback
NSString *completedurl = webView.request mainDocumentURL.absoluteString;
// Let's assume you have your form url in the string property self.formurl
if ([completedurl isEqualToString:self.formurl]) {
// Display your alert view saying that the credentials were incorrect
} else {
// Perform segue to next controller
}
The whole solution is a bit scary though, because any other error page or whatever would
trick your app to believe that the user was successfully logged on. A better approach might
be to check the html contents of the webview for some significant string that the
successful-login-page contains. You can get the html body by calling:
NSString *yourHTMLSourceCodeString = [webView stringByEvaluatingJavaScriptFromString:#"document.body.innerHTML"];
Yet another approach is to look for a cookie in the webview that confirms the login.
Or even better, use a native login form, submit the login request with native code, using
AFNetworking or the standard http SDK methods, and check the response for success.

iOS uiwebview goBack history control

I am loading a "webpage1" on an uiwebview and saving its first request on an instance variable "webviewHistory". Then, I must reuse that webview to load another page, "webpage2" (saved its first request to "webviewHistory" too), and history should start now from this request. Problem is if I execute goBack (button IBAction) from "webpage2", I can keep going back to "webpage1" history and following.
If I check request and compare with initial saved, works! but not with redirections, for example, if I trigger request to www.youtube.com, request is redirectioned to m.youtube.com and first one is not saved as navigation history! how to solve it?
if (![webViewSocial.request.URL.absoluteString isEqualToString:self.webviewHistory]){
[webViewSocial goBack];
}
UIWebviews have instance methods that allow you to go backwards and forwards with the history, without you having to track the last request. Is there a particular reason you are saving the last request specifically?
Sample code:
if ([webView canGoBack]) {
[webView goBack];
}
Here is a project that is a simple uiwebview browser if you want to play with it:
https://github.com/msencenb/UIWebView-Example
for swift 3 version of Msencenb answer:
if webView.canGoBack {
webView.goBack()
}

iPhone: Handle action event from webview page

I am loading a html page using loadHTMLString.
[myWebview loadHTMLString:myHtmlStr baseURL:nil];
Its loading properly. In this html web page, i'm also adding buttons, drop down(with selection) etc. I want to catch the action events in my code now for these web page controls. For example, from the drop down if user chooses an option, i need to add another 'textarea' dynamically in the same web page. and, if user clicks on a button(button from the webview), i need to handle some events. I would like to know, how to do some tasks under the events triggered for controls in web view which is generated by my string.
Could someone please advise me.
to handle event on button in webview load a url in webview for ex:http://button1clicked and then in webView:shouldStartLoadWithRequest:navigationType: method check if the url is button1clicked then return no and also perform your action which you want to do on button clicked.
for example:
write this in webView:shouldStartLoadWithRequest:navigationType: method.
if([[[request URL] absoluteString] isEqualToString:#"http://button1clicked"])
{
//perform you action you want to do
return NO;
}
and on button click call javascript function window.location='http://button1clicked';
The documentation clearly states that UIWebView is not intended for Sub-Classing. However, you can still detect all the events.
But maybe this will help : Tutorial

iPhone : webView:shouldStartLoadWithRequest:navigationType timing problem

I have a WebView which is loading and HTML String and I want it to catch clicks on links.
For that I need to use the webView:shouldStartLoadWithRequest:navigationType method.
The problem is that this method gets called multiple times before the HTML content is fully loaded and I only want to start catching clicks at that moment.
The question is how to know when the HTML content is fully loaded ? I thought it was simple so I created a boolean as an iVar of the ViewController containing the WebView and I set it to YES after calling loadHTMLString. Then, in webView:shouldStartLoadWithRequest:navigationType I was testing if that boolean was true and if it was the case I was outputting something like "OK". But "OK" was appearing without clicking on a link => fail.
Any idea on how I could make this work ?
Thanks in advance
You could use the webViewDidFinishLoad: delegate method to know when the HTML is loaded.
But I'd rather use another solution:
In webView:shouldStartLoadWithRequest:navigationType: you can filter requests by navigation type:
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
// Catch links
return NO; // if you want to cancel the request, else YES
}
- (void)webViewDidFinishLoad:(UIWebView *)webViews{
}
this method will callwhen the HTML content is fully loaded.
it may helps you.

Updating address bar with current url from UIWebview

Like iPhone safari browser, I want to update UItextview with current url of UIWebView
I tried shouldStartLoadWithRequest delegate method, but I end up with url of some ads/iframe
If I use webViewDidFinishLoad delegate method, then address bar gets updated at the end of the request.
Please help me on the best approach.
You want to use the mainDocumentURL method on the NSURLRequest in webView:shouldStartLoadWithRequest:. This should contain the main document URL regardless of whether the page is loading or a resource on the page (e.g. a frame) is loading.
Use webviewDidFinishLoad when delegated from your webview, and then query it for the title with [webview stringByEvaluatingJavaScriptFromString:#"document.title"] or weboutWebView.request.URL.absoluteString for the url.