目标C iPhone重定向到错误页面,以获取例外



我有一个我想将用户重定向到错误页面的类,如果有一个:未被发达的例外,被捕获的异常或自定义异常。我也想删除一封错误电子邮件。(以便我通知)。

我无法在此类中访问当前视图控制器(如果是未被发见的例外)。因为它是由代表侦听器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];
    
}

@end

您应该假设可可异常是不可回复的。不要扔它们,不要将它们添加到您的程序中。(尽管在这方面混合C 和OBJC可能不安全,但使用C 中的异常是可以的

有一些奇怪的OBJC API,您可以从其中一些恢复。如果可能,请使用不投掷的替代方案。如果您必须在这种情况下尝试/捕获,则应使您的处理程序尽可能地为本地呼叫 。假设未被发现的OBJC例外或被高级处理程序捕获的例外是无法恢复的。

现在,由于您抓取的异常是呼叫者本地的,因此您可以轻松地添加逻辑以将错误传播到视图控制器或重新回到事件循环中(例如,AlertView)。是的,在这种情况下,您可能会有一些错误参数或返回值。

电子邮件是一个单独的问题。

我尝试了以下操作: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;
}

在接口中,

@interface ...appDelegate : NSObject <UIApplicationDelegate, UIAlertViewDelegate>
...
void exceptionHandler(NSException *exception);

如果您真的想实现这一目标,则可以尝试类似的东西

1-将当前屏幕对象存储在uiviewController对象(应用程序级别)中。

2-这将为您提供当前屏幕。

(我没有测试过,但我想应该有效)

3-发送通知(BCOZ现在您有当前的屏幕实例)。

4-显示您的错误页面

请参阅在iPhone顶级异常处理程序中显示有关如何安装全局异常处理程序的信息。但是,正如贾斯汀所说,应将例外视为"致命" - - 您可以打开一个URL/显示WebView

但不要尝试恢复正常操作

Main.m类使用 -

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = -1;
@try {
    retVal = UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
@catch (NSException* exception) {
    NSLog(@"Uncaught exception: %@", exception.description);
    --Send email here or Open ur link or Wat ever action u'd like to do or Use the Error helper class of urs HERE--
}
[pool release];
return retVal;

这个想法是,由于任何对象造成的所有崩溃都将最终通过Main.M类,这是整个应用程序生命周期中崩溃的最佳地点。

最新更新