如何在 iOS 中以编程方式重新启动 iPhone 应用程序

发布于 2024-10-04 03:22:15 字数 185 浏览 1 评论 0原文

如何在 iOS 中以编程方式重新启动 iPhone 应用程序?

我发现这样 http://writeitstudios.com/david/?p=54

但是可能很简单。

How programmatically restart an iPhone app in iOS?

I find this way http://writeitstudios.com/david/?p=54

But may be something simple.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

落叶缤纷 2024-10-11 03:22:15

我知道这样做的唯一方法并不理想,但它有效。

首先,您的应用程序必须选择退出后台执行(多任务处理)应用程序必须在退出时退出,而不是作为后台任务运行。这是通过 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.

╰ゝ天使的微笑 2024-10-11 03:22:15

您链接到的我的帖子指的是 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.

ゞ花落谁相伴 2024-10-11 03:22:15

除非您正在为越狱设备进行开发,否则 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.

↙厌世 2024-10-11 03:22:15

您的 AppDelegate 实例有一个方法

(void)applicationDidBecomeActive:(UIApplication *)application
{
}

,您可以在此处放置逻辑来确定应用程序是否应该重新启动,或者继续执行它正在执行的操作。例如,您可以有一个 BOOL 变量 appMustRestart,该变量最初为 false,但每当您的应用中发生您想要的情况时,该变量就会被触发为 true喜欢下次重新推出。

if (appMustRestart)
{
    [self resetVars];  // call a method that resets all your vars to initial settings

    // INSERT CODE HERE TO TRANSFER FOCUS TO INITIAL VIEWCONTROLLER
}

Your AppDelegate instance has a method

(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.

if (appMustRestart)
{
    [self resetVars];  // call a method that resets all your vars to initial settings

    // INSERT CODE HERE TO TRANSFER FOCUS TO INITIAL VIEWCONTROLLER
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文