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


当前回答

[[UIApplication sharedApplication] terminateWithSuccess];

它工作正常,自动调用

- (void)applicationWillTerminateUIApplication *)application delegate.

要删除编译时警告,请添加以下代码

@interface UIApplication(MyExtras)
  - (void)terminateWithSuccess;
@end 

其他回答

点击这里查看问答: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

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

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

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

我使用了上面提到的[[NSMutableArray new] addObject:nil]方法来强制退出(崩溃)应用程序,而不做一个泄露的退出(0)函数调用。

为什么?因为我的应用程序在所有网络API调用上都使用了证书钉住,以防止中间人攻击。这些包括我的财务应用程序在启动时所做的初始化调用。

如果证书认证失败,我的所有初始化调用错误,让我的应用程序在一个不确定的状态。让用户回家然后回到应用程序并没有帮助,除非应用程序已经被操作系统清除,否则它仍然是未初始化和不值得信任的。

因此,在这种情况下,我们认为最好弹出一个警告,通知用户应用程序正在不安全的环境中运行,然后,当他们点击“关闭”时,使用上述方法强制退出应用程序。

在iPadOS 13中,你现在可以像这样关闭所有场景会话:

for session in UIApplication.shared.openSessions {
    UIApplication.shared.requestSceneSessionDestruction(session, options: nil, errorHandler: nil)
}

这将在你的应用委托上调用applicationWillTerminate(_ application: UIApplication),并在最后终止应用。

但要注意两件事:

这当然不是用来关闭所有的场景。(参见https://developer.apple.com/design/human-interface-guidelines/ios/system-capabilities/multiple-windows/) 它在iPhone的iOS 13上编译和运行良好,但似乎什么都不做。

更多关于iOS/iPadOS 13场景的信息:https://developer.apple.com/documentation/uikit/app_and_environment/scenes

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