2011-12-19 145 views
1

我正在構建一個iPhone應用程序,利用RestKit將數據提取/推送到遠程API。此API需要用戶名/密碼認證。我想知道如何處理一個不好的用戶名或密碼,如果用戶輸入一個不工作。所有我能找到是通用RestKit錯誤處理程序不幫我一大堆好:處理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} 

我想知道是否有更好的方法來處理不正確的用戶名或密碼響應?

回答

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

希望這有助於!

+0

if(error.code == -1012)會更簡單 – thanhbinh84 2012-09-17 07:15:22

相關問題