2

A user uses an internet connection where he is redirected to an internet providers login page.

Running a request before he has logged in will give you html data from the provider and data from the address that you originally requested data from.

Is there a way to check if the data address that you request data from is the same address that you get the response from?

i.e.

Is there a way to get the response URL?

AlexanderN
  • 2,610
  • 1
  • 26
  • 42

1 Answers1

1

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.

Community
  • 1
  • 1
followben
  • 9,067
  • 4
  • 40
  • 42