我调用服务器 url 数据库连接来识别用户名和密码。我使用了 NSURL。如果我输入错误的用户名和密码,它会在 NSLog 中成功显示连接。如果我输入正确的用户名和密码,它会显示连接成功。如果我没有输入用户名和密码 UITextField 然后单击登录按钮它显示连接成功。我只需要为正确的用户名和密码工作
代码:
-(void)login:(id)sender{
NSString *uname=usrname.text;
NSString *pword=password.text;
NSURL *theURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://myserver.net/projects/mobile/database_connection.php?name=%@&password=%@",uname, pword]]; //Here you place your URL Link
NSLog(@"url is %@",theURL);
NSURLRequest *req = [NSURLRequest requestWithURL:theURL];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:req delegate:self];
if (connection) {
NSLog(@"connection successful");
}
else {
NSLog(@"Failed");
}
}
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
//NSLog(@"Did Receive Data %@", data);
[receiveData appendData:data];
}
-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
[receiveData setLength:0];
}
-(void) connectionDidFinishLoading:(NSURLConnection *)connection{
NSLog(@"recieved data lenght is %d", [receiveData length]);
}
- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
{
NSLog(@"%@" , error);
}
// Implement TextField delegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
-(void)touchesBegan :(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
}