2

MattGallagherのグローバル例外ハンドラーをプロジェクトの1つに追加しようとしています。次の場所にある彼のサンプルプロジェクトを実行しています。

http://cocoawithlove.com/2010/05/handling-unhandled-exceptions-and.html

[終了]を押してもアプリが終了しないという問題が発生しました。アプリに戻るだけです。kill()呼び出しでアプリを強制終了しようとしましたが、アプリを終了させることができません。

alertviewからのコールバックは、Continueの場合のみを処理するようであり、アプリの強制終了は処理しません。

- (void)alertView:(UIAlertView *)anAlertView clickedButtonAtIndex:(NSInteger)anIndex
{
   if (anIndex == 0)
   {
        dismissed = YES;
   }
}

私はアプリの性質上、アプリを終了できないことを知っていますが、この場合、アプリがクラッシュした場合は、ユーザーに[終了]ボタンを押してアプリを終了してもらいたいと思います。

ありがとう!

4

1 に答える 1

5

Appleは終了ボタンを信じていません。ただし、キャッチしない別の例外をスローしてアプリをクラッシュさせることもできますが、アプリがクラッシュすると承認されません。

info.plistでUIApplicationExitsOnSuspendをtrueに設定し、ホームボタンを押すと、アプリが終了します。その場合、終了ボタンを他のアプリへのリンクにすることができます。

常に例外を発生させるようにifステートメントを変更すると、アプリがクラッシュして終了します。

- (void)handleException:(NSException *)exception
{
    [self validateAndSaveCriticalApplicationData];

    UIAlertView *alert =
        [[[UIAlertView alloc]
            initWithTitle:NSLocalizedString(@"Unhandled exception", nil)
            message:[NSString stringWithFormat:NSLocalizedString(
                @"You can try to continue but the application may be unstable.\n\n"
                @"Debug details follow:\n%@\n%@", nil),
                [exception reason],
                [[exception userInfo] objectForKey:UncaughtExceptionHandlerAddressesKey]]
            delegate:self
            cancelButtonTitle:NSLocalizedString(@"Quit", nil)
            otherButtonTitles:NSLocalizedString(@"Continue", nil), nil]
        autorelease];
    [alert show];

    CFRunLoopRef runLoop = CFRunLoopGetCurrent();
    CFArrayRef allModes = CFRunLoopCopyAllModes(runLoop);

    while (!dismissed)
    {
        for (NSString *mode in (NSArray *)allModes)
        {
            CFRunLoopRunInMode((CFStringRef)mode, 0.001, false);
        }
    }

    CFRelease(allModes);

    NSSetUncaughtExceptionHandler(NULL);
    signal(SIGABRT, SIG_DFL);
    signal(SIGILL, SIG_DFL);
    signal(SIGSEGV, SIG_DFL);
    signal(SIGFPE, SIG_DFL);
    signal(SIGBUS, SIG_DFL);
    signal(SIGPIPE, SIG_DFL);

    [exception raise];
}
于 2010-12-03T22:44:56.097 に答える