I have used below code in my application of User Registration to interact with remote database.
-(IBAction)checkFirstName:(UITextField*)textField
{
NSURL *url = [NSURL URLWithString:@"http://mysite.com"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue: firstName.text forKey:@"firstName"];
[request setDelegate:self];
[request didReceiveDataSelector];
[request startAsynchronous];
firstName.rightViewMode = UITextFieldViewModeAlways;
if (receivedData==0)
{
UIImage *image = [UIImage imageNamed:@"close.png"];
[button setImage:image forState:UIControlStateNormal];
firstName.rightView = button;
}
}
‘firstName’ is the textfield value which i used to take input from user.
I used the below method to receive response from database
-(void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data
{
receivedData = [request responseData];
[receivedData appendData:data];
receivedString = [[NSString alloc] initWithData:receivedData
encoding:NSUTF8StringEncoding];
}
‘receivedData’ is the NSData object, declared globally. I have to get the ‘responseData’ as either 0 or 1, which will be stored in a variable ‘receivedData’.
- How to check the value of ‘receivedData’ object (0 or 1)?.
- checkFirstName: action will be fired on uitextfield Editing DidEnd event.
Can we use responseData==0 ? Please, suggest me with a solution…
Waiting for an urgent response..
First, you are going to need to rethink the way your methods work.
[request didReceiveDataSelector];returns the method name that is called when the request receives data. Since you are not setting this value to a variable, this line does nothing.if (receivedData==0)won’t do what you want it to do. The request runs asynchronously, so the value ofreceivedDatawill not ever reflect what happens in the request you started 2 lines previously.receivedDatais null? If so, say this:if (receivedData == nil), it makes it clear what you’re trying to do here.request:didReceiveDatamethod it looks like you are getting the data from the request and appending it onto itself (I’m not sure what theresponseDataproperty looks like when you access it before the request is finished. It could be null). Whatever is happening here, I’m pretty sure it’s not what you wanted to happen.Finally, unless you really want to aggregate and parse the incoming data yourself (you probably don’t) you should impliment these two methods instead of the
request:didReceiveDatamethod.They’re called after the request is completely done. Access a string value for the server response by calling
Only after your
requestFinishedmethod is called can you set the value of your first nameUITextField.And one bit of advice: it’s rare that you need to use global variables in Obj-C and you certainly do not need to use them here. I’d get rid of those ASAP.
I made a lot of assumptions about what your intent was, so leave a comment if this isn’t what you wanted. Good luck!