-(IBAction)logIn{
// create the request
NSString *url=[NSString stringWithFormat:@"http://www.dannyflax.comli.com/login.php?user=%@&pw=%@",username.text,password.text];
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
// create the connection with the request
// and start loading the data
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
// Create the NSMutableData that will hold
// the received data
// receivedData is declared as a method instance elsewhere
receivedData=[[NSMutableData data] retain];
} else {
UIAlertView *alertTest = [[UIAlertView alloc]
initWithTitle:@"Error"
message:@"Unable to connect. The server may be down or you may be unable to connect to the internet."
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertTest show];
[alertTest autorelease];
// inform the user that the download could not be made
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// this method is called when the server has determined that it
// has enough information to create the NSURLResponse
// it can be called multiple times, for example in the case of a
// redirect, so each time we reset the data.
// receivedData is declared as a method instance elsewhere
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// append the new data to the receivedData
// receivedData is declared as a method instance elsewhere
[receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error
{
// release the connection, and the data object
[connection release];
// receivedData is declared as a method instance elsewhere
[receivedData release];
// inform the user
UIAlertView *alertTest = [[UIAlertView alloc]
initWithTitle:@"Error"
message:@"Unable to connect. The server may be down or you may be unable to connect to the internet."
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertTest show];
[alertTest autorelease];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// do something with the data
//"\n<script type=\"text/javascript\" src=\"http://analytics.hosting24.com/count.php\"></script>\n<noscript><a href=\"http://www.hosting24.com/\"><img src=\"http://analytics.hosting24.com/count.php\" alt=\"web hosting\" /></a></noscript><!-- End Of Analytics Code -->"
// receivedData is declared as a method instance elsewhere
NSString *data=[[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
data=[data stringByReplacingOccurrencesOfString:@"<!-- www.000webhost.com Analytics Code -->" withString:@""];
data=[data stringByReplacingOccurrencesOfString:@"<script type=\"text/javascript\" src=\"http://analytics.hosting24.com/count.php\"></script>" withString:@""];
data=[data stringByReplacingOccurrencesOfString:@"<noscript><a href=\"http://www.hosting24.com/\"><img src=\"http://analytics.hosting24.com/count.php\" alt=\"web hosting\" /></a></noscript>" withString:@""];
data=[data stringByReplacingOccurrencesOfString:@"<!-- End Of Analytics Code -->" withString:@""];
data=[data stringByReplacingOccurrencesOfString:@"\n" withString:@""];
if([data isEqualToString:@"Success"]){
UIAlertView *alertTest = [[UIAlertView alloc]
initWithTitle:@"Success!"
message:@"Successfully logged in!"
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertTest show];
[alertTest autorelease];
}
else {
UIAlertView *alertTest = [[UIAlertView alloc]
initWithTitle:@"Unable to log in"
message:data
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertTest show];
[alertTest autorelease];
}
// release the connection, and the data object
[connection release];
[receivedData release];
}