Casper522
Casper522

Reputation: 95

ASIHTTPRequest delegation

im using ASIHTTPRequest to call webservice (soap):

-(void)callWebService:(NSString*)URL:(NSString*)SOAP{

    NSURL *url = [NSURL URLWithString:URL];
    NSString *SOAPMessage = [NSString stringWithFormat:SOAP];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];

    request.shouldAttemptPersistentConnection = NO;
    [request setValidatesSecureCertificate:NO];
    [request setRequestMethod:@"POST"];

    [request appendPostData:[SOAPMessage dataUsingEncoding:NSUTF8StringEncoding]];
    [request setDidFinishSelector:@selector(requestCompleted:)];
    [request setDidFailSelector:@selector(requestFailed:)];

    [request setDelegate:self];
    [request startAsynchronous];

}

-(void)requestCompleted:(ASIHTTPRequest * )r{

    NSString *responseString = [r responseString];
    NSLog(@"%@",responseString);

}

-(void)requestFailed:(ASIHTTPRequest * )r{

    NSError *Err = [r error];
    NSLog(@"%@",Err);

}

If i call this in appDelegate.m, it works fine, requestCompleted handler throws the response...But when i use this same code in my own class it throws BAD ACCESS error, which i figured tells me i cannot delegate:self to handle response. if i setDelegate to appdelgate pointer (passed as ID sender) it works (and have handlers there). So why cant my own class handle its own events ? Im new to objective-c so i guess im missing something major here. Thanks

Upvotes: 2

Views: 1045

Answers (3)

user1773493
user1773493

Reputation:

Check if your self is getting released while the delegate call happens. Make sure it is retained properly.

Upvotes: 0

Vinnie
Vinnie

Reputation: 1740

You have to have the requestCompleted and requestFailed in your "own class". Also that class has to live which means it can't be released while the service is being called. You have to save the instance of "your own class" in a strong/retained property or something.

Upvotes: 3

NeverBe
NeverBe

Reputation: 5038

Add this code to dealloc or viewWillDisappear

[request setDelegate:nil];

Upvotes: 0

Related Questions