What I am tying to do is to login to a website from Xcode. I am communicating with the website by using JavaScript and the DOM. Here is my issue. I can enter the username and password into the website and then submit that form and it will log me in but what I don’t know how to do is to see if the password is valid or not.
On the website if the password is invalid it will say “Unable to login.”
this is what i have so far
-(IBAction)Login:(id)sender {
stud_pass = Username.text;
password1 = Password.text;
//Puts the 2 NSStrings into the websites text fields
[webView stringByEvaluatingJavaScriptFromString: [NSString stringWithFormat:@"document.getElementById('stud_pass').value = ('%@');", stud_pass]];
[webView stringByEvaluatingJavaScriptFromString: [NSString stringWithFormat:@"document.getElementById('password1').value = ('%@');", password1]];
//Sends the form
[webView stringByEvaluatingJavaScriptFromString:@"document.forms[0].submit();"];
//See if login was sucsessfull
if (
} else {
}
}
This is where I am stuck, and I don’t know how I can make the webview tell xcode that the login was unsuccessful and then based on that make xcode have a certain output.
The basic problem with what you are assuming you can do in the “
//See if login was sucsessfull” portion of your code is that this function will have returned long before(in machine time) yourUIWebViewwill have loaded the page stating whether or not you have successfully logged in. This is how it should be since yourIBActionmethod is tied to the UI and locking that method with a network request would, in short, be bad.What you will need to do is to determine whether you have a successful login by using a
stringByEvaluatingJavaScriptFromString:call while in one of theUIWebView‘s delegate methods, probablywebViewDidFinishLoad:. The implementation of that function will vary greatly based on the structure of the website you are attempting to login to. But could look something like this: