NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

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

2      

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.

1      

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?

1      

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

1      

Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

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.