是否可以在后台启动extendRuntimeSession

问题描述 投票:0回答:1

我的 iPhone 应用程序有一个伴侣手表应用程序,并通过手表和手机之间的消息和 applicationContext 进行通信。如果我在 iPhone 上启动一个事件并且我的手表已打开,extendRuntimeSession 将按其应有的方式启动。但是,一旦我的手表显示屏关闭或我不在应用程序中,我就无法启动扩展的运行时会话,即使我通过 applicationContext 接收它,并且在尝试在后台启动会话时收到以下错误消息。

-[CSLSSessionService startSession:completionWithExpirationDate:]_block_invoke session A76273B7-3E01-4333-920C-0614C1FAC5B0 encountered error Error Domain=com.apple.CarouselServices.SessionErrorDomain Code=12 "app not in required app state" UserInfo={NSLocalizedDescription=app not in required app state}
Is Running is true
Timer started/continuend
-[SPApplicationDelegate appBeginWorkout:]_block_invoke:1334: Asked to start a workout, but WKExtensionDelegate <SwiftUI.ExtensionDelegate: 0x15d7c500> doesn't implement handleWorkoutConfiguration:
Extended runtime session invalidated with reason: WKExtendedRuntimeSessionInvalidationReason(rawValue: -1), error: Optional(Error Domain=WKExtendedRuntimeSessionErrorDomain Code=3 "The app must be active and before applicationWillResignActive to start or schedule a WKExtendedRuntimeSession." UserInfo={NSLocalizedDescription=The app must be active and before applicationWillResignActive to start or schedule a WKExtendedRuntimeSession..})

我像这样控制和管理我的会话:

class WatchViewModel: NSObject, ObservableObject {
    static let shared = WatchViewModel()
    var session: WCSession
    var extendedRuntimeSessionManager: ExtendedRuntimeSessionManager
    var lastMessage: [String: Any] = ["method": "none"]
    
    @Published var iOSIsReachable = false
  
    @Published var dataFromMessages = DataFromMessages()
    @Published var flextailCoordinates: FlextailCoordinates?
    
    
    init(session: WCSession = .default, extendedRuntimeSessionManager: ExtendedRuntimeSessionManager = ExtendedRuntimeSessionManager()) {
        NSLog("init start")
        self.session = session
        self.extendedRuntimeSessionManager = extendedRuntimeSessionManager
        
        super.init()
        self.session.delegate = self
        session.activate()
        NSLog("init done")
    }
}

class ExtendedRuntimeSessionManager: NSObject, WKExtendedRuntimeSessionDelegate, ObservableObject{
    var extendedRuntimeSession: WKExtendedRuntimeSession?
    @Published var sessionIsActive = false

    override init() {
        super.init()
        setupExtendedRuntimeSession()
    }

    func setupExtendedRuntimeSession() {
        extendedRuntimeSession = WKExtendedRuntimeSession()
        extendedRuntimeSession?.delegate = self
    }

    func startExtendedRuntimeSession() {
        if sessionIsActive == false{
            extendedRuntimeSession?.start()
        } else{
            print("Already running extendedRuntimeSession")
        }
    }

    func stopExtendedRuntimeSession() {
        extendedRuntimeSession?.invalidate()
        sessionIsActive = false
        setupExtendedRuntimeSession()
    }

    // WKExtendedRuntimeSessionDelegate methods
    func extendedRuntimeSessionDidStart(_ extendedRuntimeSession: WKExtendedRuntimeSession) {
        print("Extended runtime session started")
        sessionIsActive = true
        
    }

    func extendedRuntimeSessionWillExpire(_ extendedRuntimeSession: WKExtendedRuntimeSession) {
        print("Extended runtime session will expire soon")
    }

    func extendedRuntimeSession(_ extendedRuntimeSession: WKExtendedRuntimeSession, didInvalidateWith reason: WKExtendedRuntimeSessionInvalidationReason, error: Error?) {
        print("Extended runtime session invalidated with reason: \(reason), error: \(String(describing: error))")
        sessionIsActive = false
        setupExtendedRuntimeSession()
        
    }
    
    
}

我尝试通过 applicationContext 发送它,但这不起作用,即使消息按照我想要的方式被接收和解析。该错误似乎具体出现在后台扩展运行时会话的启动中。

swiftui watchkit apple-watch watchos
1个回答
0
投票

不幸的是,目前这是不可能的。 Apple 文档特别指出“当您的应用程序状态处于 WKApplicationState.active 状态时,您必须始终启动或安排扩展运行时会话。”

请参阅使用扩展运行时会话

© www.soinside.com 2019 - 2024. All rights reserved.