Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

(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

Posted on May 12, 2011 8:26 AM

Reply
33 replies

May 12, 2011 8:25 PM in response to G_Morales

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.

May 16, 2011 11:29 AM in response to G_Morales

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

May 16, 2011 11:40 AM in response to G_Morales

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];

}

(Xcode) How to connect to a PHP page?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.