如何让IOS应用从容地崩溃

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何让IOS应用从容地崩溃相关的知识,希望对你有一定的参考价值。

ios SDK中提供了一个现成的函数 NSSetUncaughtExceptionHandler 用来做异常处理,但功能非常有限,而引起崩溃的大多数原因如:内存访问错误,重复释放等错误就无能为力了,因为这种错误它抛出的是Signal,所以必须 要专门做Signal处理。首先定义一个UncaughtExceptionHandler类,.h头文件的代码如下:

#import <UIKit/UIKit.h>
@interface UncaughtExceptionHandler : NSObject

BOOL dismissed;

@end
void InstallUncaughtExceptionHandler();
然后在.mm文件实现InstallUncaughtExceptionHandler(),如下:
void InstallUncaughtExceptionHandler()

signal(SIGABRT, MySignalHandler);
signal(SIGILL, MySignalHandler);
signal(SIGSEGV, MySignalHandler);
signal(SIGFPE, MySignalHandler);
signal(SIGBUS, MySignalHandler);
signal(SIGPIPE, MySignalHandler);


这样,当应用发生错误而产生上述Signal后,就将会进入我们自定义的回调函数MySignalHandler。为了得到崩溃时的现场信息,还可以加入一些获取CallTrace及设备信息的代码,.mm文件的完整代码如下:

#import "UncaughtExceptionHandler.h"
#include <libkern/OSAtomic.h>
#include <execinfo.h>
NSString * const UncaughtExceptionHandlerSignalExceptionName = @"UncaughtExceptionHandlerSignalExceptionName";
NSString * const UncaughtExceptionHandlerSignalKey = @"UncaughtExceptionHandlerSignalKey";
NSString * const UncaughtExceptionHandlerAddressesKey = @"UncaughtExceptionHandlerAddressesKey";
volatile int32_t UncaughtExceptionCount = 0;
const int32_t UncaughtExceptionMaximum = 10;
const NSInteger UncaughtExceptionHandlerSkipAddressCount = 4;
const NSInteger UncaughtExceptionHandlerReportAddressCount = 5;
@implementation UncaughtExceptionHandler
+ (NSArray *)backtrace

void* callstack[128];
int frames = backtrace(callstack, 128);
char **strs = backtrace_symbols(callstack, frames);
int i;
NSMutableArray *backtrace = [NSMutableArray arrayWithCapacity:frames];
for (
i = UncaughtExceptionHandlerSkipAddressCount;
i < UncaughtExceptionHandlerSkipAddressCount +
UncaughtExceptionHandlerReportAddressCount;
i++)

[backtrace addObject:[NSString stringWithUTF8String:strs[i]]];

free(strs);
return backtrace;

- (void)alertView:(UIAlertView *)anAlertView clickedButtonAtIndex:(NSInteger)anIndex

if (anIndex == 0)

dismissed = YES;


- (void)handleException:(NSException *)exception

UIAlertView *alert =
[[[UIAlertView alloc]
initWithTitle:NSLocalizedString(@"Unhandled exception", nil)
message:[NSString stringWithFormat:NSLocalizedString(
@"You can try to continue but the application may be unstable.\\n"
@"%@\\n%@", nil),
[exception reason],
[[exception userInfo] objectForKey:UncaughtExceptionHandlerAddressesKey]]
delegate:self
cancelButtonTitle:NSLocalizedString(@"Quit", nil)
otherButtonTitles:NSLocalizedString(@"Continue", nil), nil]
autorelease];
[alert show];
CFRunLoopRef runLoop = CFRunLoopGetCurrent();
CFArrayRef allModes = CFRunLoopCopyAllModes(runLoop);
while (!dismissed)

for (NSString *mode in (NSArray *)allModes)

CFRunLoopRunInMode((CFStringRef)mode, 0.001, false);


CFRelease(allModes);
NSSetUncaughtExceptionHandler(NULL);
signal(SIGABRT, SIG_DFL);
signal(SIGILL, SIG_DFL);
signal(SIGSEGV, SIG_DFL);
signal(SIGFPE, SIG_DFL);
signal(SIGBUS, SIG_DFL);
signal(SIGPIPE, SIG_DFL);
if ([[exception name] isEqual:UncaughtExceptionHandlerSignalExceptionName])

kill(getpid(), [[[exception userInfo] objectForKey:UncaughtExceptionHandlerSignalKey] intValue]);

else

[exception raise];


@end
NSString* getAppInfo()

NSString *appInfo = [NSString stringWithFormat:@"App : %@ %@(%@)\\nDevice : %@\\nOS Version : %@ %@\\nUDID : %@\\n",
[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"],
[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"],
[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"],
[UIDevice currentDevice].model,
[UIDevice currentDevice].systemName,
[UIDevice currentDevice].systemVersion,
[UIDevice currentDevice].uniqueIdentifier];
NSLog(@"Crash!!!! %@", appInfo);
return appInfo;

void MySignalHandler(int signal)

int32_t exceptionCount = OSAtomicIncrement32(&UncaughtExceptionCount);
if (exceptionCount > UncaughtExceptionMaximum)

return;

NSMutableDictionary *userInfo =
[NSMutableDictionary
dictionaryWithObject:[NSNumber numberWithInt:signal]
forKey:UncaughtExceptionHandlerSignalKey];
NSArray *callStack = [UncaughtExceptionHandler backtrace];
[userInfo
setObject:callStack
forKey:UncaughtExceptionHandlerAddressesKey];
[[[[UncaughtExceptionHandler alloc] init] autorelease]
performSelectorOnMainThread:@selector(handleException:)
withObject:
[NSException
exceptionWithName:UncaughtExceptionHandlerSignalExceptionName
reason:
[NSString stringWithFormat:
NSLocalizedString(@"Signal %d was raised.\\n"
@"%@", nil),
signal, getAppInfo()]
userInfo:
[NSDictionary
dictionaryWithObject:[NSNumber numberWithInt:signal]
forKey:UncaughtExceptionHandlerSignalKey]]
waitUntilDone:YES];

void InstallUncaughtExceptionHandler()

signal(SIGABRT, MySignalHandler);
signal(SIGILL, MySignalHandler);
signal(SIGSEGV, MySignalHandler);
signal(SIGFPE, MySignalHandler);
signal(SIGBUS, MySignalHandler);
signal(SIGPIPE, MySignalHandler);


在应用自身的 didFinishLaunchingWithOptions 前,加入一个函数:

- (void)installUncaughtExceptionHandler

InstallUncaughtExceptionHandler();


最后,在 didFinishLaunchingWithOptions 中加入这一句代码就行了:

[self InstallUncaughtExceptionHandler];

现在,基本上所有崩溃都能Hold住了。崩溃时将会显示出如下的对话框:
参考技术A 从容的崩溃? 崩溃很容易, 写个数组定义一个元素, 然后下一个步骤去访问array, 程序立马就崩溃

显示单元部分明智地导致 iOS 应用程序崩溃

【中文标题】显示单元部分明智地导致 iOS 应用程序崩溃【英文标题】:Display cell section wise crashes iOS app 【发布时间】:2013-11-18 09:40:48 【问题描述】:

我有Tableview,其中包含从AZ 的部分(我动态计算的部分数量不固定)

我想这样显示: : 我的数组值:msg_array=["AajKaCatch","AajKaItem","Anari","Big C Mobiles","Big Flix","BigRock","caksonflowers, ...."]

当我尝试在cellForRowAtIndexPath 中这样显示时,它会显示NSInvalidArgumentException

cell.textLabel.text=[[[msg_array objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"Merchant_Name"];

请帮助并提前致谢。

【问题讨论】:

msg_array 仅包含字符串对象。它应该包含 NSArray 的对象,其中将包含 NSDictionary 类型的对象。 【参考方案1】:

你的数组是这样的:

arrayobject,object,object,object,object;

在这种情况下,你不能使用like:

[[msg_array objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]

因为要实现这样一个,[msg_array objectAtIndex:indexPath.section] 应该返回一个数组。

所以实现这个,你需要这样尝试:

arrayarrayobjects starts with 'A',arrayobjects starts with 'B',arrayobjects starts with 'C';

【讨论】:

-(void)createAlphabetArray NSMutableArray *tempFirstLetterArray = [[NSMutableArray alloc] init]; for (int i = 0; i &lt; [msg_array count]; i++) NSMutableArray *temp=[[NSMutableArray alloc] init]; NSString *letterString = [[[msg_array objectAtIndex:i] objectForKey:@"Merchant_Name"] substringToIndex:1]; if (![tempFirstLetterArray containsObject:[letterString uppercaseString]]) [tempFirstLetterArray addObject:letterString]; [temp addObject:[[msg_array objectAtIndex:i] objectForKey:@"Merchant_Name"]]; 你说得对,但是如何在我的数组中添加元素?以上是我写的代码,但只添加了字符的第一个元素 @Krunal:在你的代码中,当它找到一个新字符时,if 块将为真,在所有其他情况下它将为假。所以它不会添加其他对象 那么,我该如何添加呢?【参考方案2】:

当你这样做时:

[[[msg_array objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"Merchant_Name"];

您正在访问 msg_array 的元素,就好像它是一个 NSArray,其中包含一个 NSDictionary

但是,在 msg_array 内部你只有NSStrings

您尝试访问的结构是:

NSArray -> NSArray -> NSDictionary

你有

NSArray -> NSString

【讨论】:

你说得对,但是如何在我的数组中添加元素?下面是我写的代码,但它只添加了字符的第一个元素 -(void)createAlphabetArray NSMutableArray *tempFirstLetterArray = [[NSMutableArray alloc] init]; for (int i = 0; i &lt; [msg_array count]; i++) NSMutableArray *temp=[[NSMutableArray alloc] init]; NSString *letterString = [[[msg_array objectAtIndex:i] objectForKey:@"Merchant_Name"] substringToIndex:1]; if (![tempFirstLetterArray containsObject:[letterString uppercaseString]]) [tempFirstLetterArray addObject:letterString]; [temp addObject:[[msg_array objectAtIndex:i] objectForKey:@"Merchant_Name"]]; 【参考方案3】:

我使用 FKRSearchBarTableViewController 对联系信息和其他类似的事情做了同样的事情,请参阅链接,下面是我的 FKRSearchBarTableViewController 代码

    - (id)initWithSectionIndexes:(BOOL)showSectionIndexes withDataSource:(NSArray*) dataSource withControllerId:(int) ControllerId forGroup:(int)groupId

if ((self = [super initWithNibName:nil bundle:nil])) 
    self.title = @"Search Bar";
    NSLog(@"%d",groupId);
    _groupID = groupId;
    _controllerId = ControllerId;

    _showSectionIndexes = showSectionIndexes;

    _famousPersons = [[NSMutableArray alloc]initWithArray:dataSource];
    if (showSectionIndexes) 
        UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];

        NSMutableArray *unsortedSections = [[NSMutableArray alloc] initWithCapacity:[[collation sectionTitles] count]];
        for (NSUInteger i = 0; i < [[collation sectionTitles] count]; i++) 
            [unsortedSections addObject:[NSMutableArray array]];
        
        if(ControllerId == 5)
        
        for (Person *personName in self.famousPersons) 

               // NSInteger index = [collation sectionForObject:[personName objectForKey:@"FirstName"] collationStringSelector:@selector(description)];
            NSLog(@"%@",personName.firstName);
            NSInteger index = [collation sectionForObject:personName.firstName collationStringSelector:@selector(description)];
                [[unsortedSections objectAtIndex:index] addObject:personName];
        
        
        else
        
            for (NSDictionary *personName in self.famousPersons) 

                 NSInteger index = [collation sectionForObject:[personName objectForKey:@"FirstName"] collationStringSelector:@selector(description)];

                [[unsortedSections objectAtIndex:index] addObject:personName];
            

        

        NSMutableArray *sortedSections = [[NSMutableArray alloc] initWithCapacity:unsortedSections.count];
        for (NSMutableArray *section in unsortedSections) 
            [sortedSections addObject:[NSMutableArray arrayWithArray:[collation sortedArrayFromArray:section collationStringSelector:@selector(description)]]];
        

        self.sections = [NSMutableArray arrayWithArray:sortedSections];

    

【讨论】:

【参考方案4】:

为了让列表更加动态,解决方案应该是

// given NSArray names = your full list of name
//       NSArray indexes = your list of index

NSMutableArray *nameSections = [NSMutableArray arrayWithCapacity:26];
NSMutableArray *filteredIndexes = [NSMutableArray arrayWithCapacity:26];

for (NSString *index in indexes) 

    NSPredicate *predicate = [NSPredicate predicateWithFormat:
                              @"SELF beginswith[c] %@",index];
    NSArray *filterNames = [names filteredArrayUsingPredicate:predicate];

    if(filterNames.count>0)
        [nameSections addObject:filterNames];
        [filteredIndexes addObject:index];
    



NSLog(@"filteredIndexes %@",filteredIndexes);
NSLog(@"nameSections %@",nameSections);


numOfSection = nameSections.count
numOfRow = [[numOfSection indexOfObject:section]count];
name = [[numOfSection indexOfObject:section]] indexOfObject:row];


// print log
//given indexes array a~z

names (
"a_string",
"a_string2",
"b_string",
"b_string2"
)

filteredIndexes (
a,
b
)

nameSections (
    (
    "a_string",
    "a_string2"
    ),
    (
    "b_string",
    "b_string2"
    )
)

【讨论】:

以上是关于如何让IOS应用从容地崩溃的主要内容,如果未能解决你的问题,请参考以下文章

iOS 13 - 带有占位符的 UITextField 让应用程序崩溃

显示单元部分明智地导致 iOS 应用程序崩溃

iOS7 Xcode 5 升级让 longpress 崩溃应用

发现iOS新漏洞!这个印度字符可以让iphone崩溃~

IOS应用崩溃复活

如何使 Android 和 iOS 的 Flutter App 崩溃(以测试 Firebase Crashlytics)?