(Xcode) How to connect to a PHP page?
I want to login at a page with my username and password. I want that my app connects to the php page in a UIWebView and login.
iPhone 4, iOS 4
I want to login at a page with my username and password. I want that my app connects to the php page in a UIWebView and login.
iPhone 4, iOS 4
There is nothing much to do. Just load the URL to the PHP page in your UIWebView and you should be all done.
Yes, my code loads the url but when I put my id and my password the webview doesn´t login. It doesnt do anything only loads the page. Do you want to see my code sptrakesh?
That is strange. The issue is unlikely to be with UIWebView itself (unless of course the result page is sending some markup that the web view cannot render, flash for instance). Do you have access to the server logs? If yes, that would be the best option to debug this issue. Another quick and easy test is to try and load the PHP page from Safari and see if you can login. UIWebView for the most part should behave in an identical fashion.
In Safari the php page loads, I know it is really strange! Do you have a sample code for something like this? Or do you want to see my code? The only thing is that the PHP page is in spanish because im from Mexico
Sure, post your code, so we can try and spot any potential issues.
Header (.h)
@interface UP_ViewController : UIViewController {
IBOutlet UITextField *nameInput;
IBOutlet UITextField *passInput;
IBOutlet UILabel *greeting;
UIAlertView *alert;
UIAlertView *alert2;
NSMutableData *webData;
}
@property(nonatomic, retain) IBOutletUITextField *nameInput;
@property(nonatomic, retain) IBOutletUITextField *passInput;
@property(nonatomic, retain) IBOutletUILabel *greeting;
@property(nonatomic, retain) NSMutableData *webData;
@property(nonatomic, retain) UIAlertView *alert;
@property(nonatomic, retain) UIAlertView *alert2;
-(IBAction)buttonClick: (id) sender;
@end
Main (.m)
@synthesize nameInput;
@synthesize passInput;
@synthesize greeting;
@synthesize webData;
@synthesize alert;
@synthesize alert2;
- (void)dealloc
{
[super dealloc];
}
-(IBAction)buttonClick:(id)sender
{
NSString* username = nameInput.text;
NSString* pass = passInput.text;
if([nameInput.textisEqualToString:@"" ]||[passInput.textisEqualToString:@""])
{
alert2 = [[UIAlertViewalloc] initWithTitle:@"UP Network"message:@"Por Favor Ingrese sus Datos"delegate:selfcancelButtonTitle:@"Continue"otherButtonTitles:nil];
[alert2 show];
[alert2 release];
[nameInputresignFirstResponder];
[passInputresignFirstResponder];
return;
}
NSString *post =
[[NSStringalloc] initWithFormat:@"Nombre de usuario=%@&Contraseña=%@",username,pass];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncodingallowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSURL *url = [NSURLURLWithString:@"Nombre de Usuario;Contraseña@moodle.up.edu.mx/moodle/login/index.php"];
NSMutableURLRequest *theRequest = [NSMutableURLRequestrequestWithURL:url];
[theRequest setHTTPMethod:@"POST"];
[theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPBody:postData];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if( theConnection )
{
webData = [[NSMutableData data] retain];
}
else
{
}
[nameInputresignFirstResponder];
[passInputresignFirstResponder];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
alert2 = [[UIAlertViewalloc] initWithTitle:@"UP Network"message:@"Datos Incorrectos"delegate:selfcancelButtonTitle:@"Continue"otherButtonTitles:nil];
[alert2 show];
[alert2 release];
nameInput.text = nil;
passInput.text = nil;
[connection release];
[webData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *loginStatus = [[NSStringalloc] initWithBytes: [webDatamutableBytes] length:[webDatalength] encoding:NSUTF8StringEncoding];
NSLog(loginStatus);
greeting.text = loginStatus;
[loginStatus release];
[connection release];
[webData release];
}
in spanish "Nombre de usuario" = english "Username"
in spanish "Contraseña" = english "Password"
moodle.up.edu.mx/moodle/login/index.php is the pagei want to log in
Looking at the HTML page source, it does not look like the form handler expects the username and password parameter (or form field) names to be in Spanish. Does it work, if you use the same names as specified in the form source?
Those are just labels, that is not what the form handler expects. View source on the page and inspect the form HTML element. You will see the actual names of the form fields. When you post to the form handler, that is what the handler expects. The labels are meaningless to the form handler.
And how do i do that? How do I see the real names? I`m practically a noob hahaha sorry
Is my syntaxis of the code is correct? I something wrong?
Open the
moodle.up.edu.mx/moodle/login/index.php URL in your browser and use the menu or context menu to view source of the page. Look for <form action="index.php". Under that you will see a couple of <input type="text" name="xxx". The name is what the form handler expects as the parameter names in the post.
Thanks for all man. Is there a way I can give you ponits or something for my gratitude?
And something else is there a way to log in to a page that isnt a php page?
(Xcode) How to connect to a PHP page?