2013-05-03 56 views
0

我想在應用程序關閉前顯示玻璃破碎動畫。我管理的應用程序設置的ExceptionHandler是否有可能爲iOS創建自定義崩潰動畫?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    // Override point for customization after application launch. 
    NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler); 
    return YES; 
} 

void uncaughtExceptionHandler(NSException *exception) { 

    UIWindow *lastWindow = [[UIApplication sharedApplication].windows lastObject]; 
    UIGraphicsBeginImageContext(lastWindow.bounds.size); 
    [lastWindow.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *pngImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    NSData * data = UIImagePNGRepresentation(pngImage); 
} 

關閉之前捕獲屏幕截圖但後來我甚至不能簡單的圖像添加到視圖,一次抽取週期之前的應用程序崩潰。應用程序關閉之前是否可以在屏幕上顯示任何內容?

+0

粘貼你的錯誤日誌 – Anil 2013-05-03 12:23:33

+0

重複的問題,下次在發佈問題之前使用搜索:http://stackoverflow.com/questions/1787254/showing-an-alert-in-an-iphone-top-level-異常處理程序 – 2013-05-03 13:07:51

回答

1

好吧,不知道如果這會幫助你,但在我的應用程序中,我已經設法顯示一個UIAlertView用戶解釋崩潰,異常類型,其描述和堆棧跟蹤(全部使用NSSetUncaughtExceptionHandler方法),像這樣:

Sample in spanish

然後我提供的查殺程序或繼續儘管該應用程序可能是不穩定的推薦選項。在我的情況下,它部分影響了應用程序的功能,所以在大多數情況下,用戶可以保存它的工作並安全地關閉應用程序。

如果你想我可以編輯答案並在這裏發佈代碼(我將不得不搜索我的Xcode項目文件夾,這就是爲什麼我沒有發佈它)。

編輯:

在AppDelegate中委託的方法willFinishLaunchingWithOptions我設置NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);

然後在我創建的處理方法如下:

static void uncaughtExceptionHandler(NSException *exception) 
{ 
    [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"kDisculpe", nil) message:[NSString stringWithFormat:@"%@ %@%@ %@%@ %@", NSLocalizedString(@"kErrorText", nil), [exception name], NSLocalizedString(@"kErrorDescripcion", nil), [exception reason], NSLocalizedString(@"kErrorTrazaPila", nil), [exception callStackReturnAddresses]] delegate:[[UIApplication sharedApplication] delegate] cancelButtonTitle:NSLocalizedString(@"kContinuar", nil) otherButtonTitles:NSLocalizedString(@"kSalir", nil), nil] show]; 
    [[NSRunLoop currentRunLoop] run]; 
} 

然後在AlertView的委託方法clickedButtonAtIndex我設置:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if ([[alertView title] isEqualToString:NSLocalizedString(@"kDisculpe", nil)]) { 
     switch (buttonIndex) { 
      case 0: 
       if ([[alertView title] isEqualToString:NSLocalizedString(@"kDisculpe", nil)]) { 
        [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"kAdvertencia", nil) message:NSLocalizedString(@"kAppContinuaraInestable", nil) delegate:[[UIApplication sharedApplication] delegate] cancelButtonTitle:NSLocalizedString(@"kContinuar", nil) otherButtonTitles:nil] show]; 
       } 
       break; 
      case 1: 
       exit(0); 
       break; 
     } 
    } 
} 

請注意,我所做的唯一重要的事情是[[NSRunLoop currentRunLoop] run];我希望這會對您有所幫助。

+0

代碼將有所幫助,謝謝! – tadasz 2013-05-03 23:50:18

3

一旦你觸發了一個異常,你的應用程序的狀態是未定義的;沒有人知道你的應用程序在做什麼,並且有很多事情可能會出錯(請參閱上面的鏈接),但我將在此着重討論的是數據損壞。

一些崩潰記錄器試圖在程序終止時立即通過網絡提交崩潰報告;在你的情況下,你試圖讓應用程序繼續運行並顯示一條消息,它具有相同的效果:保持應用程序的運行意味着應用程序自己的代碼的執行,然後應用程序可以自由嘗試寫入可能損壞的用戶數據。

考慮一個以數據爲核心的應用程序,其中一個模型對象進行更新,然後保存:

person.name = name; 
person.age = age; // an exception occurs here 
person.birthday = birthday; 
[context save: NULL]; 

在崩潰之時,被管理對象上下文包含了部分更新的記錄 - 當然不是你想保存到數據庫的東西。但是,如果未捕獲的異常處理程序繼續執行,應用程序中的任何網絡連接,計時器或其他待執行的runloop分派也將運行。如果從runloop派發的任何應用程序代碼包含對 - [NSManagedObjectContext save:]的調用,則會向數據庫寫入部分更新的記錄,從而破壞用戶的數據。

當您的應用程序處於未知/非確定狀態時,最安全的事情就是退出。

+0

問題不在於應用程序崩潰時執行某些操作,我可以執行諸如將照片保存到內存等功能。但是,我希望使崩潰變得美麗 - 而不是僅僅關閉我想顯示動畫,如Screen Glass Shatter;) – tadasz 2013-05-03 23:47:03

相關問題