首页 > 编程知识 正文

ios老是闪退怎么办,iOS闪退

时间:2023-05-04 03:04:20 阅读:143239 作者:3639

Java有try catch,事先验证正确性。 iOS中也有嘛,但是只捕捉数组越界、数据类型错误等异常,Xcode识别不到的话,就会崩溃闪回。

提高用户体验,不要闪回,从我开始,哈哈,很简单啊

@try(/执行的代码如果异常则抛出,程序不会继续执行((catch ) nsexception*exception )/(捕捉异常) ) finally )//这里需要不管是否异常) )看逻辑怎么写

在上面的代码中定义对象

. h中

# import foundation/foundation.h # importuikit/uik it.h @ interfaceuncaughtexceptionhandler 3360 ns object { bool dismised } voidsignalHandler(intsignal; voidinstalluncaughtexceptionhandler (void; m中

# import ' uncaughtexceptionhandler.h ' # importuikit/uik it.h # includelibkern/OS atomic.h # includexecinfo.h # impon impor constuncaughtexceptionhandlersignalexceptionname=@ ' uncaughtexceptionhandlersignalexceptionname '; ns string * constuncaughtexceptionhandlersignalkey=@ ' uncaughtexceptionhandlersignalkey '; ns string * constuncaughtexceptionhandleraddresseskey=@ ' uncaughtexceptionhandleraddresseskey '; volatile int 32 _ tuncaughtexceptioncount=0; constint 32 _ tuncaughtexceptionmaximum=10; constnsintegeruncaughtexceptionhandlerskipaddresscount=4; constnsintegeruncaughtexceptionhandlerreportaddresscount=5; @ implementationuncaughtexceptionhandler (ns array ) ) backtrace(/程序崩溃2 (程序崩溃的第二步方法) void*callstack(128 ) intframes=backtrace(callstack,128 ); char * * strs=backtrace _ symbols (call stack,frames ); int i; nsmutablearray * backtrace=[ nsmutablearrayarraywithcapacity 3360 frames ]; for (I=uncaughtexceptionhandlerskipaddresscount; iuncaughtexceptionhandlerskipaddresscountuncaughtexceptionhandlerreportaddresscount; I ) { [ backtrace addobject : [ nsstringstringwithutf8string : strs [ I ] }; (free ) strs; 返回后台; (void ) alertview:(uialertview* ) analertviewclickedbuttonatindex : (ns integer ) anindex ) if ) an index==0} elex }--(void ) validateandsavecriticalapplicationdata (/崩溃块能做的事情,用这个方法写也很棒的nslog(@ '崩溃了) ); uialertcontroller * alert=[ uialertcontrollertcontrollerwithtitle : @ '温馨提示' message 3360 @ '打开首选样式3360

tAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }]; [alert addAction:cancelAction]; UIViewController * rootViewController = [[[UIApplication sharedApplication] keyWindow] rootViewController]; [rootViewController presentViewController:alert animated:YES completion:^{ }];}// 程序崩溃3(程序崩溃是第三进入的方法)- (void)handleException:(NSException *)exception { [self validateAndSaveCriticalApplicationData]; NSString *message = [NSString stringWithFormat:NSLocalizedString(@"如果点击继续,程序有可能会出现其他的问题,建议您还是点击退出按钮并重新打开nn"@"异常原因如下:n%@n%@",nil),[exception reason],[[exception userInfo] objectForKey:UncaughtExceptionHandlerAddressesKey]]; UIAlertView *alert =[[UIAlertView alloc]initWithTitle:NSLocalizedString(@"抱歉,程序出现了异常",nil) message:message delegate:self cancelButtonTitle:NSLocalizedString(@"退出",nil) otherButtonTitles:NSLocalizedString(@"继续",nil), nil]; NSLog(@"异常显示了"); [alert show]; UIAlertAction *act1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }]; UIAlertController *vc = [UIAlertController alertControllerWithTitle:@"异常啦" message:nil preferredStyle:UIAlertControllerStyleAlert]; [vc addAction:act1]; UIViewController *rootViewController = [[[UIApplication sharedApplication] keyWindow] rootViewController]; [rootViewController presentViewController:vc animated:YES completion:nil]; CFRunLoopRef runLoop = CFRunLoopGetCurrent(); CFArrayRef allModes = CFRunLoopCopyAllModes(runLoop); while (!dismissed) { for (NSString *mode in (__bridge 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]; }}@endvoid HandleException(NSException *exception) { int32_t exceptionCount =OSAtomicIncrement32(&UncaughtExceptionCount); if (exceptionCount >UncaughtExceptionMaximum) { return; } NSArray *callStack = [UncaughtExceptionHandler backtrace]; NSMutableDictionary *userInfo =[NSMutableDictionary dictionaryWithDictionary:[exception userInfo]];[userInfo setObject:callStack forKey:UncaughtExceptionHandlerAddressesKey]; [[[UncaughtExceptionHandler alloc] init]performSelectorOnMainThread:@selector(handleException:)withObject: [NSException exceptionWithName:[exception name] reason:[exception reason] userInfo:userInfo]waitUntilDone:YES];}void SignalHandler(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]performSelectorOnMainThread:@selector(handleException:)withObject:[NSException exceptionWithName:UncaughtExceptionHandlerSignalExceptionName reason:[NSString stringWithFormat:NSLocalizedString(@"Signal %d was raised.",nil),signal]userInfo: [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:signal]forKey:UncaughtExceptionHandlerSignalKey]]waitUntilDone:YES];}void InstallUncaughtExceptionHandler(void) { NSSetUncaughtExceptionHandler(&HandleException); signal(SIGHUP, SignalHandler);//本信号在用户终端连接(正常或非正常)结束时发出, 通常是在终端的控制进程结束时, 通知同一session内的各个作业 signal(SIGINT, SignalHandler);//程序终止(interrupt)信号, 在用户键入INTR字符(通常是Ctrl-C)时发出,用于通知前台进程组终止进程。 signal(SIGQUIT, SignalHandler);//类似于一个程序错误信号。 signal(SIGABRT,SignalHandler);//调用abort函数生成的信号。 signal(SIGILL,SignalHandler);//用来立即结束程序的运行. 本 signal(SIGSEGV,SignalHandler);//试图访问未分配给自己的内存, 或试图往没有写权限的内存地址写数据. signal(SIGFPE,SignalHandler);//在发生致命的算术运算错误时发出. signal(SIGBUS,SignalHandler);//访问不属于自己存储空间或只读存储空间 signal(SIGPIPE,SignalHandler);//管道破裂。}

之后AppDelegate.h里面

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler); return YES; }

彩蛋,NullSafe 下一个扔工程里面,起码解决你空指针问题。但是最好还是严谨些
这样就可以啦

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。