urlConnection委托在同一个类中处理多个答案

时间:2012-07-16 08:14:30

标签: objective-c delegates urlconnection

我有一个带有2个按钮的ViewController.m文件,用于打开urlConnection,我需要处理响应。 我的问题是当连接来自按钮1(abilita)或button2(登录)时,我在代理中无法理解。 我该怎么办?

-(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];
}

你能用我在connectionDidFinishLoading中理解的方式编写代码吗?

1 个答案:

答案 0 :(得分:3)

采取urlConnection1& urlConnection2作为2个不同的变量,然后在connectionDidFinishLoading或didReceiveResponse方法中进行比较,如下所示:

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

希望它有所帮助...