应用程序启动时的操作

发布于 2024-12-13 05:06:35 字数 241 浏览 0 评论 0原文

我希望每次我的应用程序启动时(甚至可能从后台恢复)它都会执行一个操作(例如 TWTweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init];)。 我还希望可以通过应用程序设置包中的开关禁用此操作(启动时)。我应该怎么办?感谢您的关注。

ps 如果我使用了不正确的词语,我深表歉意..我是初学者:)

I'd like that each time my app starts up (possibly even when it's restored from background) it makes an action (for example TWTweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init];).
I'd also like that this action (on startup) can be disabled by a switch in the setting bundle of the app. What should I do? Thanks for your attention.

p.s. I apologize if I used incorrect words.. I'm a beginner :)

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

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

发布评论

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

评论(3

撑一把青伞 2024-12-20 05:06:35

您可以在应用程序委托中实现多种方法,这些方法可在 UIApplicationDelegate 协议中使用。

当您的应用程序首次启动时,将调用 applicationDidFinishLaunching

当您的应用程序从后台恢复时,将调用 applicationWillEnterForeground

添加到设置包中的开关将有一个与其关联的键,该键是一个 NSString 。开关在该键下的标准 NSUserDefaults 中存储编码为 NSNumber 的布尔值。您可以从标准用户默认值中读取布尔值,并使用它来确定是否执行该操作。

有关如何添加设置包的苹果文档是 此处
在您的设置包中,您需要一个切换开关。您将在标准用户默认值中查找的密钥由 Key 字段指定。切换开关的默认值由 DefaultValue 字段指定。 请参阅此处

这是您需要在 applicationDidFinishLaunching 中执行的操作 方法

static NSString *const kTakeActionOnLaunchSettingKey = @"Key";

- (void)applicationDidFinishLaunching
{
  NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  BOOL doTakeActionOnLaunch = [userDefaults boolForKey:kTakeActionOnLaunchSettingKey];
  if (doTakeActionOnLaunch) {
    // Do something
  }
}

There are several methods which you can implement in your application delegate which are available in the UIApplicationDelegate protocol.

When your app is first launched applicationDidFinishLaunching is called.

When your app is restored from the background applicationWillEnterForeground is called.

A switch which you add to your setting bundle will have a key, which is an NSString, associated with it. A switch stores a boolean value encoded as an NSNumber in the standard NSUserDefaults under that key. You can read the value of the boolean from the standard user defaults and use it to determine whether to perform the action.

Apples documentation on how to add a settings bundle is here.
In your settings bundle you'll need a toggle switch. The key that you will look up in the standard user defaults is specified by the Key field. The default value for your toggle switch is specified by the DefaultValue field. See here

Here is what you need to do in your applicationDidFinishLaunching method

static NSString *const kTakeActionOnLaunchSettingKey = @"Key";

- (void)applicationDidFinishLaunching
{
  NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  BOOL doTakeActionOnLaunch = [userDefaults boolForKey:kTakeActionOnLaunchSettingKey];
  if (doTakeActionOnLaunch) {
    // Do something
  }
}
予囚 2024-12-20 05:06:35

在 voidDidLoad {} 方法中初始化它。要禁用它,您可以使用对象库中的开关

Initialise it in voidDidLoad {} method. For disabling it you can use switch from object libary

岁吢 2024-12-20 05:06:35

这很简单,我用它来从启动画面到主页进行很酷的过渡。
您需要将代码放入 AppDelegate m 文件中。

用于

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

在启动时运行代码。

使用以下方法来管理后台<->前景过渡:

- (void)applicationWillResignActive:(UIApplication *)application 
- (void)applicationDidEnterBackground:(UIApplication *)application 
- (void)applicationWillEnterForeground:(UIApplication *)application 
- (void)applicationDidBecomeActive:(UIApplication *)application 

希望这对您有帮助。

It's easy and I used it to make a cool transition from the splash screen to the home page.
You need to put your code inside the AppDelegate m file.

use

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

to run code at startup.

use the following methods to manage the Backgroud <-> Foreground transitions:

- (void)applicationWillResignActive:(UIApplication *)application 
- (void)applicationDidEnterBackground:(UIApplication *)application 
- (void)applicationWillEnterForeground:(UIApplication *)application 
- (void)applicationDidBecomeActive:(UIApplication *)application 

Hope this helps you.

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