我有一个类,如果存在以下情况,我想将用户重定向到错误页面:未捕获的异常、捕获的异常或自定义异常。我还想关闭一封错误电子邮件。(以便我收到通知)。
我无法访问此类中的当前视图控制器(在未捕获异常的情况下)。因为它是由委托侦听器触发的onUncaughtException(NSException* exception)
。
如何访问当前的视图控制器,或者如果失败,以模式将用户重定向到错误视图控制器?
#import "ErrorHelper.h"
#import "ErrorViewController.h"
#import "Global.h"
#import "AppDelegate.h"
@implementation ErrorHelper
+(void) handleUncaughtError:(NSException*) exception
{
NSLog(@"Uncaught exception occurred!");
[self sendErrorEmailIfAppropriate:exception :nil];
[self redirectToErrorPage];
}
+(void) handleCaughtError:(NSException*) exception
{
NSLog(@"Error caught!!");
[self sendErrorEmailIfAppropriate:exception :nil];
[self redirectToErrorPage];
}
+(void) handleCaughtCustomError:(NSString*) ref :(NSString*) details
{
NSLog(@"Custom error caught!!");
//can do conditional branching on @ref, to do appropriate action.
[self sendErrorEmailIfAppropriate:nil :details];
[self redirectToErrorPage];
}
+(void) sendErrorEmailIfAppropriate:(NSException*) exception :(NSString*)details
{
if([[Global get] isInTestMode] || [[Global get] isInLiveMode]) {
bool isCustomException = details != nil;
}
}
+(void) redirectToErrorPage
{
/*Try redirect user to error page
E.G:
-There's been an error, our App Dev team will try and resolve the issue for you
-Regards -Dev team
*/
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
}
@结尾