BackgroundTask iOS 13 Swift

丶灬走出姿态 提交于 2019-12-10 03:34:25

问题


I want to run the function in the background. But I get an error. The error I received is

"nw_connection_receive_internal_block_invoke.

Why am I getting this problem? I'm trying it on an iOS 13 device. But I can't run the application in the background. I added a background run feature in Info.plist. I want to run the interator.tickTimer function in the background. But I'm getting an error. Does not work in the background. In the background, I want to periodically retrieve data from firebase.

import BackgroundTasks

  @available(iOS 13.0, *)
  extension AppDelegate {

      func cancelAllPandingBGTask() {
          BGTaskScheduler.shared.cancelAllTaskRequests()
      }

      func scheduleImageFetcher() {
          let request = BGProcessingTaskRequest(identifier: "....")
          request.requiresNetworkConnectivity = true // Need to true if your task need to network process. Defaults to false.
          request.requiresExternalPower = false
          request.earliestBeginDate = Date(timeIntervalSinceNow: 40)

          do {

              try BGTaskScheduler.shared.submit(request)
          } catch {
              print("Could not schedule image featch: \(error)")
          }
      }


      func handleAppRefreshTask(task: BGAppRefreshTask) {

          task.expirationHandler = {

          }
         DispatchQueue.main.async {

         let interator =  MainTableViewController()
         interator.tickTimer()


           }
          task.setTaskCompleted(success: true)
      }

      func handleImageFetcherTask(task: BGProcessingTask) {
          scheduleImageFetcher() // Recall

          //Todo Work
          task.expirationHandler = {

          }
          task.setTaskCompleted(success: true)

      }
  }
  xtension AppDelegate {

      func registerLocalNotification() {
          let notificationCenter = UNUserNotificationCenter.current()
          let options: UNAuthorizationOptions = [.alert, .sound, .badge]

          notificationCenter.requestAuthorization(options: options) {
              (didAllow, error) in
              if !didAllow {
                 // print("User has declined notifications")
              }
          }
      }

      func scheduleLocalNotification() {
          let notificationCenter = UNUserNotificationCenter.current()
          notificationCenter.getNotificationSettings { (settings) in
              if settings.authorizationStatus == .authorized {
                  self.fireNotification()
              }
          }
      }

      func fireNotification() {
          // Create Notification Content
          let notificationContent = UNMutableNotificationContent()

          // Configure Notification Content
          notificationContent.title = "Bg"
          notificationContent.body = "BG Notifications."

          // Add Trigger
          let notificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 1.0, repeats: false)

          // Create Notification Request
          let notificationRequest = UNNotificationRequest(identifier: "local_notification", content: notificationContent, trigger: notificationTrigger)

          // Add Request to User Notification Center
          UNUserNotificationCenter.current().add(notificationRequest) { (error) in
              if let error = error {
                  print("Unable to Add Notification Request (\(error), \(error.localizedDescription))")
              }
          }
      }
  }
  @UIApplicationMain
  class AppDelegate: UIResponder, UIApplicationDelegate, UITabBarControllerDelegate {

      var window: UIWindow?
   func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    if #available(iOS 13.0, *) {
              registerBackgroundTaks()
              registerLocalNotification()

          } else {
              // Fallback on earlier versions
          }

          return true
     }

      func applicationDidEnterBackground(_ application: UIApplication) {
          if #available(iOS 13.0, *) {
              cancelAllPandingBGTask()
                     scheduleImageFetcher()
          } else {
              // Fallback on earlier versions
          }

      }

      //MARK: Regiater BackGround Tasks
      @available(iOS 13.0, *)
      private func registerBackgroundTaks() {

          BGTaskScheduler.shared.register(forTaskWithIdentifier: "....-...", using: nil) { task in
              //This task is cast with processing request (BGProcessingTask)
              self.scheduleLocalNotification()

              self.handleImageFetcherTask(task: task as! BGProcessingTask)
          }   
      }

来源:https://stackoverflow.com/questions/57413646/backgroundtask-ios-13-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!