我调用了服务器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];
}
答案 0 :(得分:0)
如果我正确理解您的问题,您希望阻止用户输入空白密码和用户名。
您可以在创建NSURLConnection
之前检查登录方法-(void)login:(id)sender
{
NSString *uname=usrname.text;
NSString *pword=password.text;
if([uname length]>0 && [pword length]>0)
{
NSURL *theURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://myserver.net/projects/mobile/database_connection.php?name=%@&password=%@",uname, pword]]; //Here you place your URL Link
NSURLRequest *req = [NSURLRequest requestWithURL:theURL];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:req delegate:self];
if (connection) {
NSLog(@"connection successful");
}
else {
NSLog(@"Failed");
}
}
}
如果用户未在用户名和密码中输入任何对象,请不要创建NSURLConnection
对象。
最好的方法是禁用登录按钮,直到用户输入用户名和密码。
修改强>
使用[connection start]
启动NSURLConnection。
浏览NSURLConnection文档,您会更好地理解。