ios - 无法让 WatchKit URLSession 后台工作

标签 ios swift watchkit

我有一个BackgroundSession类的文件

class BackgroundSession: NSObject {
  static let shared = BackgroundSession()

  static let identifier = "com.***.bg"

  private var session: URLSession!

  var savedCompletionHandler: (() -> Void)?

  private override init() {
    super.init()

    let configuration = URLSessionConfiguration.background(withIdentifier: BackgroundSession.identifier)
    session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil)
  }

  func start(_ request: URLRequest) {
    session.downloadTask(with: request).resume()
  }
}

extension BackgroundSession: URLSessionDelegate {
  func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) {
    DispatchQueue.main.async {
      self.savedCompletionHandler?()
      self.savedCompletionHandler = nil
    }
  }
}

extension BackgroundSession: URLSessionTaskDelegate {
  func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
    if let error = error {
      // handle failure here
      print("\(error.localizedDescription)")
    }
  }
}

extension BackgroundSession: URLSessionDownloadDelegate {
  func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
    do {
      let data = try Data(contentsOf: location)
      let json = try JSONSerialization.jsonObject(with: data)

      print("\(json)")
      // do something with json
    } catch {
      print("\(error.localizedDescription)")
    }
  }
}

我正在监听稍后出现的后台位置更新

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    print("didUpdateLocations")
    if locations.first != nil {
      let lastLocation = locations.last
      self.lastLocation = lastLocation
      print("doing background work")
      self.getUserData()
      if PubnubController.pubnubChannel != nil {
        PubnubController.sharedClient.publish(["action": "newCoordinates", "data": ["coordinates": ["latitude": lastLocation?.coordinate.latitude, "longitude": lastLocation?.coordinate.longitude]]], toChannel: PubnubController.pubnubChannel!, compressed: false)
      }
    }
  }

self.getUserData()看起来像这样

func getUserData() {
    print("getUserData")
    if (self.userId != -1 && self.userAuthToken != nil) {
      let httpUrl: String = "https://api.***.com/dev/users/\(self.userId)"
      guard let url = URL(string: httpUrl) else {
        return
      }
      var request = URLRequest(url: url)
      request.setValue(self.userAuthToken, forHTTPHeaderField: "Authorization")
      let session = BackgroundSession.shared
      session.start(request)
    }
  }

在我的ExtensionDelegate.swift我有典型的func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>)

带有 for循环和switch配有外壳WKURLSessionRefreshBackgroundTask看起来像这样

case let urlSessionTask as WKURLSessionRefreshBackgroundTask:
              print("WKURLSessionRefreshBackgroundTask")
                // Be sure to complete the URL session task once you’re done.
                urlSessionTask.setTaskCompletedWithSnapshot(false)

在我的 Controller 中,我还粘贴了该类应该调用的函数

func application(_ application: WKExtension, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {
    print("handleEventsForBackgroundURLSession")
    BackgroundSession.shared.savedCompletionHandler = parseUserData
  }

似乎委托(delegate)函数和这个粘贴的函数都没有被我的数据调用。我很难理解这个后台 URLSession 流程

注意 BackgroundSession类来自这个 Stackoverflow 问题

URLSession.datatask with request block not called in background

最佳答案

这个handleEventsForBackgroundURLSession是一个iOS模式。这是 UIApplicationDelegate 的方法协议(protocol)。你不能只是将其添加到某个随机 Controller 中。它仅适用于您的 iOS 应用的 UIApplicationDelegate

对于 watchOS,我怀疑想法是相同的,只不过不是调用 iOS 提供的完成处理程序,而是向调用 setTaskCompletedWithSnapshotBackgroundSession 提供您自己的完成处理程序WKURLSessionRefreshBackgroundTask 的 >:

func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) {
    // Sent when the system needs to launch the application in the background to process tasks. Tasks arrive in a set, so loop through and process each one.
    for task in backgroundTasks {
        // Use a switch statement to check the task type
        switch task {
        case let urlSessionTask as WKURLSessionRefreshBackgroundTask:
            // Be sure to complete the URL session task once you’re done.
            BackgroundSession.shared.savedCompletionHandler = {
                urlSessionTask.setTaskCompletedWithSnapshot(false)
            }
        ...
        }
    }
}

但是,实际上,想法是相同的。我们将 setTaskCompletedWithSnapshot 推迟到调用 urlSessionDidFinishEvents(forBackgroundURLSession:) 为止。

<小时/>

如果您希望 BackgroundSession 调用 Controller 的解析器,您可以为该接口(interface)指定协议(protocol):

protocol Parser: class {
    func parse(_ data: Data)
}

然后,您可以为您的 BackgroundSession 提供一个属性来跟踪解析器:

weak var parser: Parser?

您可以让 didFinishDownloadingTo 调用解析器:

extension BackgroundSession: URLSessionDownloadDelegate {
    func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
        do {
            let data = try Data(contentsOf: location)
            parser?.parse(data)
        } catch {
            os_log(.error, log: log, "Error retrieving data for %{public}@: %{public}@", downloadTask.originalRequest?.url?.absoluteString ?? "Unknown request", error.localizedDescription)
        }
    }
}

然后您可以让您的 Controller (或其他任何东西)(a) 符合此协议(protocol); (b) 实现该协议(protocol)的 parse(_:) 方法; (c) 将自身指定为解析器:

BackgroundSession.shared.parser = self

关于ios - 无法让 WatchKit URLSession 后台工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57433884/

相关文章:

ios - 是否 openParentApplication :reply require App Groups capability to be enabled?

iphone - 将图像数组添加到 ScrollView Objective-C

iphone - 将它链接到 Storyboard 中的某个 View Controller ?

ios - 附近使用 Swift 3.0 的蓝牙设备

ios - ScrollView 不覆盖所有宽度

ios - 自定义 UITableViewCell - 不显示

watchkit - WKInterfaceTable 的 didSelectRowAtIndex 永远不会在 WKInterfaceController 中被调用

ios纸应用程序: swipe from off screen

ios - 在 iOS 中向外部蓝牙设备写入 >20 个字节

ios - "devices not paired"的 WatchKit 事件?