Reputation: 46218
I am using AFNetworking in my project and I simply need to check if a URL is successful (status 2xx).
I tried
NSURLRequest *request = [NSURLRequest requestWithURL:url2check];
AFJSONRequestOperation *operation = [AFJSONRequestOperation
JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
if (response.status == 200) {
NSLog(@"Ok %@", JSON);
}
}
failure:nil
];
[operation start];
But as I'm not awaiting JSON especially, I was expecting to use something like
AFHTTPRequestOperation *operation = [AFHTTPRequestOperation
HTTPRequestOperationWithRequest: success: failure:];
But this does not exist (I don't know why), so I used
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]
initWithRequest:request];
[operation setCompletionBlockWithSuccess:...]
Isn't there simpler ?
How would you do this simple URL check with AFNetworking ?
Upvotes: 2
Views: 4073
Reputation: 46218
Here is what I ended up with
NSURLRequest *request = [NSURLRequest requestWithURL:url2check];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]
initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation
, id responseObject) {
// code
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// code
}
];
[operation start];
Feel free to add a simpler way.
Upvotes: 2
Reputation: 52237
You just could instantiate a AFHTTPClient object with the base URL and call getPath:parameters:success:failure:
on it.
Creates an AFHTTPRequestOperation with a GET request, and enqueues it to the HTTP client’s operation queue.
Upvotes: 0