How to crash a cordova ios app

767 views Asked by At

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

4

There are 4 answers

3
Shebuka On BEST ANSWER

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.

1
Kalle On

Have you tried abort() as in the default core data implementation example? It causes the application to generate a crash log and terminate.

0
rachelnino On

Thanks Everyone. I have tried with all the suggestion except for plugin suggested by Will. Overall, there are 2 ways to crash the app.

  1. As Michale suggested, use abort() to terminate the app.

Here is the piece of code I used,

- (BOOL)webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:
(UIWebViewNavigationType)navigationType
{
   NSURL* url = [request URL];
   if([url.path containsString:@"CRASH"])
   {
     abort();
   }
   ...
 } 
  1. As shebuka's suggested, dispatch the exception on main thread. The trick here is that we can not use accessing nil array or dividing 0 to raise this exception but have to write I post in my question. Otherwise, the app won't crash and no log shown.

Here is the code piece I used,

- (BOOL)webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:
(UIWebViewNavigationType)navigationType
{
NSURL* url = [request URL];
if([url.path containsString:@"CRASH"])
{
    dispatch_async(dispatch_get_main_queue(), ^{
        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];
    });

} ...}

I am going to choose solution 2 cause this crashes app with an exception which fits my requirement better.

Thanks everyone.

0
Will On

The hockeyapp plugin provides functionality that allows you to crash an app. You can use this as a temporary solution until you figure out the problem.

hockeyapp.forceCrash():void

Check out the repository https://github.com/Microsoft/cordova-plugin-hockeyapp