-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 に答える