我有一个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 提供的完成处理程序,而是向调用 setTaskCompletedWithSnapshot
的 BackgroundSession
提供您自己的完成处理程序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/