-1

我调用服务器 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];
}
4

1 回答 1

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如果用户没有在用户名和密码中输入任何内容,则不要创建对象。

最好的方法是禁用登录按钮,直到用户输入用户名和密码。

编辑:

使用 启动 NSURLConnection [connection start]

浏览NSURLConnection文档,你会更好地理解。

于 2013-09-05T09:24:08.477 回答