如何重新启动我的iphone应用程序[重复]

时间:2022-01-26 20:11:52

Possible Duplicate:
Force iphone app to restart programmatically

可能重复:强制iphone应用程序以编程方式重新启动

How can I restart/reopen my iphone/iPad application programmatically

如何以编程方式重新启动/重新打开iphone / iPad应用程序

2 个解决方案

#1


5  

Note:

注意:

Although this has been down-voted I think it is a perfectly valid question for a new iOS developer to ask. There is a way to 'restart' your app from the user's perspective which is not technically restarting or exiting the iOS App. As pointed out by other answers an iOS App should never explicitly exit because this is not allowed on iOS.

虽然这已被低估了,但我认为这对于一个新的iOS开发人员来说是一个非常有效的问题。有一种方法可以从用户的角度“重新启动”您的应用程序,这在技术上不会重新启动或退出iOS应用程序。正如其他答案所指出的,iOS应用程序永远不会明确退出,因为iOS上不允许这样做。

My Answer:

我的答案:

If you want your app to go back to the state it was in at launch this is not 100% possible but I will explain a way to get most of the way there that should be sufficient for all valid purposes.

如果你希望你的应用程序回到它在启动时所处的状态,这不是100%可能的,但我会解释一种方法来获得大部分应该足以满足所有有效目的的方式。

The first thing to do is to re-create your root view controller. I recommend doing this from a method in the app delegate like this:

首先要做的是重新创建根视图控制器。我建议从app委托中的方法执行此操作,如下所示:

- (void)resetAppToFirstController
{
    self.window.rootViewController = [[MyMainViewController alloc] initWithNibName:nil bundle:nil];
}

In many cases this will be enough, but any app-state that you have should also be reset in this method. For example log out a user, reset any non-persistent state, and nullify (release) all objects that you can. This method can also be used to initially create your first view controller from application:didFinishLaunchingWithOptions.

在许多情况下,这已经足够了,但是您应该在此方法中重置您拥有的任何应用程序状态。例如,注销用户,重置任何非持久状态,并取消(释放)您可以使用的所有对象。此方法还可用于从应用程序初始创建第一个视图控制器:didFinishLaunchingWithOptions。

Framework classes and Singletons:

框架类和单身人士:

You will not be able to completely reset the state of any framework singletons or per-app instances, such as these:

您将无法完全重置任何框架单例或每个应用程序实例的状态,例如:

[UIApplication sharedApplication];
[NSNotificationCenter defaultCenter];
[NSUserDefaults standardUserDefaults];
[UIScreen screens];
// etc...

That is probably fine as you shouldn't be storing any non-persistent state in these anyway (except NSNotificationCenter, but all registered observers should have been removed when the objects were released). If you do want to initialise or reset any framework state you can do it in the same resetAppToFirstController method. Anyway, there should be no need to re-create these, or the window object.

这可能很好,因为你不应该在这些中存储任何非持久状态(除了NSNotificationCenter,但是当释放对象时应该删除所有注册的观察者)。如果您确实想要初始化或重置任何框架状态,可以在相同的resetAppToFirstController方法中执行此操作。无论如何,应该没有必要重新创建这些或窗口对象。

If you have any of your own singletons, you can re-create these by storing them in a singleton-holder class (which is itself a real singleton). Conceptually, this is a simple singleton class with properties for each of your other singletons and a reset method to nullify and release them all. Your other singletons should use this class (instead of a static or global variable) to store singleton instances. Be careful if you use any third party libraries as they may also employ singletons and you will need to ensure these also use your singleton-holder so that you can reset them as needed. I think this technique is good practice anyway, because in some cases (for example unit testing) you do want objects which are usually singletons to go away and reinitialise to a pristine state. However, you don't want to couple the singleton implementations with your singleton-holder, so a good way to implement this is by using an NSMutableDictionary as an associated object on [UIApplication sharedApplication] with the singleton class names as keys. However, I'm going off topic a bit as this is a more advanced technique beyond the scope of this question.

如果你有自己的单身,你可以通过将它们存储在一个单例持有者类(它本身就是一个真正的单例)中来重新创建它们。从概念上讲,这是一个简单的单例类,其中包含每个其他单例的属性,以及一个重置方法,可以使它们全部无效并释放它们。您的其他单身人士应该使用此类(而不是静态或全局变量)来存储单例实例。如果您使用任何第三方库,请小心,因为他们也可能使用单件,您需要确保这些库也使用您的单件持有人,以便您可以根据需要重置它们。我认为这种技术无论如何都是很好的做法,因为在某些情况下(例如单元测试)你确实希望通常是单身的物体消失并重新初始化为原始状态。但是,您不希望将单例实现与单例持有者耦合,因此实现此方法的一种好方法是使用NSMutableDictionary作为[UIApplication sharedApplication]上的关联对象,并将单例类名称作为键。但是,我有点偏离主题,因为这是一个超出这个问题范围的更先进的技术。


The above should be sufficient to 'reset' your application as far as the user is concerned. You can even show the splash screen again if you want as your first view controller.

就用户而言,上述内容应足以“重置”您的应用程序。如果您想要作为第一个视图控制器,甚至可以再次显示启动画面。

#2


0  

No, there is no way to restart an iOS application programmatically. The application's lifecycle is controlled by the OS.

不,没有办法以编程方式重新启动iOS应用程序。应用程序的生命周期由操作系统控制。

#1


5  

Note:

注意:

Although this has been down-voted I think it is a perfectly valid question for a new iOS developer to ask. There is a way to 'restart' your app from the user's perspective which is not technically restarting or exiting the iOS App. As pointed out by other answers an iOS App should never explicitly exit because this is not allowed on iOS.

虽然这已被低估了,但我认为这对于一个新的iOS开发人员来说是一个非常有效的问题。有一种方法可以从用户的角度“重新启动”您的应用程序,这在技术上不会重新启动或退出iOS应用程序。正如其他答案所指出的,iOS应用程序永远不会明确退出,因为iOS上不允许这样做。

My Answer:

我的答案:

If you want your app to go back to the state it was in at launch this is not 100% possible but I will explain a way to get most of the way there that should be sufficient for all valid purposes.

如果你希望你的应用程序回到它在启动时所处的状态,这不是100%可能的,但我会解释一种方法来获得大部分应该足以满足所有有效目的的方式。

The first thing to do is to re-create your root view controller. I recommend doing this from a method in the app delegate like this:

首先要做的是重新创建根视图控制器。我建议从app委托中的方法执行此操作,如下所示:

- (void)resetAppToFirstController
{
    self.window.rootViewController = [[MyMainViewController alloc] initWithNibName:nil bundle:nil];
}

In many cases this will be enough, but any app-state that you have should also be reset in this method. For example log out a user, reset any non-persistent state, and nullify (release) all objects that you can. This method can also be used to initially create your first view controller from application:didFinishLaunchingWithOptions.

在许多情况下,这已经足够了,但是您应该在此方法中重置您拥有的任何应用程序状态。例如,注销用户,重置任何非持久状态,并取消(释放)您可以使用的所有对象。此方法还可用于从应用程序初始创建第一个视图控制器:didFinishLaunchingWithOptions。

Framework classes and Singletons:

框架类和单身人士:

You will not be able to completely reset the state of any framework singletons or per-app instances, such as these:

您将无法完全重置任何框架单例或每个应用程序实例的状态,例如:

[UIApplication sharedApplication];
[NSNotificationCenter defaultCenter];
[NSUserDefaults standardUserDefaults];
[UIScreen screens];
// etc...

That is probably fine as you shouldn't be storing any non-persistent state in these anyway (except NSNotificationCenter, but all registered observers should have been removed when the objects were released). If you do want to initialise or reset any framework state you can do it in the same resetAppToFirstController method. Anyway, there should be no need to re-create these, or the window object.

这可能很好,因为你不应该在这些中存储任何非持久状态(除了NSNotificationCenter,但是当释放对象时应该删除所有注册的观察者)。如果您确实想要初始化或重置任何框架状态,可以在相同的resetAppToFirstController方法中执行此操作。无论如何,应该没有必要重新创建这些或窗口对象。

If you have any of your own singletons, you can re-create these by storing them in a singleton-holder class (which is itself a real singleton). Conceptually, this is a simple singleton class with properties for each of your other singletons and a reset method to nullify and release them all. Your other singletons should use this class (instead of a static or global variable) to store singleton instances. Be careful if you use any third party libraries as they may also employ singletons and you will need to ensure these also use your singleton-holder so that you can reset them as needed. I think this technique is good practice anyway, because in some cases (for example unit testing) you do want objects which are usually singletons to go away and reinitialise to a pristine state. However, you don't want to couple the singleton implementations with your singleton-holder, so a good way to implement this is by using an NSMutableDictionary as an associated object on [UIApplication sharedApplication] with the singleton class names as keys. However, I'm going off topic a bit as this is a more advanced technique beyond the scope of this question.

如果你有自己的单身,你可以通过将它们存储在一个单例持有者类(它本身就是一个真正的单例)中来重新创建它们。从概念上讲,这是一个简单的单例类,其中包含每个其他单例的属性,以及一个重置方法,可以使它们全部无效并释放它们。您的其他单身人士应该使用此类(而不是静态或全局变量)来存储单例实例。如果您使用任何第三方库,请小心,因为他们也可能使用单件,您需要确保这些库也使用您的单件持有人,以便您可以根据需要重置它们。我认为这种技术无论如何都是很好的做法,因为在某些情况下(例如单元测试)你确实希望通常是单身的物体消失并重新初始化为原始状态。但是,您不希望将单例实现与单例持有者耦合,因此实现此方法的一种好方法是使用NSMutableDictionary作为[UIApplication sharedApplication]上的关联对象,并将单例类名称作为键。但是,我有点偏离主题,因为这是一个超出这个问题范围的更先进的技术。


The above should be sufficient to 'reset' your application as far as the user is concerned. You can even show the splash screen again if you want as your first view controller.

就用户而言,上述内容应足以“重置”您的应用程序。如果您想要作为第一个视图控制器,甚至可以再次显示启动画面。

#2


0  

No, there is no way to restart an iOS application programmatically. The application's lifecycle is controlled by the OS.

不,没有办法以编程方式重新启动iOS应用程序。应用程序的生命周期由操作系统控制。