我正在编程一个iPhone应用程序,我需要强制它退出由于某些用户操作。在清理应用程序分配的内存后,调用什么方法来终止应用程序?
当前回答
苹果说:
警告:不要调用exit函数。调用exit的应用程序将在用户看来已经崩溃,而不是执行优雅的终止并动画返回主屏幕。”
我认为这是一个错误的假设。如果用户点击退出按钮,出现类似“应用程序现在将退出”这样的消息,它似乎没有崩溃。苹果应该提供退出应用程序的有效方法(而不是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);
}
}
您是否尝试过exit(0)?
或者,[[NSThread主线程]退出],虽然我没有尝试,这似乎是更合适的解决方案。
这并不是一种退出项目的方法,而是一种强迫人们退出的方法。
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];
苹果说:
警告:不要调用exit函数。调用exit的应用程序将在用户看来已经崩溃,而不是执行优雅的终止并动画返回主屏幕。”
我认为这是一个错误的假设。如果用户点击退出按钮,出现类似“应用程序现在将退出”这样的消息,它似乎没有崩溃。苹果应该提供退出应用程序的有效方法(而不是exit(0))。
Swift 4.2(或更老版本)
库称为达尔文可以使用。
import Darwin
exit(0) // Here you go
注意:在iOS应用程序中不建议这样做。
这样做会得到崩溃日志。
推荐文章
- Xcode构建失败“架构x86_64未定义的符号”
- 如何使用Xcode创建。ipa文件?
- 动态改变UILabel的字体大小
- registerForRemoteNotificationTypes: iOS 8.0及以上版本不支持
- Xcode 4 -在新的Macintosh安装上的配置文件上“没有找到有效的签名标识”错误
- 新的自动引用计数机制是如何工作的?
- 如何测试对象在Objective-C中的类?
- 在iPhone上确定用户是否启用了推送通知
- 是否有可能禁用浮动头在UITableView与UITableViewStylePlain?
- 从Cocoa应用程序执行一个终端命令
- 错误ITMS-9000:“冗余二进制文件上传。火车1.0版本已经有一个二进制版本上传。
- Swift -转换为绝对值
- Swift编译器错误:“框架模块内的非模块化头”
- 从父iOS访问容器视图控制器
- 自定义dealloc和ARC (Objective-C)