create a match in gamekit and convert it to swiftui in swift

To create a match using GameKit, you can follow these steps:

  1. Import the GameKit framework and implement the GKMatchmakerViewControllerDelegate protocol.
main.swift
import GameKit

class MatchmakingViewController: UIViewController, GKMatchmakerViewControllerDelegate {
    
    // your code here
    
}
138 chars
8 lines
  1. Create a function that opens a GKMatchmakerViewController instance and presents it to the user.
main.swift
func findMatch() {
    let request = GKMatchRequest()
    request.minPlayers = 2
    request.maxPlayers = 4
    
    let vc = GKMatchmakerViewController(matchRequest: request)
    vc.matchmakerDelegate = self
    
    present(vc, animated: true, completion: nil)
}
265 chars
11 lines

When the user dismisses the view controller, the delegate method matchmakerViewControllerWasCancelled(_:) will be called.

  1. Implement the necessary delegate methods to handle the match and to dismiss the GKMatchmakerViewController.
main.swift
func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFailWithError error: Error) {
    // handle error here
}

func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFind match: GKMatch) {
    let gameScene = GameScene(match: match)
    navigationController?.pushViewController(gameScene, animated: true)
}

func matchmakerViewControllerWasCancelled(_ viewController: GKMatchmakerViewController) {
    viewController.dismiss(animated: true, completion: nil)
}
510 chars
13 lines
  1. Create a GameScene class that accepts a GKMatch instance and implements the necessary methods to handle the match.
main.swift
class GameScene: UIViewController {
    
    let match: GKMatch
    
    init(match: GKMatch) {
        self.match = match
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // your code here
    
}
317 chars
17 lines

You can then use SwiftUI to display the MatchmakingViewController and the GameScene. For example, you can create a MatchmakingView that has a Button to initiate the matchmaking process and a NavigationLink that links to the GameSceneView.

main.swift
struct MatchmakingView: View {
    
    @State private var isPresented = false
    @State private var match: GKMatch?
    
    var body: some View {
        VStack {
            Button("Find Match") {
                isPresented = true
            }
            NavigationLink(destination: GameSceneView(match: match), isActive: .constant(match != nil)) {
                EmptyView()
            }
        }
        .sheet(isPresented: $isPresented) {
            MatchmakingViewControllerWrapper(onDismiss: { match in self.match = match })
        }
    }
    
}
564 chars
21 lines

The MatchmakingViewControllerWrapper is a UIViewControllerRepresentable that wraps the MatchmakingViewController and passes the result back to the MatchmakingView.

main.swift
struct MatchmakingViewControllerWrapper: UIViewControllerRepresentable {
    
    let onDismiss: (GKMatch?) -> Void
    
    func makeUIViewController(context: Context) -> MatchmakingViewController {
        let viewController = MatchmakingViewController()
        viewController.matchmakerDelegate = viewController
        return viewController
    }
    
    func updateUIViewController(_ uiViewController: MatchmakingViewController, context: Context) {}
    
    func makeCoordinator() -> Coordinator {
        Coordinator(onDismiss: onDismiss)
    }
    
    class Coordinator: NSObject, GKMatchmakerViewControllerDelegate {
        
        let onDismiss: (GKMatch?) -> Void
        
        init(onDismiss: @escaping (GKMatch?) -> Void) {
            self.onDismiss = onDismiss
        }
        
        func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFind match: GKMatch) {
            onDismiss(match)
            viewController.dismiss(animated: true, completion: nil)
        }
        
        func matchmakerViewControllerWasCancelled(_ viewController: GKMatchmakerViewController) {
            onDismiss(nil)
            viewController.dismiss(animated: true, completion: nil)
        }
        
    }
    
}
1254 chars
38 lines

Finally, you can create a GameSceneView that displays the SKView to render the GameScene.

main.swift
struct GameSceneView: UIViewRepresentable {
    
    let match: GKMatch
    
    func makeUIView(context: Context) -> SKView {
        let view = SKView()
        let gameScene = GameScene(match: match)
        view.presentScene(gameScene)
        return view
    }
    
    func updateUIView(_ uiView: SKView, context: Context) {}
    
}
339 chars
15 lines

This is just one possible way to use GameKit and SwiftUI to create a matchmaking and game scene flow. You can customize it according to your specific needs.

gistlibby LogSnag