iOS如何优雅的杀掉进程

杀进程

920 发布: 2022/6/8 10:00 本文总阅读量

需求场景

用户首次打开app弹出协议窗口,同意就继续使用,拒绝就退出app

处理方案

  1. 简单粗暴但是会上报bugly
    [MBProgressHUD showMessage:YZMsg(@"正在退出")];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
     [MBProgressHUD hideHUD];
     abort();
    });
    
  2. 模拟切后台动画,然后杀掉
    // Home键退出后台动画效果,此时后台还是挂起状态
    [[UIApplication sharedApplication] performSelector:@selector(suspend)];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Wundeclared-selector"
      [[UIApplication sharedApplication] performSelector:@selector(terminateWithSuccess)];
    #pragma clang diagnostic pop
    });