后台任务 iOS 13 Swift

2024-03-10

我想在后台运行该函数。但我收到错误。我收到的错误是

“nw_connection_receive_internal_block_invoke。

为什么我会遇到这个问题?我正在尝试iOS 13设备。但我无法在后台运行该应用程序。我添加了后台运行功能Info.plist。我想运行interator.tickTimer在后台运行的功能。但我收到错误。不在后台工作。在后台,我想定期从 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)
          }   
      }


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

后台任务 iOS 13 Swift 的相关文章

随机推荐

  • 如何在 QML 中重用代码

    我有这段 QML 代码 Column spacing units gu 2 anchors fill parent centerIn parent Row spacing units gu 4 Row spacing units gu 4
  • 如何将变量从一个脚本传递到另一个脚本

    这是一个我无法正常工作的脚本 它的目的是检查一个复选框是否被选中 然后调用第二个 响应的例程 取决于我最初调用脚本时显示的 selectedindex sindex
  • C# 将字符串转换为 ASCII 字节

    我有一个字符串 LogoDataStr ABC0000 我想转换为 ASCII 字节 结果应该是 LogoDataBy 0 0x41 LogoDataBy 1 0x42 LogoDataBy 2 0x43 LogoDataBy 3 0x30
  • 不推荐使用delayWhen RxJs

    I got a Deprecated symbol used 我的 IDE 中出现使用警告delayWhen在我的应用程序中 My code private nextImg Observable
  • 从文件或标准输入读取

    我正在编写一个实用程序 它接受文件名或从标准输入读取 我想知道检查标准输入是否存在 数据正在通过管道传输到程序 以及如果存在则读取该数据的最强大 最快的方法 如果不存在 则处理将在文件名上进行给予 我尝试使用以下测试来确定大小stdin但我
  • OpenCV 数据矩阵代码查找器

    有人写过或听说过用 OpenCV 编写的数据矩阵代码解码器吗 我用的是自带的解码器ZXing http code google com p zxing 不过说实话 还挺糟糕的 它需要是一个完全清晰的图像 使用整个图像 并且不成一个角度 后者
  • 设置详细的正则表达式

    在尝试设置详细的正则表达式时 set up variables ankerwords beerdigt bestattet begraben beigesetzt combine the words five words before af
  • 是否可以让 Windows Azure 模拟器将浏览器打开到 127.0.0.1 以外的 URL

    简单的问题 但背后有很多意义 讨论 是否可以让 Windows Azure 模拟器将浏览器打开到 127 0 0 1 和端口 81 之外的 URL 按照步骤将 127 0 0 1 更改为所需 IP 计算模拟器设置 转到 Program Fi
  • 如何从 Ruby 字符串中删除特定字符?

    我有几个看起来像这样的字符串 String1 它们的长度都不同 如何在循环中删除所有这些字符串中的括号 使用如下操作String tr http ruby doc org core 2 0 0 String html method i tr
  • Scala:伴生对象与案例类的定义顺序

    在 Scala 2 9 1 中 我得到以下行为 class Foo case class X object X this compiles def bar object Y this compiles case class Y case c
  • python:几何布朗运动模拟[关闭]

    这个问题不太可能对任何未来的访客有帮助 它只与一个较小的地理区域 一个特定的时间点或一个非常狭窄的情况相关 通常不适用于全世界的互联网受众 为了帮助使这个问题更广泛地适用 访问帮助中心 help reopen questions GBM 的
  • 在 Snowflake 中逆透视多个列

    I have a table that looks as follows I need to unpivot the Rating and the Comments as follows 在 Snowflake 中执行此操作的最佳方法是什么
  • 嵌套首选项屏幕因 Android 中的屏幕方向更改而关闭

    我最近偶然发现一个问题 我正在使用这样的嵌套 PreferenceScreen
  • 可以在 Tcp 连接中分离某些数据吗?

    我的问题很简单 可以在 TCP 连接中分离某些数据 如标题所示 吗 例如 如果我打电话MySocket Send Encoding UTF8 GetBytes this is a test 我可以从连接的另一端获得 2 个单独的接收操作 例
  • 第二次调用同一源时 strncpy() 失败

    我是 c 新手 想将字符串分成两部分 这是我的代码 include
  • 投影某一列并在每一行进行一项

    我有一个如下所示的数据集 metadata d meta v 1 5 9 data a T 1652167964645 1652168781684 1652168781720 V 1 2 3
  • 如何使用 for in 循环并从 firebase 查询数据

    我有一个查询 可以获取我的 firebase 数据库中用户的所有密钥 这些键保存到数组中 然后我想循环遍历数组并在循环查询中查找用户名 问题是它在加载第一个名称时停止 即使数组中有 1000 个键 循环也不会查询 var i for i 0
  • 删除 javafx 中边框窗格中的特定节点

    我有一个边框窗格 其顶部 左侧和右侧都有组件 我想通过鼠标事件使用 borderPane 的 id 来删除其右侧的组件 我怎么做 Just do borderPane setRight null
  • 为什么 /Qvec-report:2 什么也不返回? (MSVC 2012)

    我尝试了 Qvec report 2 选项来查看 MSVC 自动矢量化器是否有任何用处 不幸的是 我没有得到任何结果 无论是积极的还是消极的 gt Microsoft R C C Optimizing Compiler Version 17
  • 后台任务 iOS 13 Swift

    我想在后台运行该函数 但我收到错误 我收到的错误是 nw connection receive internal block invoke 为什么我会遇到这个问题 我正在尝试iOS 13设备 但我无法在后台运行该应用程序 我添加了后台运行功