1

我有一个 ViewController.m 文件,其中有 2 个打开 urlConnection 的按钮,我需要处理响应。我的问题是当连接来自按钮 1(abilita) 或按钮 2(登录) 时,我不明白代表的意思。我能做些什么?

-(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中我能理解的方式编写代码吗?我在处理什么连接?

4

1 回答 1

3

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

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

希望能帮助到你...

于 2012-07-16T09:00:38.867 回答