UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Project25 Update for iOS 13.4.1

Forums > Books

I'm not sure how far back this problem goes, but for sure there is a problem with the code as-is for iOS 13.4.1. I've read that it is because of the introduction of SceneDelegate but no one has said why.

Regardless, here is the update to Paul's code to make this pretty cool app work!

  1. Change the declaration of the advertising assistant to:

    var mcAdvertiserAssistant: MCNearbyServiceAdvertiser?
  2. Declare that you will conform to a new protocol: MCNearbyServiceAdvertiserDelegate

    class ViewController: UICollectionViewController, UINavigationControllerDelegate, ... , MCNearbyServiceAdvertiserDelegate {
  3. Implement a new function required by the MCNearbyServiceAdvertiserDelegate:

    func advertiser(_ advertiser: MCNearbyServiceAdvertiser, didReceiveInvitationFromPeer peerID: MCPeerID, withContext context: Data?, invitationHandler: @escaping (Bool, MCSession?) -> Void) {
    
        // This line will accept the connection request of anyone with your app asking to join
        invitationHandler(true, mcSession)
    
        // If you want to add an AlertController to allow the host to permit or deny joining, replace the above line with this
        let ac = UIAlertController(title: "Connection Request", message: "User: \(peerID) is requesting to join the network.", preferredStyle: .alert)
        ac.addAction(UIAlertAction(title: "Allow", style: .default) {[weak self] action in
            invitationHandler(true, self?.mcSession)
            })
        ac.addAction(UIAlertAction(title: "Deny", style: .cancel) {[weak self] action in
            invitationHandler(false, self?.mcSession)
            })
        present(ac, animated: true)
    
        }

I think that's it. I found the approach online but noone knew how to add a prompt to decide on allowing the join request. I was able to write this using what I learned from Paul's books! Doubly cool! @PaulHudson

4      

I just briefly checked Project 25 (https://www.hackingwithswift.com/read/25/overview) and I dont see a way why would it not work on iOS 13 with SceneDelegate. Can you elaborate?

SceneDelegate was added with iOS 13 as a way to handle apps with multiple windows (iPadOS and Mac Catalyst) and some of the AppDelegate responsibilities have moved there in updated form. For example handling shortcuts, launchOptions and similar stuff.

3      

I would have been happy for it to work with scenedelegate but it just doesn't. I know that the supplied code is syntactically fine, but XCode/iOS 13.+ just does not like it. Have you tried running the code?

3      

Not in this project but I tried building another one (connecting iPhone with Apple TV via multipeer) and it worked based on sample code on HWS and I did not have to modify anything in SceneDelegate

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.