【问题标题】:is there anyway to keep my WatchKit app running in the background?有没有办法让我的 WatchKit 应用程序在后台运行?
【发布时间】:2017-03-02 10:06:02
【问题描述】:

我是 swift 和 WatchKit 框架的新手。我只学习了一个月。现在我的问题是:是否可以在 Apple Watch 上以后台模式运行应用程序?我从文档中读到 WatchKit扩展程序不支持后台执行模式。但是如果这件事是真的,如何使用健康应用程序之类的应用程序?我的意思是,在手表上有一个名为健康的应用程序,即使该应用程序不是在前台。我或多或少会做同样的事情。在我的应用程序中,即使应用程序不在前台,我也只会在用户移动手表时检测到用户加速度。我该怎么办?请发给我必要的文件。非常感谢!

附言。对不起我的英语不好!

【问题讨论】:

    标签: ios swift watchkit core-motion


    【解决方案1】:

    是的,你可以。有几种方法:

    1. 使用 HKWorkoutSession 开始锻炼 - 最强大的方式,但是您需要使用 HealthKit,而且 Apple Watch 上只有 1 个应用可以通过这种方式运行。
    2. 使用 WKAudioFilePlayer 在后台播放音频 - 仅用于音频。
    3. WKURLSessionRefreshBackgroundTask 适合短暂的背景刷新。
    4. 还有其他后台任务,例如WKSnapshotRefreshBackgroundTaskWKURLSessionRefreshBackgroundTask。它们列在this article 的后台任务部分。
    5. performExpiringActivity(withReason:using:) 可用于在应用程序进入后台时短暂延长其生命周期。

    另外,请阅读Leveraging iOS Technologies

    【讨论】:

      【解决方案2】:

      在 WatchOS 6+ 中,如果您的手表应用属于以下 5 个用例之一,则可以请求WKExtendedRuntimeSession (see documentation)

      • 自我照顾
      • 正念
      • 物理疗法
      • 警报
      • 健康监测

      解释WKExtendedRuntimeSession的相关WWDC视频:https://developer.apple.com/videos/play/wwdc2019/251/

      【讨论】:

      • 如果我的应用不属于这些类别怎么办?如果是这样,如果我仍然选择一种作为背景模式会怎样?
      • 如果你为你的应用选择了一个不合适的类别,只是为了能够在后台模式下运行你的应用,我想你的应用最有可能在应用的审核过程中被拒绝店铺。 Apple 将这些类别放在首位是有原因的,他们不希望任何应用开发者滥用它们。
      【解决方案3】:

      在 watchOS 3 中,锻炼应用可以在后台运行。我没有亲身经历,但请查看 WWDC 2016 视频Building Great Workout Apps

      【讨论】:

        【解决方案4】:

        我也面临同样的问题。我想在应用程序处于后台模式时执行一些代码。我已经解决了这个问题。我发现了另一种状态,我的手表应用程序将继续在后台运行,这不需要HKWorkoutSession 或上面讨论的任何其他解决方案。只需按照以下步骤操作

        1. 导入CoreLocation
        2. 在课堂上添加CLLocationManagerDelegate
        3. willActive方法中添加如下代码。

          locationManager = CLLocationManager()
          locationManager.delegate = self
          locationManager.requestWhenInUseAuthorization()
          locationManager.desiredAccuracy = kCLLocationAccuracyBest
          locationManager.distanceFilter = kCLDistanceFilterNone
          if #available(watchOSApplicationExtension 3.0, *) {
              locationManager.startUpdatingLocation()
          } else {
              // Fallback on earlier versions
          }
          
          if #available(watchOSApplicationExtension 4.0, *) {
              locationManager.allowsBackgroundLocationUpdates = true
          } else {
             print("Location not updated")
          }
          
        4. 最后只需添加 CLLocationManagerDelegate

        func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
            print("Location updated\(locations)")
        }
        
        func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
            print("Location_update_error\(error)")
        }
        

        也许这会解决你的问题。

        【讨论】:

          猜你喜欢
          • 2011-05-28
          • 2023-03-15
          • 1970-01-01
          • 2012-06-03
          • 1970-01-01
          • 2022-01-02
          • 2020-11-08
          • 2011-12-23
          • 2014-11-24
          相关资源
          最近更新 更多