FREE TRIAL: Accelerate your app development career with Hacking with Swift+! >>

Day 83, Day 84: Suggest fix for Project 25 code not letting you connect to another hosting peer

Forums > 100 Days of Swift

Disclaimer: This fix is working for me as of August 2021

So I was following the original tutorial and by the end of day 84 was still not able to get the code working 😅. After browsing our forum, googling and StackOverflowing, I’ve figured a solution to fix the issue of it not letting you connect to the session hosting device:

  1. In Info.plist, make sure you have these 3 keys:
    • "Privacy Local Network Usage Description": "Insert your own description here...."
    • "Bonjour services" in that there’re 2 child keys:
      • Item 0: _hws-project25._tcp
      • Item 1: _hws-project25._udp

(note the underscores (_) in those keys)

  1. In your main ViewController, make these edits:

// in your properties list
var mcAdAssistant: MCNearbyServiceAdvertiser?

// in your startHosting(:) code
func startHosting(via action: UIAlertAction) {
    guard mcSession != nil else { return }
    mcAdAssistant = MCNearbyServiceAdvertiser(peer: peerID, discoveryInfo: nil, serviceType: MCConnectivity.serviceID)
    mcAdAssistant?.delegate = self
    mcAdAssistant?.startAdvertisingPeer()
}

// also just in case if you decided to go a step further and have a stopSession(:) method
func stopSession(via action: UIAlertAction) {
    mcAdAssistant?.stopAdvertisingPeer()
    mcSession?.disconnect()
}

// MOST IMPORTANT PART TO THE FIX
// conform your VC to MCNearbyServiceAdvertiserDelegate
// then implement this method didReceiveInvitationFromPeer(:)

func advertiser(_ advertiser: MCNearbyServiceAdvertiser, didReceiveInvitationFromPeer peerID: MCPeerID, withContext context: Data?, invitationHandler: @escaping (Bool, MCSession?) -> Void) {

    let ac = UIAlertController(title: title, message: "\(peerID.displayName) wants to connect", preferredStyle: .alert)

    ac.addAction(UIAlertAction(title: "Allow", style: .default, handler: { [weak self] _ in
        invitationHandler(true, self?.mcSession)
    }))

    ac.addAction(UIAlertAction(title: "Decline", style: .cancel, handler: { _ in
        invitationHandler(false, nil)
    }))

    present(ac, animated: true)
}
  1. In my case, there was no need to alter the code of my mcBrowser, AppDelegate.swift, or Application Scene Manifest keys of Info.plist whatsoever (thank God 😅)

Bonus: In case you wanna check out my code, here you are 🤗. Happy coding fellas 🥳👋🏻

3      

@mihll  

Your fix is a godsend! Thank you so much for posting it! Works great on iOS 14.8. Unfortunatelly there is another bug, that displays hosting device in the "invitee" list multiple times. But other than that, it works. Thanks again!

1      

Hello! Thanks for sharing your approach, I am also trying to solve this.

Quick questions:

  1. Did you end up using MCBrowserViewController or did you change it to MCNearbyServiceBrowser ?
  2. Isn't Item 1: _hws-project25._udp instead of "_hws-project25._ucp" (I think there might be a typo)
  3. When I implement the func disconnectFromSession(action: UIAlertAction) to try solving the first part of the challenge, I get a fatal error, have you experienced this?

This is the error: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Modifications to the layout engine must not be performed from a background thread after it has been accessed from the main thread.'

Thanks in advance for your attention. KB

1      

Hi @KeyBarreto, thank you for your reply.

1) I was still using MCBrowserViewController. It works fine, as mentioned there was no need to fix the code in mcBrowser part of the tutorial.

2) Ah shucks yes it was a typo. Thank you for pointing it out ☺️

3) Your func disconnectFromSession(action: UIAction) sounds a bit like my func stopSession(via action: UIAlertAction). If so, kindly refer to my solution code up top. However, judging from the log you posted, it seems like a threading issue.

Also feel free to check out my code on GitHub. Link to it is at the bottom of my original post.

   

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for iOS devs who want to become complete senior developers — from October 18th to 24th. Learn how to apply iOS app architecture patterns through a series of lectures and practical coding sessions.

Learn more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.