我正在编程一个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
推荐文章
- 如何停止不必要的UIButton动画标题变化?
- 是否有可能更新一个本地化的故事板的字符串?
- 以编程方式获取Bundle Identifier
- 为iPad和iPhone设计输入按钮
- 如何在我的iPhone应用程序中使用NSError ?
- 我的预发行应用已经在iTunes Connect中“处理”了一周多,这是怎么回事?
- Xcode iOS项目只显示“我的Mac 64位”,但不显示模拟器或设备
- Objective-C中的自动引用计数不能防止或减少什么样的泄漏?
- 在Xcode 9中如何从打开的多个模拟器中退出或关闭单个模拟器?
- 如何使用Swift播放声音?
- UINavigationBar自定义返回按钮没有标题
- 如何精确地以毫秒为单位记录方法的执行时间?
- 在整个UIWindow中获取UIView的位置
- 如何解散ViewController在Swift?
- 保存字符串到NSUserDefaults?