0

I am trying to make a registration system for the iphone. Everything works fine and the server return the message after it checks if the user already exist. The only problem is that the alert view is not shown when the string returned is equal to this one: enter image description here

This is the code:

PHP

//other stuff here 
//verify if user already in database

$verify = "SELECT userid 
                FROM user 
                WHERE userid=:user LIMIT 1;";


$verifyQry = $db_conn->prepare($verify);
$verifyQry->bindParam(':user', $user);
$verifyQry->execute();
$userExists = $verifyQry->fetch();


//if user not in the database, add!
if (!$userExists) {
$insert = 'INSERT INTO user(`userid`,`password`) VALUES (:user,:password)';
$qry = $db_conn->prepare($insert);
$qry->bindParam(':user', $user);
$qry->bindParam(':password', $pass);
$qry->execute();
$message = "success";

}else{
$message = "userExist"; 

}


echo utf8_encode($message);

XCODE

-(void)signUpWithUsername:(NSString *)username andWithPassword:(NSString *)password{

    //NSString *token = [[NSUserDefaults standardUserDefaults]
        //               objectForKey:@"tokenId"];
    dispatch_async(kBgQueue, ^{



        NSString *rawStr = [NSString stringWithFormat:@"username=%@&password=%@", username,
                            password];

        NSData *data = [rawStr dataUsingEncoding:NSUTF8StringEncoding];

        NSURL *url = [NSURL URLWithString:@"http://www.myserver.com/api/registerUser.php"];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

        [request setHTTPMethod:@"POST"];
        [request setHTTPBody:data];
        NSURLResponse *response;
        NSError *err;
        NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

        NSString *responseString = [NSString stringWithUTF8String:[responseData bytes]];
        NSLog(@"%@", responseString);// I GOT THIS STRING FROM THE SERVER I


        if ([responseString  isEqualToString: @"userExist"]) {
            UIAlertView *userExistAlert = [[UIAlertView alloc]initWithTitle:@"User Exist" message:@"A user with this email address has already sign up. Did you forgot your password" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"retrive password", nil];

            [userExistAlert show];
        }else{


        }


        [self dismissViewControllerAnimated:YES completion:nil]; // Dismiss the viewController upon success


    });




}
4

2 回答 2

0

最好的猜测是,您从服务器收到的数据并不是认为的原因。它可能有前导和/或尾随空格或换行符。

为确保没有隐藏的周围字符,请使用:

responseString = [responseString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];];

代替:

NSLog(@"%@", responseString);// I GOT THIS STRING FROM THE SERVER I

利用:

NSLog(@"response string from server: '@'", responseString);

我还将 NSLogresponseData以确保编码是预期的并且没有隐藏字符,例如换行符:

NSLog(@"response data from server: @", responseData);

也代替:

NSString *responseString = [NSString stringWithUTF8String:[responseData bytes]];

利用:

NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding

当我遇到这样的问题时,我会使用 Charles Proxy 来准确查看服务器发送的内容。

于 2013-11-07T14:56:20.620 回答
0

问题是没有显示 alertView 但我能够检查从服务器返回的字符串是否等于“userExist”。我使用以下方法修复了它:

- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait

如果用户已经存在,这是现在显示警报视图的代码:

-(void)signUpWithUsername:(NSString *)username andWithPassword:(NSString *)password{

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{



        NSString *rawStr = [NSString stringWithFormat:@"username=%@&password=%@", username,
                            password];

        NSData *data = [rawStr dataUsingEncoding:NSUTF8StringEncoding];

        NSURL *url = [NSURL URLWithString:@"http://www.myserver.com/api/registerUser.php"];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

        [request setHTTPMethod:@"POST"];
        [request setHTTPBody:data];
        NSURLResponse *response;
        NSError *err;
        NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

        NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

        responseString = [responseString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

        NSLog(@"response string from server: '%@'", responseString);
        NSString *userExist = @"userExist";
        [userExist dataUsingEncoding:NSUTF8StringEncoding];
        if ([responseString  isEqualToString: userExist]) {

            [self performSelectorOnMainThread:@selector(showAlertView)
                                   withObject:responseString waitUntilDone:YES];


        }else{
            NSLog(@"User doesn't exist");

        }
    });
}


-(void)showAlertView{

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"User exiat" message:@"user exist" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:nil, nil ];
    [alert show];

}
于 2013-11-09T05:40:40.380 回答