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


当前回答

我们不能使用exit(0), abort()函数退出应用程序,因为苹果强烈反对使用这些函数。不过,您可以将此函数用于开发或测试目的。

如果在开发或测试期间,有必要终止您的 建议使用应用程序、中止函数或断言宏

请找到这个苹果问答线程以获得更多信息。

由于使用此功能创建印象,应用程序正在崩溃。所以我得到了一些建议,比如我们可以显示警报与终止消息意识到用户关闭应用程序,由于某些功能不可用。

但是iOS人机界面启动和停止应用指南,建议永远不要使用退出或关闭按钮来终止应用。而不是他们建议展示适当的信息来解释情况。

iOS应用永远不会显示关闭或退出选项。人们不再使用 当他们切换到另一个应用程序,返回主屏幕,或放 他们的设备处于睡眠模式。 永远不要以编程方式退出iOS应用程序。人们倾向于这样解释 作为一个崩溃。如果有什么东西阻止你的应用程序运行 有意的,你需要告诉用户情况和解释什么 他们可以做些什么。

其他回答

Swift 4.2(或更老版本)

库称为达尔文可以使用。

import Darwin

exit(0) // Here you go

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

这样做会得到崩溃日志。

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

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

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

我的应用程序最近被拒绝了,因为我使用了一个无文件记录的方法。字面意思:

“不幸的是,它不能添加到App Store,因为它使用的是私有API。如iPhone开发者程序许可协议第3.3.1节所述,禁止使用非公共api:

“3.3.1应用程序只能按照苹果规定的方式使用文档api,不得使用或调用任何私有api。”

包含在应用程序中的非公共API是terminateWithSuccess"

Exit(0)在用户面前显示为崩溃,因此向用户显示确认消息。确认后暂停(home键按程序),等待2秒,应用程序将进入后台动画,然后退出用户视图

-(IBAction)doExit
{
    //show confirmation message to user
    UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Confirmation"
                                                 message:@"Do you want to exit?"
                                                delegate:self
                                       cancelButtonTitle:@"Cancel"
                                       otherButtonTitles:@"OK", nil];
    [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
        exit(0);
    }
}
- (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);
  }
}