Connor Grady
Connor Grady

Reputation: 161

Handling RestKit Authentication Challenge

I'm building an iPhone App leveraging RestKit to pull/push data to a remote API. This API requires a username/password authentication. I'm wondering how to handle a bad username or password if the user were to input one that didn't work. All I've been able to find is the generic RestKit error handler which doesn't do me a whole lot of good:

- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error

With an incorrect username or password, this will give me:

MyApp iPhone[4758:fb03] W restkit.network:RKResponse.m:157 Failed authentication challenge after 1 failures
MyApp iPhone[4758:fb03] E app:iPhoneSignIn.m:720 Hit error: Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn’t be completed. (NSURLErrorDomain error -1012.)" UserInfo=0xd964b80 {NSErrorFailingURLKey=https://my.example.com/api/v1.0/?format=json, NSErrorFailingURLStringKey=https://my.example.com/api/v1.0/?format=json}

I was wondering if there was a better way to handle an improper username or password response?

Upvotes: 1

Views: 988

Answers (1)

Connor Grady
Connor Grady

Reputation: 161

For anyone still searching for the answer, I figured it out. Basically my code below scans the error response for "-1012" which is the error code that NSURL gives for an authentication failure. Here's the code:

- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error {
    NSRange range = [[error localizedDescription] rangeOfString:@"-1012"];
    if (range.length > 0){
        //Do whatever here to handle authentication failures
    }
    RKLogError(@"Hit error: %@", error);
}

Hope this helps!

Upvotes: 4

Related Questions