简体   繁体   English

如何使Cordova iOS应用程序崩溃

[英]How to crash a cordova ios app

I am trying to write a small sample of cordova ios application. 我正在尝试编写cordova ios应用程序的一小部分示例。 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, 我试图在CDVUIWebViewNavigationDelegate.m中引发异常,如下所示,

- (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! 2017-09-04 17:09:57.148 HRent [96124:12077045]使用NSException的用户崩溃bookmart 2017-09-04 17:09:57.149 HRent [96124:12077045] *** WebKit丢弃了> webView中未捕获的异常: definePolicyForNavigationAction:request:frame:decisionListener:委托:用户崩溃了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? 或者通过某些配置,我可以禁止WebKit丢弃此类异常吗?

Much appreciate for your answers! 非常感谢您的回答!

Regards Rachel 问候雷切尔

Try to launch your exception with dispatch on the main thread: 尝试通过主线程上的dispatch启动您的异常:

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. 如果可行,请尝试使用NSException方法获取所有这些额外信息。

Have you tried abort() as in the default core data implementation example? 您是否像默认的核心数据实现示例中那样尝试过abort()? It causes the application to generate a crash log and terminate. 它使应用程序生成崩溃日志并终止。

The hockeyapp plugin provides functionality that allows you to crash an app. hockeyapp插件提供的功能可让您使应用程序崩溃。 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 查看存储库https://github.com/Microsoft/cordova-plugin-hockeyapp

Thanks Everyone. 感谢大家。 I have tried with all the suggestion except for plugin suggested by Will. 我已经尝试了所有建议,但Will建议的插件除外。 Overall, there are 2 ways to crash the app. 总体而言,有两种方法可以使应用程序崩溃。

  1. As Michale suggested, use abort() to terminate the app. 如Michale所建议,使用abort()终止应用程序。

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. 正如shebuka的建议,在主线程上调度异常。 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. 这里的技巧是我们不能使用访问nil数组或除以0引发此异常,而必须在问题中写我的帖子。 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. 我将选择解决方案2,导致该应用崩溃,但异常情况更适合我的要求。

Thanks everyone. 感谢大家。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM