主に、レスポンダーチェーンを利用してエラーを上に転送したいため、UIKit で AppKit のエラー処理メカニズムをミラーリングしようとしています。これを完全にテストしたわけではありませんが、現時点では以下のようになっています。
これは AppKit をかなり厳密に反映していますが、will/did フックをオーバーライドして、それぞれカスタム エラー表示と回復を実行できます。デフォルトの動作は、プレゼンテーション用に UIAlertView を表示し、回復用に psuedo-NSErrorRecoveryAttempting オブジェクトを使用することです。
@implementation UIResponder (ErrorHandling)
- (void)presentError:(NSError *)error
completion:(void (^)(BOOL recovered))completion
{
if (nil == (error = [self willPresentError:error])) {
return;
}
if (self.nextResponder) {
[self.nextResponder presentError:error completion:completion];
return;
}
// Code to create and show UIAlertView
// e.g. https://github.com/jayway/CWUIKit/blob/master/Classes/UIAlertView%2BCWErrorHandler.m
// The UIAlertViewDelegate calls didPresentError...
}
/*
Override to customise the error object as in AppKit.
You can also perform your own error presentation, and return nil to terminate the default handling.
Custom error presentation UI should still call didPresentError... when dismissed
*/
- (NSError *)willPresentError:(NSError *)error
{
return error;
}
/*
Override to perform custom error recovery.
*/
- (void)didPresentError:(NSError *)error optionIndex:(NSInteger)optionIndex completion:(void (^)(BOOL recovered))completion
{
id recoveryAttempter = [error recoveryAttempter];
if ([recoveryAttempter respondsToSelector:@selector(attemptRecoveryFromError:optionIndex:completion:)]) {
[recoveryAttempter attemptRecoveryFromError:error optionIndex:optionIndex completion:completion];
}
}
@end