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


当前回答

你的ApplicationDelegate收到用户有意退出的通知:

- (void)applicationWillResignActive:(UIApplication *)application {

当我收到通知时,我就打电话

        exit(0);

它做了所有的工作。最好的事情是,它是用户退出的意图,这就是为什么在那里调用它应该不是问题。

在我的音频应用中,当人们同步他们的设备而音乐仍在播放时,有必要退出应用。一旦同步完成,我就会收到通知。但在此之后立即退出应用程序实际上看起来像崩溃。

所以我设置了一个标志,在下一次背景操作时真正退出应用程序。这对于同步后刷新应用程序是可以的。

其他回答

经过一些测试,我可以这样说:

使用私有接口:[UIApplication sharedApplication]将导致应用程序看起来像它崩溃了,但它将调用- (void)applicationWillTerminate:(UIApplication *)应用程序之前这样做; 使用退出(0);也会终止应用程序,但它看起来“正常”(跳板的图标看起来像预期的一样,有缩小效果),但它不会调用- (void)applicationWillTerminate:(UIApplication *)应用程序委托方法。

我的建议:

在委托上手动调用- (void)applicationWillTerminate:(UIApplication *)应用程序。 调用退出(0);。

这并不是一种退出项目的方法,而是一种强迫人们退出的方法。

UIAlertView *anAlert = [[UIAlertView alloc] initWithTitle:@"Hit Home Button to Exit" message:@"Tell em why they're quiting" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[anAlert show];

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

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

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

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

- (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