Luca Robusto
Luca Robusto

Reputation: 11

urlConnection Delegate to handle multiple answer in the same class

I have a ViewController.m file with 2 buttons that open a urlConnection, and I need to handle response. My problem is I don't understand in the delegate when a connection comes from button 1(abilita) or button2(login). what can I do?

-(IBAction)abilita {

    NSString *usernameEncoded = [username.text urlencode];
    NSString *passwordEncoded = [password.text urlencode];
    NSString *mittenteEncoded = [abilitaField.text urlencode];
    NSString *code=@"abcdef";

    NSString * address = [NSString stringWithFormat:@"http://www.xxx.net/smsweb/setsender/recsender.php?login=%25%40&password=%25%40&codice%@",usernameEncoded, passwordEncoded,code];

    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:address]];
    urlConnectionRecsender=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
}


- (IBAction)login {
    NSString * address = [NSString stringWithFormat:@"http://www.xxx.net/smsscript/sendsms.php?login=%25%40&password=%25%40&tipo=2",usernameEncoded, passwordEncoded];

    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:address]];
    urlConnectionLogin=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
}

//DELEGATE:

- (void)connection:(NSURLConnection *)conn didReceiveResponse:(NSURLResponse *)response
{
      mutableData = [NSMutableData data] ;
      if ([conn isEquals:urlConnectionRecsender]){
             // this is request urlConnectionRecsender
      }
      if ([conn isEquals:urlConnectionLogin]){
             // this is request urlConnectionLogin
      }
}

- (void)connectionDidFinishLoading:(NSURLConnection *)conn
{
    NSMutableString *stringData = [[NSMutableString alloc] initWithData:mutableData encoding: NSASCIIStringEncoding];
}

Can you write the code in the way I can understand in connectionDidFinishLoading what connection am I handling?

Upvotes: 1

Views: 687

Answers (1)

Obaid Maroof
Obaid Maroof

Reputation: 1579

take urlConnection1 & urlConnection2 as 2 different variables and then compare them in the connectionDidFinishLoading or didReceiveResponse method as follow:

if ([conn isEquals:urlConnection1]){
    // First urlConnection
}
if ([conn isEquals:urlConnection2]){
    // Second urlConnection
}

Hope it helps...

Upvotes: 3

Related Questions