webview linkclicked - iphone

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.

Related

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

Submit a form in UIWebView

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.

Which view function load after pop-up exit in iphone

I have a login screen which is saying "SingnIn" when user is clicking on that he getting a pop of facebook login screen.
I want that if somebody successfully logged in, I want to change SignIn text with their name. But I am not getting place where I should write that part of code. Which view function load again after pop screen exit.
If you implemented facebook connect and if you meant facebook login then after login you can write code in facebook delegate method.
- (void)session:(FBSession*)session didLogin:(FBUID)uid {
//Write code here for changing label for user name
}

iPhone - How may I know where my user is going to into a UIWebView

Web presenting a website in a UIWebView, the user can touch links.
When testing [[webView.request URL] absoluteString]in didFailLoadWithError or webViewDidStartLoad, it returns the previous url. The touched url is shown in webViewDidFinishLoad
If the user touches a link, and the connection fails, how may I know where he was going to as webViewDidFinishLoad is not triggered ?
Immediately after the link is tapped, -webView:shouldStartLoadWithRequest:navigationType: is called in your delegate. That gives you a chance to inspect the request and what kind of navigation action triggered it (link, back/forward, form post...).