Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Session doesn't connect to peers #15

Open
chaert-s opened this issue Sep 8, 2023 · 4 comments
Open

Session doesn't connect to peers #15

chaert-s opened this issue Sep 8, 2023 · 4 comments

Comments

@chaert-s
Copy link

chaert-s commented Sep 8, 2023

Hi,
I have this running on 2 physical devices but when I try to connect them, I simply get this in the Xcode cmd output:
Not in connected state, so giving up for participant [48c78cc] on channel [0]. Not in connected state, so giving up for participant [48c78cc] on channel [1]. Not in connected state, so giving up for participant [48c78cc] on channel [2].

The peers never connect to each other. Is there anything I need to consider aside from initializing the session? I am currently doing it like in the sample app (which works fine on both devices)

import Foundation
import SwiftUI
import Foundation
import MultipeerConnectivity


struct tutorialView: View {
    @State var delegate = multidelegate()
    var body: some View {
        Text("Peers connected: \(delegate.multipeerHelp?.connectedPeers.count ?? 999)").onAppear(){
            delegate.setupMultipeer()
        }
    }
}

class multidelegate: MultipeerHelperDelegate{
    var multipeerHelp: MultipeerHelper!
    func setupMultipeer() {
        multipeerHelp = MultipeerHelper(
            serviceName: "demo",
            sessionType: .both,
            delegate: self
        )
    }
    func receivedData(_ data: Data, _ peer: MCPeerID) {
      print(String(data: data, encoding: .unicode) ?? "Data is not a unicode string")
    }

    func peerJoined(_ peer: MCPeerID) {
      print("new peer has joined: \(peer.displayName)")
    }
}
@maxxfrazer
Copy link
Owner

Hi @chaert-s, there's also the adding of NSLocalNetworkUsageDescription and NSBonjourServices to the Info.plist. Have you done that part? It is outlined under Usage in the README:
https://github.com/maxxfrazer/MultipeerHelper#usage

@chaert-s
Copy link
Author

Naturally : )

@maxxfrazer
Copy link
Owner

just thought of something else, you're using @State object for the delegate; try one of these changes:

  • Remove State and just have it as a regular property
  • Use a StateObject instead
  • Or an ObservableObject that has multidelegate as one of its properties.
  • All else fails, put it as a static property for the class (again without state object, observable etc), so it is accessed just with tutorialView.delegate globally.

I'm not the best with SwiftUI, but try putting an initialiser for multidelegate to see how many times it is created.

@chaert-s
Copy link
Author

Thanks for the suggestions, I'll try them out!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants