ユーザーが Instapaper で認証できるアプリがあります。ただし、これを行うには Instapaper のサブスクリプションが必要なので、Instapaper にサブスクライブしていないアカウントでログインしようとすると、エラーを表示したいと考えています。
しかし、ログインしようとすると、AFNetworkingはログインが成功したと見なし、次のエラーをコンソールに表示します。
エラー: エラー Domain=AFNetworkingErrorDomain Code=-1011 "Expected status code in (200-299), got 400" UserInfo=0x8374840 {NSLocalizedRecoverySuggestion=[{"error_code": 1041, "message": "サブスクリプション アカウントが必要です", "type ": "エラー"}]、AFNetworkingOperationFailingURLRequestErrorKey=https://www.instapaper.com/api/1/bookmarks/list>、NSErrorFailingURLKey = https://www.instapaper.com/api/1/bookmarks/list、NSLocalizedDescription =(200-299) の予期されるステータス コード、400 を取得、AFNetworkingOperationFailingURLResponseErrorKey=}
私が使用しているのは、 AFNetworkingの変更である AFXAuthClient だけです。これをサブクラス化して、次のようなカスタム Instapaper API クライアントを作成しました。
#import "AFInstapaperClient.h"
#import "AFJSONRequestOperation.h"
@implementation AFInstapaperClient
+ (AFInstapaperClient *)sharedClient {
static AFInstapaperClient *sharedClient = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedClient = [[AFInstapaperClient alloc] initWithBaseURL:[NSURL URLWithString:@"https://www.instapaper.com/"]
key:@"..."
secret:@"..."];
});
return sharedClient;
}
- (id)initWithBaseURL:(NSURL *)url {
if (self = [super initWithBaseURL:url]) {
[self registerHTTPOperationClass:[AFJSONRequestOperation class]];
[self setDefaultHeader:@"Accept" value:@"application/json"];
}
return self;
}
@end
ログインすると、次のコードが実行されます。
- (IBAction)doneButtonPressed:(UIBarButtonItem *)sender {
[[AFInstapaperClient sharedClient] authorizeUsingXAuthWithAccessTokenPath:@"/api/1/oauth/access_token"
accessMethod:@"POST"
username:self.loginBox.text
password:self.passwordBox.text
success:^(AFXAuthToken *accessToken) {
// Save the token information into the Keychain
[UICKeyChainStore setString:accessToken.key forKey:@"InstapaperKey"];
[UICKeyChainStore setString:accessToken.secret forKey:@"InstapaperSecret"];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Login Successful"
message:@"Your articles are being downloaded now and will appear in your queue."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
[[NSUserDefaults standardUserDefaults] setObject:@"YES" forKey:@"IsLoggedInToInstapaper"];
[self dismissViewControllerAnimated:YES completion:nil];
}
failure:^(NSError *error) {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Login Failed."
message:@"Are you connected to the internet? Instapaper may also be down. Try again later."
delegate:nil
cancelButtonTitle:@"Okay"
otherButtonTitles: nil];
[alert show];
}];
}
しかし、コードが障害ブロックに入ることはありません。Instapaper サブスクリプション アカウントが必要であることを伝えられるように、コードを変更するにはどうすればよいですか?