I'm trying to make a App for iOS using ASIHTTPRequest, but I'm facing some issues using it. To demonstrate my problems, I've uploaded an test-project, which you can download from here: http://uploads.demaweb.dk/ASIHTTPRequestTester.zip.
I've created a WebService class which use the ASIHTTPRequestDelegate
protocol:
#import "WebService.h"
#import "ASIHTTPRequest.h"
@implementation WebService
- (void)requestFinished:(ASIHTTPRequest *)request {
NSLog(@"requestFinished");
}
- (void)requestFailed:(ASIHTTPRequest *)request {
NSLog(@"requestFailed");
}
- (void) testSynchronous {
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
NSLog(@"starting Synchronous");
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSLog(@"got response");
}
}
- (void) testAsynchronous {
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
NSLog(@"starting Asynchronous");
[request startAsynchronous];
}
@end
The synchronous method is working fine, but the asynchronous is not working at all. First the requestFinished and requestFailed was never called and right now I'm getting an EXC_BAD_ACCESS. The two test-methods is called from my ViewController's viewDidLoad
. I hope someone can help me making this work.
EDIT1:
As per this topic, it is possible because of the Automatic Reference Counting
which is enabled for my project. The topic advice to add a [self retain]
, but I cannot do this with ARC turned on. Anyone with a solution for this?
EDIT2: Update as per answer from MrMage.
@interface ViewController()
@property (nonatomic,strong) WebService *ws;
@end
@implementation ViewController
@synthesize ws = _ws;
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
[self setWs:[[WebService alloc] init]];
[self.ws testSynchronous];
[self.ws testAsynchronous];
}
@end