如何在 iOS 中以编程方式重新启动 iPhone 应用程序
How programmatically restart an iPhone app in iOS?
I find this way http://writeitstudios.com/david/?p=54
But may be something simple.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我知道这样做的唯一方法并不理想,但它有效。
首先,您的应用程序必须选择退出后台执行(多任务处理)应用程序必须在退出时退出,而不是作为后台任务运行。这是通过 plist 键 UIApplicationExitsOnSuspend 完成的。
其次,您的应用程序需要注册可用于启动应用程序的自定义 URL 方案。
第三,您需要一个托管在某处的网页,加载后将重定向到您应用程序的自定义 URL 方案。
第四,用户需要有效的互联网连接。
要退出并重新启动,请在托管重定向网页上调用 UIApplication openURL。您的应用程序将退出,Safari 将启动并加载您的页面。该页面会将 Safari 重定向到您的自定义 URL 方案,提示 Safari 在内部调用 openURL,从而导致 iOS 启动您的应用程序。
The only way I know to do this is not ideal, but it works.
First, your app has to opt out of background execution (multitasking) The app has to quit when exited, not run as a background task. This is done with the plist key UIApplicationExitsOnSuspend.
Second, your app needs to register a custom URL scheme that can be used to launch the app.
Third, you need a web page hosted somewhere that when loaded will redirect to your app's custom URL scheme.
Forth, the user needs an active Internet connection.
To exit and restart, call UIApplication openURL on your hosted redirecting web page. Your app will exit and safari will launch and load your page. The page will redirect Safari to your custom URL scheme, prompting Safari to internally call openURL, causing iOS to launch your app.
您链接到的我的帖子指的是 Cocoa 应用程序,而不是 iOS。在 iOS 上,您可以使用
exit(0);
退出应用程序(但 Apple 不喜欢这样做),但我不建议这样做。但您无法重新启动 iPhone 应用程序。my post that you linked to is referring to a Cocoa Application, not the iOS. On the iOS, you can quit an application (but Apple doesn't like this) by using
exit(0);
but I don't recommend that. You cannot restart iPhone apps though.除非您正在为越狱设备进行开发,否则 Apple 甚至不允许您以编程方式终止您的应用程序。所以重启设备是不可能的。
Unless you're developing for jailbroken devices, Apple won't even allow you to programatically terminate your app. So restarting the device is out of the question.
您的 AppDelegate 实例有一个方法
,您可以在此处放置逻辑来确定应用程序是否应该重新启动,或者继续执行它正在执行的操作。例如,您可以有一个
BOOL
变量appMustRestart
,该变量最初为false
,但每当您的应用中发生您想要的情况时,该变量就会被触发为 true喜欢下次重新推出。Your
AppDelegate
instance has a methodIn here, you can put logic to figure out if the app should restart, or continue doing whatever it was doing. For example you can have a
BOOL
variableappMustRestart
that isfalse
at first but gets triggered as true whenever something happens in your app that you'd like the next time to be a fresh relaunch.