Search code examples
iosobjective-ccordovacustom-error-handling

How to crash a cordova ios app


I am trying to write a small sample of cordova ios application. One of my requirements is to provide a button/link to allow user to crash the application.

I have tried to raise exception in CDVUIWebViewNavigationDelegate.m as follows,

- (BOOL)webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
 NSURL* url = [request URL];
if([url.path containsString:@"CRASH"])
{
    NSLog(@"User crash bookmart with NSException");
    NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
    NSDate *current = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle]; // Set date and time styles
    [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
    NSString *currentTime = [dateFormatter stringFromDate:current];
    [userInfo setObject:@"Crash Time" forKey:currentTime];
    NSException *ex = [[NSException alloc] initWithName:@"BookmartCrashException" reason:@"User crashed bookmart!" userInfo:userInfo];
    [ex raise];
}
...
}

But when I tried, I saw following log,

2017-09-04 17:09:57.148 HRent[96124:12077045] User crash bookmart with NSException 2017-09-04 17:09:57.149 HRent[96124:12077045] *** WebKit discarded an uncaught exception in the >webView:decidePolicyForNavigationAction:request:frame:decisionListener: delegate: User crashed bookmart!

The exception has been discarded and app hasn't crash : (

Is there any other way to crash the app for sure? Or with some configuration can I disable WebKit to discard such exception?

Much appreciate for your answers!

Regards Rachel


Solution

  • Try to launch your exception with dispatch on the main thread:

    dispatch_async(dispatch_get_main_queue(), ^{
        NSInteger asd = 5 / 0;    // Simple division by 0
    });
    

    If it works then try NSException approach to have all that extra info.