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      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.