Hello,
I am experiencing the same issue. I have a game that uses the camera, and when the user plays quickly while the Game Center "Hello" animation is active, the camera won't start or will freeze. It only resumes functioning once the Game Center animation has completed.
Below is a straightforward sample code to reproduce and test the issue:
//
// ViewController.swift
// ShowCameraIssue
//
// Created by Dylan Absil on 02/12/2024.
//
import UIKit
import AVFoundation
import GameKit
class ViewController: UIViewController {
private var captureSession: AVCaptureSession!
private var previewLayer: AVCaptureVideoPreviewLayer!
override func viewDidLoad() {
super.viewDidLoad()
handleCameraAuthorization()
}
func beginTest() {
self.setupCameraPreview()
DispatchQueue.global(qos: .userInitiated).async {
self.captureSession.startRunning()
}
DispatchQueue.main.async {
self.gameCenterLogin()
}
}
private func handleCameraAuthorization() {
switch AVCaptureDevice.authorizationStatus(for: .video) {
case .authorized:
beginTest()
case .notDetermined:
AVCaptureDevice.requestAccess(for: .video) { granted in
if granted {
self.beginTest()
}
}
default:
print("Camera access denied or restricted.")
}
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
previewLayer.frame = self.view.bounds
}
}
extension ViewController { // CAMERA
private func setupCameraPreview() {
captureSession = AVCaptureSession()
captureSession.sessionPreset = .high
guard let frontCamera = AVCaptureDevice.default(.builtInWideAngleCamera,
for: .video,
position: .front) else {
return
}
do {
let input = try AVCaptureDeviceInput(device: frontCamera)
if captureSession.canAddInput(input) {
captureSession.addInput(input)
}
} catch {
return
}
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer.videoGravity = .resizeAspectFill
previewLayer.frame = self.view.bounds
self.view.layer.addSublayer(previewLayer)
}
}
extension ViewController { // GAME CENTER
func gameCenterLogin() {
GKLocalPlayer.local.authenticateHandler = { viewController, error in
if let err = error {
print(err)
}
else if let controller = viewController {
self.present(controller, animated: true, completion: nil)
}
}
}
}