我正在编程一个iPhone应用程序,我需要强制它退出由于某些用户操作。在清理应用程序分配的内存后,调用什么方法来终止应用程序?


当前回答

Swift 4.2(或更老版本)

库称为达尔文可以使用。

import Darwin

exit(0) // Here you go

注意:在iOS应用程序中不建议这样做。

这样做会得到崩溃日志。

其他回答

- (IBAction)logOutButton:(id)sender
{
   //show confirmation message to user
   CustomAlert* alert = [[CustomAlert alloc] initWithTitle:@"Confirmation" message:@"Do you want  to exit?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
   alert.style = AlertStyleWhite;
   [alert setFontName:@"Helvetica" fontColor:[UIColor blackColor] fontShadowColor:[UIColor clearColor]];
   [alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{

   if (buttonIndex != 0)  // 0 == the cancel button
   {
      //home button press programmatically
      UIApplication *app = [UIApplication sharedApplication];
      [app performSelector:@selector(suspend)];
      //wait 2 seconds while app is going background
      [NSThread sleepForTimeInterval:2.0];
      //exit app when app is in background
      NSLog(@"exit(0)");
      exit(0);
  }
}

点击这里查看问答:https://developer.apple.com/library/content/qa/qa1561/_index.html

Q: How do I programmatically quit my iOS application? There is no API provided for gracefully terminating an iOS application. In iOS, the user presses the Home button to close applications. Should your application have conditions in which it cannot provide its intended function, the recommended approach is to display an alert for the user that indicates the nature of the problem and possible actions the user could take — turning on WiFi, enabling Location Services, etc. Allow the user to terminate the application at their own discretion. WARNING: Do not call the exit function. Applications calling exit will appear to the user to have crashed, rather than performing a graceful termination and animating back to the Home screen. Additionally, data may not be saved, because -applicationWillTerminate: and similar UIApplicationDelegate methods will not be invoked if you call exit. If during development or testing it is necessary to terminate your application, the abort function, or assert macro is recommended

除了上面的,很好,答案我只想补充一下,想想清理你的记忆。

应用程序退出后,iPhone操作系统将自动清理应用程序遗留的所有内容,因此手动释放所有内存只会增加应用程序退出所需的时间。

用户应该决定应用程序何时退出。 我不认为当应用退出时这是一种良好的用户交互。因此,它没有很好的API,只有home按钮有一个。

如果有错误:更好地实现它或通知用户。 如果必须重新启动:执行它更好的通知用户。

这听起来很愚蠢,但在不让用户决定的情况下退出应用程序,并且不通知他,这是一种糟糕的做法。苹果表示,由于有一个home键用于用户交互,所以同一个功能(退出应用程序)不应该有两个东西。

您是否尝试过exit(0)?

或者,[[NSThread主线程]退出],虽然我没有尝试,这似乎是更合适的解决方案。