如何在 iOS 中以编程方式重启 iPhone 应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4238979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How programmatically restart an iPhone app in iOS
提问by Andrei Eremchuk
How programmatically restart an iPhone app in iOS?
如何在 iOS 中以编程方式重启 iPhone 应用程序?
I find this way http://writeitstudios.com/david/?p=54
我找到这种方式http://writeitstudios.com/david/?p=54
But may be something simple.
但可能是一些简单的事情。
回答by TomSwift
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.
首先,您的应用程序必须选择退出后台执行(多任务)应用程序在退出时必须退出,而不是作为后台任务运行。这是通过 plist 键 UIApplicationExitsOnSuspend 完成的。
Second, your app needs to register a custom URL scheme that can be used to launch the app.
其次,您的应用程序需要注册可用于启动应用程序的自定义 URL 方案。
Third, you need a web page hosted somewhere that when loaded will redirect to your app's custom URL scheme.
第三,您需要在某处托管一个网页,该网页在加载时会重定向到您应用的自定义 URL 方案。
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.
要退出并重新启动,请在您托管的重定向网页上调用 UIApplication openURL。您的应用程序将退出,safari 将启动并加载您的页面。该页面会将 Safari 重定向到您的自定义 URL 方案,提示 Safari 内部调用 openURL,从而导致 iOS 启动您的应用程序。
回答by Pripyat
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.
您链接到的我的帖子指的是 Cocoa 应用程序,而不是 iOS。在 iOS 上,您可以使用退出应用程序(但 Apple 不喜欢这样),exit(0);
但我不建议这样做。但是,您无法重新启动 iPhone 应用程序。
回答by samvermette
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.
除非您正在为越狱设备开发,否则 Apple 甚至不允许您以编程方式终止您的应用程序。所以重启设备是不可能的。
回答by user2084569
Your AppDelegate
instance has a method
您的AppDelegate
实例有一个方法
(void)applicationDidBecomeActive:(UIApplication *)application
{
}
In 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
variable appMustRestart
that is false
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.
在这里,您可以放置逻辑来确定应用程序是否应该重新启动,或者继续执行它正在做的任何事情。例如,您可以有一个BOOL
变量appMustRestart
,该变量false
最初是,但每当您的应用程序中发生您希望下次重新启动的事情时,它就会被触发为真。
if (appMustRestart)
{
[self resetVars]; // call a method that resets all your vars to initial settings
// INSERT CODE HERE TO TRANSFER FOCUS TO INITIAL VIEWCONTROLLER
}