我可以主动关闭我的 iOS 应用程序吗?
在一些罕见的数据损坏情况下,我不想在同一会话中尝试恢复,而是想执行一些修复,然后关闭应用程序,以便下次启动时安全。
除了用 *(unsigned int *)0 = 0xDEADBEEF
这样的愚蠢的东西硬崩溃应用程序之外,我在 Cocoa 中找不到导致正常关闭的 API。
更新:找到了有关该主题的文档,该文档基本上证实了怀疑并指出 exit
作为最后的选择:
http://developer.apple.com/iphone/library/qa/qa2008/qa1561.html
There are some rare data-corruption circumstances where rather than attempt a recovery in the same session, I'd like to perform some fixes and then shutdown the app so the next launch will be safe.
Short of hard crashing the app with something dumb like *(unsigned int *)0 = 0xDEADBEEF
, I can't find an API in Cocoa that causes a graceful shutdown.
UPDATE: Found this documentation on the subject, that essentially confirms suspicion and points to exit
as a last-ditch option:
http://developer.apple.com/iphone/library/qa/qa2008/qa1561.html
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,它也适用于 iPhone。
或者,
Yep it works on the iPhone too.
Alternatively,
在调试调试版本时,我更喜欢一些会崩溃到调试器中的东西,这样我不仅可以知道原因,而且可以在必要时查看更多详细信息。向任何非 nil 对象发送随机消息是 Objective C 实现此目的的一种方法。
对于应用程序商店提交,-terminate 肯定会让您的应用程序因使用(官方)未记录的 API 而被拒绝。据报道,exit(0) 也会这样做,即使它是一个有记录的操作系统调用。
在 OS 4.0 之前,您始终可以向 Safari 发送 URL,这将导致操作系统正常终止您的应用程序。不知道在 OS 4.0 中做什么,之后应用程序商店是合法的。
When debugging a Debug build, I prefer something that will crash into the debugger so I'll not only know the reason why, but can poke around for more details if necessary. Sending a random message to any non-nil object is one Objective C way of doing this.
For App store submission, -terminate will definitely get your app rejected for using an (officially) undocumented API. And exit(0) has been reported to do so also, even though it's a documented OS call.
Prior to OS 4.0, you could always send a URL to Safari, which would cause the OS to gracefully terminate your app. Not sure what to do in OS 4.0 and after that's App store legal.
这对我来说没有任何意义 - 任何资源都不应该无法进行内部重置。强制关闭应用程序对用户来说很糟糕。
This doesn't make any sense to me - no resource should be out of reach for an internal reset. Force-closing the app sucks for the user.