2017-09-04 90 views
2

我想写一个小样本的科尔多瓦IOS应用程序。我的一个要求是提供一个按钮/链接来允许用户崩溃应用程序。如何崩溃科尔多瓦IOS应用程序

我试图引发异常的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]; 
} 
... 
} 

但是,当我尝试,我看到了下面的日志,

2017年9月4日17:09:57.148 HRent [96124:12077045]用户崩溃与NSException的bookmart 2017-09-04 17:09:57.149 HRent [96124:12077045] *** WebKit在> webView中放弃未捕获的异常:decidePolicyForNavigationAction:request:frame:decisionListener:delegate :用户坠毁了bookmart!

的异常已经被舍弃,应用程序还没有崩溃:?(

是否有任何其他的方式崩溃的应用程序肯定还是有一些配置,我可以禁用的WebKit丢弃这样的异常

非常感激你的答案

问候 雷切尔

+1

如果您提供的应用程序崩溃,则您的应用程序将被从App Store拒绝。为什么你需要这样的功能? – the4kman

+0

我不打算把这个样本发布到真正的市场上。这只是我们产品的样品,它将监测混合动力应用。 – rachelnino

+0

如果你想崩溃的应用程序比试试这个..采取一个空的数组并打印它的objectAtIndex任何值。它会崩溃应用 –

回答

0

尝试与调度启动您的例外在主线程:

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

如果有效,那么尝试使用NSException方法来获取所有额外的信息。

+0

这应该与我的代码提出异常,然后糖果坠毁!:)谢谢Shebuka。我将列出我在下面用作完整解决方案的代码。 – rachelnino

+0

很高兴您找到了解决方案。作为“谢谢”,您可以为所有答案找到解决问题的有用答案;) – Shebuka

+0

我的名声低于15,所以我的投票不能公开显示。 :) – rachelnino

0

您是否像在默认的核心数据实现示例中一样尝试了abort()?它会导致应用程序生成崩溃日志并终止。

+0

这肯定会崩溃应用程序!谢谢Michale! – rachelnino

0

谢谢大家。 我已经尝试过除了Will提出的插件之外的所有建议。总体而言,有2种方式可以使应用程序崩溃。

  1. 正如Michale建议的,使用abort()来终止应用程序。

这里是一块我使用的代码,

- (BOOL)webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType: 
(UIWebViewNavigationType)navigationType 
{ 
    NSURL* url = [request URL]; 
    if([url.path containsString:@"CRASH"]) 
    { 
    abort(); 
    } 
    ... 
} 
  • 作为shebuka商建议的,调度上主线程除外。这里的技巧是,我们不能使用访问nil数组或分割0来引发这个异常,但必须写我在我的问题发布。否则,该应用程序不会崩溃并且不显示日志。
  • 下面是一段代码我用过,

    - (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]; 
        }); 
    
    } ...} 
    

    我会选择解决方案2原因这个崩溃的应用程序与符合我的要求,更好的异常。

    谢谢大家。

    相关问题