HomeiOS Developmentios - Points establishing WCSessionDelegate utilizing SwiftUI

ios – Points establishing WCSessionDelegate utilizing SwiftUI


So I’m studying methods to setup the WCSession delegate for cellphone and watch.

I really feel like I’m doing what Apple docs is advising however for no matter cause I get this log in my console: “WCSession is lacking its delegate.” And I say log as a substitute of error, as a result of I setup my delegate to deal with errors within the delegate’s activationDidCompleteWith perform, however that perform won’t get hit.

Any concept on what I’m doing unsuitable? I’m utilizing SwiftUI.

Right here is the code on my telephones facet:

PhoneMain.swift

import SwiftUI
import WatchConnectivity

@foremost
/// The principle start line for the cellphone app
struct PhoneMain: App {
    var physique: some Scene {
        WindowGroup {
            ContentView()
                .onAppear {
                    print("*** Cellphone Content material View Appeared ***")
                    // Establishing cellphone default configurations
                    PhoneAppDefaults.sharedInstance.configure()
                }
        }
    }
}

ContentView.swift

import SwiftUI
import WatchConnectivity

struct ContentView: View {
    var physique: some View {
        VStack {
            Picture(systemName: "globe")
                .imageScale(.giant)
                .foregroundStyle(.tint)
            Textual content("Good day, world!")
        }
        .padding()
    }
}

PhoneAppDefaults.swift

import Basis
import WatchConnectivity

/// The place all the cellphone app default configurations are setup
class PhoneAppDefaults {
    
    /// The sharedInstance for the PhoneAppDefaults singleton
    static var sharedInstance = PhoneAppDefaults()
    
    /// Non-public init makes this struct a singleton
    non-public init() {
        print("*** WatchAppDefaults.sharedInstance initialized ***")
    }
    
    /// Initiaties default app configurations
    func configure() {
        print("*** Configuring watch defaults settings ***")
        PhoneWCSessionDelegate().startSession()
    }
}

PhoneWCSessionDelegate.swift

import Basis
import WatchConnectivity

/// The WCSession delegate on the watch facet
class PhoneWCSessionDelegate: NSObject, WCSessionDelegate {
    
    /// Assigns this delegate to WCSession and begins the session
    func startSession() {
        guard WCSession.isSupported() else { return }
        print("*** Beginning WCSession for cellphone ***")
        let session = WCSession.default
        session.delegate = self
        print("*** Activating cellphone WCSession ***")
        session.activate()
    }
    
    /// A delegate perform known as everytime WCSession is activated
    func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
        if let error = error {
            print("*** Cellphone WCSession activation error: (error) ***")
        } else {
            swap activationState {
            case .activated:
                print("*** WCSession activated for cellphone ***")
            case .notActivated:
                print("*** WCSession didn't activate for cellphone ***")
            case .inactive:
                print("*** WCSession inactive for cellphone ***")
            @unknown default:
                print("*** WCSession activation consequence: Unknown, for cellphone ***")
            }
        }
    }
    
    /// A delegate perform known as everytime WCSession recieves an utility context replace
    func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
        print("*** WCSession recieved utility context on cellphone ***")
    }
    
    /// A delegate perform known as everytime WCSession turns into inactive
    func sessionDidBecomeInactive(_ session: WCSession) {
        print("*** WCSession grew to become inactive on cellphone ***")
    }
    
    /// A delegate perform known as everytime WCSession deactivates
    func sessionDidDeactivate(_ session: WCSession) {
        print("*** WCSession deactivated on cellphone ***")
    }
}

And right here is the watch facet:

WatchMain.swift

import SwiftUI
import WatchConnectivity

@foremost
/// The principle start line for the watch app
struct WatchMain: App {
    var physique: some Scene {
        WindowGroup {
            ContentView()
                .onAppear {
                    print("*** Watch Content material View Appeared ***")
                    // Establishing watch default configurations
                    WatchAppDefaults.sharedInstance.configure()
                }
        }
    }
}

ContentView.swift

import SwiftUI
import WatchConnectivity

struct ContentView: View {
    var physique: some View {
        VStack {
            Picture(systemName: "globe")
                .imageScale(.giant)
                .foregroundStyle(.tint)
            Textual content("Good day, world!")
        }
        .padding()
    }
}

WatchAppDefaults.swift

import Basis
import WatchConnectivity

/// The place all the watch app default configurations are setup
class WatchAppDefaults {
    
    /// The sharedInstance for the WatchAppDefaults singleton
    static var sharedInstance = WatchAppDefaults()
    
    /// Non-public init makes this struct a singleton
    non-public init() {
        print("*** WatchAppDefaults.sharedInstance initialized ***")
    }
    
    /// Initiaties default app configurations
    func configure() {
        print("*** Configuring watch defaults settings ***")
        WatchWCSessionDelegate().startSession()
    }
}

WatchWCSessionDelegate.swift

import Basis
import WatchConnectivity

/// The WCSession delegate on the watch facet
class WatchWCSessionDelegate: NSObject, WCSessionDelegate {
    
    /// Assigns this delegate to WCSession and begins the session
    func startSession() {
        print("*** Beginning WCSession for watch ***")
        let session = WCSession.default
        session.delegate = self
        print("*** Activating watch WCSession ***")
        session.activate()
    }
    
    /// A delegate perform known as everytime WCSession is activated
    func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
        if let error = error {
            print("*** Watch WCSession activation error: (error) ***")
        } else {
            swap activationState {
            case .activated:
                print("*** WCSession activated for watch ***")
            case .notActivated:
                print("*** WCSession didn't activate for watch ***")
            case .inactive:
                print("*** WCSession inactive for watch ***")
            @unknown default:
                print("*** WCSession activation consequence: Unknown, for watch ***")
            }
        }
    }
    
    /// A delegate perform known as everytime WCSession recieves an utility context replace
    func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
        print("*** WCSession recieved utility context on watch ***")
        
    }
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments