【问题标题】:How can I detect my app was launched for background fetch in didFinishLaunchingWithOptions method?如何在 didFinishLaunchingWithOptions 方法中检测到我的应用程序是否已启动以进行后台提取?
【发布时间】:2015-02-04 11:17:05
【问题描述】:

我想在我的应用程序委托的 didFinishLaunchingWithOptions 中检查我的应用程序是否已启动以进行后台提取。 launchOptions 字典中没有任何内容。那么有什么办法可以查到呢?

我知道我可以检查applicationState,但由于某种原因,即使我正常启动应用程序,有时它也会返回 UIApplicationStateBackground。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    if (application.applicationState != UIApplicationStateBackground) {
    // Analytics initialization code
    }
}

我在 Analytics 初始化代码处创建了断点,有时即使我正常启动应用程序也会进入此块!

我知道我可以在以后调用applicationDidBecomeActive 或applicationDidEnterBackground 时检测到状态。如果我将使用这些方法来检测状态,我需要将我的 Analytics 初始化代码移动到其他地方。如果它保留在application:didFinishLaunchingWithOptions: 中,则每次我的应用程序启动后台获取时都会调用它。因此,也许我应该将 Analytics 初始化代码移至其他方法,而不是在 application:didFinishLaunchingWithOptions: 中检查 applicationState?如果是这样,我可以使用哪种方法?

【问题讨论】:

  • 可以在模拟器中模拟一次后台刷新。我会尝试查看 [UIApplication sharedApplication] applicationState] 以查看它是否报告它在后台。
  • 我已经试过了。但由于某种原因,即使我正常启动应用程序,有时它也会返回 UIApplicationStateBackground!
  • @tagirkaZ 你的意思是当你正常启动你的应用程序并在你的 didFinishLaunchingWithOptions 中检测到 UIApplicationStateBackground 时?
  • 为什么要知道application:didFinishLaunchingWithOptions:的上线原因?我会尝试清理该实现,以便它不会做太多事情并摆脱了解启动原因的需要。基本上只需设置窗口并初始化潜在的 SDK 等。然后我将在application:performFetchWithCompletionHandler: 中执行与背景获取相关的所有操作。也许如果你粘贴一些代码或只是关于你在做什么的伪代码,我可以提供进一步的帮助。
  • @CarmeloS 是的!我正常启动应用程序并检查 applicationState,有时它会返回 UIApplicationStateBackground。

标签: ios objective-c background-fetch


【解决方案1】:

查看此演示文稿的幻灯片 19-21:http://www.slideshare.net/moliver816/background-fetch

在启动时,您可以立即检查applicationState 是否等于UIApplicationStateBackground 以确定您的应用是否已启动到后台。

否则,如果您只想知道您的应用何时获取数据以进行后台应用刷新,您可以在UIApplicationDelegate 方法application:performFetchWithCompletionHandler: 中进行。

【讨论】:

  • 感谢您提供的链接!我知道我应该检查状态。但有时即使我正常启动应用程序也是 UIApplicationStateBackground!
  • 你能发布你用来检查的代码吗?这应该是不可能的。
  • 就我而言,UIApplication.shared.applicationState 总是在didFinishLaunchingWithOptions 中返回inactive。
【解决方案2】:

您可以使用 GCD 调度一次将分析初始化代码包装在一个单例中。这样你就知道每次应用程序加载它只运行一次。从后台恢复或任何其他状态都无关紧要,因为生命周期仍然是连续的。

+ (Analytics*)sharedInstance
  {
     static dispatch_once_t onceToken;
     static Analytics* sharedInstance;
     dispatch_once(& onceToken, ^{
         sharedInstance = [[self alloc] init];
         //Do analytics initialization code here
      });
     return sharedInstance;
  }

【讨论】:

    【解决方案3】:

    试试这个

    [[NSNotificationCenter defaultCenter] addObserver:self
                                                     selector:@selector(AppIsInBackground:)
                                                         name:UIApplicationDidEnterBackgroundNotification
                                                       object:nil];
    
            [[NSNotificationCenter defaultCenter] addObserver:self
                                                     selector:@selector(AppIsActive:)
                                                         name:UIApplicationDidBecomeActiveNotification
                                                     object:nil];
    

    你必须调用选择器:NSNotificationCenter 正在调用 AppIsInBackground 选择器(后台应用)

    - (void) AppIsInBackground:(NSNotification *) notification {
     //Shut down the memory/processor intensive things and save any states for when the app is reinitialized
    }
    

    【讨论】:

    • applicationDidEnterBackground 和 applicationDidBecomeActive 都是在 application:didFinishLaunchingWithOptions: 之后调用的。所以这种方法对我没用。
    【解决方案4】:

    以下是 iOS 为我们的应用程序执行后台获取时调用的委托方法。

    这是一个 appDelegate 方法。

    func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-15
      • 1970-01-01
      • 2021-12-31
      • 2012-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多