简体   繁体   English

如何在仍然针对 iOS 15 的同时使用`.backgroundTask` 修饰符?

[英]How to use `.backgroundTask` modifier while still targeting iOS 15?

I'm trying to use the new .backgroundTask modifier announced at WWDC '22 but I can't get the below to compile?我正在尝试使用 WWDC '22 上宣布的新.backgroundTask 修饰符,但我无法编译以下内容? Xcode says Cannot infer contextual base in reference to member 'appRefresh' 🤔 How can I do this? Xcode 说Cannot infer contextual base in reference to member 'appRefresh' 🤔 我该怎么做?

import SwiftUI
import Foundation
import BackgroundTasks

  var body: some Scene {
      WindowGroup {
          if #available(iOS 16, *) {
              AppRootView()
                  .backgroundTask(.appRefresh("refreshAllData")) {
                      refreshData() 
                  }
          } else {
              AppRootView()
          }
      }
  }

Edit: as agentBilly said .backgroundTask is a scene modifier, so should look like this but I'm still unsure how to check for iOS 16 availability?编辑:正如 agentBilly 所说 .backgroundTask 是一个场景修饰符,所以应该看起来像这样,但我仍然不确定如何检查 iOS 16 的可用性?

import SwiftUI
import Foundation
import BackgroundTasks


@main
struct myApp: App {
    
    @Environment(\.scenePhase) var scenePhase
   
    var body: some Scene {
        WindowGroup {
            AppRootView()
        }
        .backgroundTask(.appRefresh("refreshAllData")) {
            refreshAllData() 
        }
}

We cannot use condition right in scene body, because SceneBuilder does not support conditional expression, so a solution is to have completely different apps loaded depending on availability.我们不能在场景主体中使用条件,因为SceneBuilder不支持条件表达式,所以解决方案是根据可用性加载完全不同的应用程序。

Simplified code to have less lines:简化代码以减少行数:

struct AppScene: Scene {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

@available(iOS 16, *)
struct RefreshingAppScene: Scene {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        .backgroundTask(.appRefresh("refreshAllData")) {
            refreshAllData()
        }
    }
}

@main
struct AppLoader {
    static func main() {
        if #available(iOS 16, *) {  // << conditional availability !!
            New_iOSApp.main()
        } else {
            Old_iOSApp.main()
        }
    }
}

@available (iOS 16, *)
struct New_iOSApp: App {
    var body: some Scene {
        RefreshingAppScene()
    }
}

struct Old_iOSApp: App {
    var body: some Scene {
        AppScene()
    }
}

Tested with Xcode 14b3 / iOS 16使用 Xcode 14b3 / iOS 16 测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS 15中如何使用scrollEdgeAppearance? - How to use scrollEdgeAppearance in iOS 15? SwiftUI 在 iOS 15 上使用 editMode 的 onDelete 修饰符和自定义滑动操作 - SwiftUI use editMode's onDelete modifier alongside with custom Swipe Actions on iOS 15 在仍支持ios 5.1的情况下如何在情节提要中使用UICollectionViewController? - How to use UICollectionViewController in storyboard while still supporting ios 5.1? backgroundTask在iOS中未完成 - backgroundTask not finishing in iOS 如何在iOS 7中仍然使用iOS6 UI? - How to still use iOS6 UI in iOS 7? 我们如何添加锁屏小部件(需要 iOS 16)并仍然支持 iOS 15? - How can we add a Lock Screen Widget (requiring iOS 16) and still support iOS 15? iOS 15 仅视图修改器会导致 iOS 14 崩溃,即使 #available(iOS 15.0, *) - iOS 15 only view modifier causes crash on iOS 14 even with if #available(iOS 15.0, *) 在定位较旧的iOS版本时,如何添加需要iOS 12的自定义意图? - How can I add a Custom Intent which requires iOS 12 while targeting an older iOS version? 如何在 Swiftui(iOS 15 / Xcode 13.2.1)的 AppDelegate 中使用我的@EnvironmentObject? - How to use my @EnvironmentObject in AppDelegate in Swiftui (iOS 15 / Xcode 13.2.1)? 面向iPhone的iOS应用仍可在iPad上全屏显示吗? - iOS application targeting iPhone still opens fullscreen on iPad?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM