Json 在BGAppRefreshTask ios 13期间,URLSession失败
我面临一个问题,在BGAppRefresh调用期间无法调用URLSession请求 我已经启用了后台提取功能,并在info.plist中添加了必要的键 后台获取被完美地调用,但是,当它使用completionHandler到达URLSession时失败 以下是AppDelegate代码:Json 在BGAppRefreshTask ios 13期间,URLSession失败,json,ios13,background-task,urlsession,xcode12,Json,Ios13,Background Task,Urlsession,Xcode12,我面临一个问题,在BGAppRefresh调用期间无法调用URLSession请求 我已经启用了后台提取功能,并在info.plist中添加了必要的键 后台获取被完美地调用,但是,当它使用completionHandler到达URLSession时失败 以下是AppDelegate代码: func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicatio
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let register = BGTaskScheduler.shared.register(forTaskWithIdentifier: "ist.notification", using: nil) { task in
self.handleDailyNotifications(task: task as! BGAppRefreshTask)
}
}
func scheduleDailyNotificationRandomQASelection() {
let request = BGAppRefreshTaskRequest(identifier: "ist.notification")
request.earliestBeginDate = Date(timeIntervalSinceNow: 1*60)
do {
try BGTaskScheduler.shared.submit(request)
} catch {
print("Could not schedule Daily Notification's DB Fetcher Method: \(error)")
}
}
@available(iOS 13.0, *)
private func handleDailyNotifications(task: BGAppRefreshTask) {
let queue = OperationQueue()
queue.maxConcurrentOperationCount = 1
queue.addOperation {
self.setNotifications()
}
task.expirationHandler = {
queue.cancelAllOperations()
}
let lastOperation = queue.operations.last
lastOperation?.completionBlock = {
task.setTaskCompleted(success: !(lastOperation?.isCancelled ?? false))
}
scheduleDailyNotificationRandomQASelection()
}
func setNotifications() {
let TimesURL = "xxx.com"
if let url = URL(string: TimesURL!) {
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
guard error == nil else {
print(error!)
return
}
if let data = data {
do {
//Json Parsing method//
}catch let error {
print("TimesURL Error:"+error.localizedDescription)
}
}
}
}
task.resume()
}
方法被卡住了
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
好心协助
非常感谢