Both NSURLRequest
and NSURLResponse
both have a URL
property, but comparing them using -isEqual:
may not give you the desired result. Here's a category on NSURL
that will help:
@interface NSURL (Equivalence)
- (BOOL)isEquivalent:(NSURL *)aURL;
@end
@implementation NSURL (Equivalence)
- (BOOL)isEquivalent:(NSURL *)aURL
{
if ([self isEqual:aURL]) return YES;
if ([[self scheme] caseInsensitiveCompare:[aURL scheme]] != NSOrderedSame) return NO;
if ([[self host] caseInsensitiveCompare:[aURL host]] != NSOrderedSame) return NO;
if ([[self path] compare:[aURL path]] != NSOrderedSame) return NO;
if ([[self port] compare:[aURL port]] != NSOrderedSame) return NO;
if ([[self query] compare:[aURL query]] != NSOrderedSame) return NO;
return YES;
}
@end
Import that category, and you could do something like the following with AFNetworking:
[self getPath:@"somePath" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
if ([operation.request.URL isEquivalent:operation.response.URL]) {
// they match
} else {
// there was a redirect
}
} failure:nil];
EDIT: I've long since incorporated NSURL+Equivalence
into my standard toolbox, but credit belongs to danh and this excellent SO answer.