Brian.Masse2
Brian.Masse2

Reputation: 45

Methods "matchmakerViewControllerWasCancelled" and "matchmakerViewController" not being called inside of GKMatchmakerViewControllerDelegate

I am making a real time multiplayer game with swift, SpriteKit and most importantly GameKit, however I am fairly new to swift and swift UI.

I have initialized my GKMatchmakerViewController with a GKrequest, and believe that I have set that particular view controller's delegate to be the view controller it is inside of. Doing this required me to conform the larger view controller to the protocol of GKMatchmakerViewControllerDelegate which gave me the functions matchmakerViewControllerWasCancelled and matchmakerViewController:didFailWithError: which should be called when either I cancel out of the matchmaking controller or it crashes with an error.

My problem is that neither of these functions seem to be getting called unless I do it manually. I am not entirely sure if I have properly set the controller delegate, which I believe to be the root of the problem, however if anyone has any, more experienced, insight I would gladly appreciate it. The conforming View Controller's code is shown below. All help is appreciated!

import UIKit
import GameKit

let request = GKMatchRequest()

class SecondScreenViewController : UIViewController, UINavigationControllerDelegate, GKMatchmakerViewControllerDelegate {

    let button = UIButton()
    let softRed = UIColor(red: 1, green: 61 / 255, blue: 61 / 255, alpha: 1)
    let softblue = UIColor(red: 38 / 255, green: 149 / 255, blue: 1, alpha: 1)
    
    let TestMatchMaker = GKMatchmaker()
    let TestMatch = GKMatch()
    var TestViewController = GKMatchmakerViewController(matchRequest: request)

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupButton()
        SetupRequest()
        view.backgroundColor = softblue
    }
    
    func matchmakerViewControllerWasCancelled(_ viewController: GKMatchmakerViewController) {
       dismiss(animated: true, completion: nil)
    }
    
    func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFailWithError error: Error) {
        
    }
    
    func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFind match: GKMatch) {
        print("yess")
    }
    
    func SetupRequest() {
        request.minPlayers = 2
        request.maxPlayers = 2
        request.inviteMessage = "test Invite"
    }

    
    func setupButton() {
         button.backgroundColor = UIColor.white
         button.setTitleColor(softblue, for: .normal)
         button.setTitle("MatchMake", for: .normal)
         button.addTarget(self, action: #selector(ButtonTapped), for: .touchUpInside)
         
         view.addSubview(button)
         setupButtonConstraints()
     }
     
     func setupButtonConstraints() {
         button.translatesAutoresizingMaskIntoConstraints = false
         button.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 30).isActive = true
         button.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -30).isActive = true
         button.heightAnchor.constraint(equalToConstant: 100).isActive = true
         button.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 0).isActive = true
         
     }
     
     @objc func ButtonTapped() {
        
        
        present(TestViewController!, animated: true, completion: nil)
        TestViewController!.delegate = self
        
     }
}

Upvotes: 1

Views: 119

Answers (1)

adelmachris
adelmachris

Reputation: 462

I had the same issue. After some research into GKMatchmakerViewController it turned out that you have to set the .matchmakerDelegate instead of .delegate in the makeUIViewController method of your UIViewControllerRepresentable.

import SwiftUI
import GameKit

struct MatchmakerView: UIViewControllerRepresentable {
    
    func makeUIViewController(context: Context) -> UIViewController {
        let matchRequest = GKMatchRequest()
        
        ///Set up the match request according to the game
        matchRequest.minPlayers = 2
        matchRequest.maxPlayers = 4
        matchRequest.defaultNumberOfPlayers = 2
        matchRequest.inviteMessage = "CustomInviteMessage"
                    
        guard let matchmakerVC = GKMatchmakerViewController(matchRequest: matchRequest) else {
            #warning("Implement AlertViewController if match could not be created")
            return UIViewController()
        }
        
        matchmakerVC.matchmakerDelegate = context.coordinator
        
        return matchmakerVC
    }
    
    func updateUIViewController(_ uiViewController: UIViewController, context: Context) {}
    
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }
    
    final class Coordinator: NSObject, GKMatchmakerViewControllerDelegate, UINavigationControllerDelegate {
        init(_ parent: MatchmakerView) {
            self.parent = parent
        }
        
        var parent: MatchmakerView
        
        func matchmakerViewControllerWasCancelled(_ viewController: GKMatchmakerViewController) {
            viewController.dismiss(animated: true)
        }
        
        func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFailWithError error: Error) {
            //Implement error handling
            fatalError("MatchMakingVC failed")
        }
    }
}

Here's the Apple documentation regarding this: https://developer.apple.com/documentation/gamekit/gkmatchmakerviewcontroller/1492426-matchmakerdelegate

Upvotes: 1

Related Questions