目标c iPhone重定向到未捕获异常的错误页面

Bac*_*tnz 5 iphone error-handling exception-handling objective-c ios

嗨大家我有一个类我想将用户重定向到错误页面,如果它是:未捕获的异常,捕获的异常或自定义异常.我还想删除错误电子邮件.(以便通知我).

我无法访问此类中的当前View Controller(在未捕获的异常情况下).因为它是用委托监听器触发的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];

}
Run Code Online (Sandbox Code Playgroud)

@结束

Abd*_*mer 1

我从以下位置尝试过:Exception_handler它的工作原理!只需编写您的电子邮件发送代码而不是警报视图。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{        
    [window makeKeyAndVisible];

    NSSetUncaughtExceptionHandler(&exceptionHandler);

    return YES;
}

BOOL exceptionAlertDismissed = FALSE;
void exceptionHandler(NSException *exception)
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"App Committed Suicide"
        message:@"Oh dear, that wasn't supposed to happen. You will have to restart the application... sorry!"
        delegate:[[UIApplication sharedApplication] delegate] cancelButtonTitle:nil otherButtonTitles:@"That's ok!", @"Erm, bye...", nil];
    [alert show];
    [alert release];

    while (exceptionAlertDismissed == FALSE)
    {
        [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    exceptionAlertDismissed = TRUE;
}
Run Code Online (Sandbox Code Playgroud)

在界面中,

@interface ...appDelegate : NSObject <UIApplicationDelegate, UIAlertViewDelegate>
...
void exceptionHandler(NSException *exception);
Run Code Online (Sandbox Code Playgroud)