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


当前回答

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

其他回答

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

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

我的建议:

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

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

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

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

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

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];

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