处理RestKit身份验证挑战

时间:2011-12-19 21:01:32

标签: iphone ios restkit

我正在构建一个利用RestKit将数据拉/推送到远程API的iPhone应用程序。此API需要用户名/密码身份验证。如果用户要输入一个不起作用的用户名或密码,我想知道如何处理错误的用户名或密码。所有我能找到的是通用的RestKit错误处理程序,它对我没有多大帮助:

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

如果用户名或密码不正确,我会:

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}

我想知道是否有更好的方法来处理不正确的用户名或密码响应?

1 个答案:

答案 0 :(得分:4)

对于仍在寻找答案的人,我想出来了。基本上我的代码扫描错误响应“-1012”,这是NSURL为验证失败提供的错误代码。这是代码:

- (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);
}

希望这有帮助!