MultiPeer icon indicating copy to clipboard operation
MultiPeer copied to clipboard

Connection between iOS 17 and macOS 14

Open ukushu opened this issue 11 months ago • 0 comments

My test code is:

import SwiftUI
import MultiPeer

struct ContentView: View {
    @ObservedObject var model = ViewModel()
    
    var body: some View {
        VStack {
            Text("Connected devices: ") + Text(model.connectedDevices.count == 0 ? "NONE" : model.connectedDevices)
            
            Text("Connected peers: ") + Text("\(model.instance.connectedPeers.description )")
            
            Text("Is connected: " + "\(model.instance.isConnected )")
            
            Button("refresh") {
                model.objectWillChange.send()
            }
            
            Button("send") {
                model.send()
            }
        }
        .padding()
    }
}

class ViewModel: ObservableObject {
    @Published var connectedDevices: String = ""
    var instance: MultiPeer { MultiPeer.instance }
    
    init() {
        instance.initialize(serviceType: "LFT-app")
        instance.delegate = self
        instance.autoConnect()
    }
    
    func send() {
        instance.send(object: "Hello World!", type: DataType.string.rawValue)
    }
}

extension ViewModel: MultiPeerDelegate {
    func multiPeer(didReceiveData data: Data, ofType type: UInt32, from peerID: MCPeerID) {
        switch type {
        case DataType.string.rawValue:
            let string = data.convert() as! String
            
            print("string recieved: \(string)")
            // do something with the received string
            break
            
        case DataType.image.rawValue:
//            let image = NSImage(data: data)
            print("image recieved")
            // do something with the received UIImage
            break
            
        default:
            print("video recieved")
            break
        }
    }
    
    func multiPeer(connectedDevicesChanged devices: [String]) {
        connectedDevices = devices.joined(separator: ", ")
    }
}

enum DataType: UInt32 {
    case string = 1
    case image = 2
    case video = 3
}

my project config:

enter image description here

enter image description here

enter image description here

In the same time I have connection in case of 2 apps run on macOS.

Environment with issue: macOS: 14.3 (23D56) iOS: 17.3.1 (21D61)

ukushu avatar Mar 05 '24 00:03 ukushu