Обнаружение использования службы Googles Chromecast в приложении - PullRequest
1 голос
/ 05 июня 2019

Я пытаюсь обнаружить использование службы googles chromecast в библиотеке.Кто-нибудь нашел способ сделать это?Обнаружение того, что устройство iOS в данный момент выполняет потоковую передачу на AppleTv, возможно с помощью:

UIScreen.didConnectNotification, UIScreen.didDisconnectNotification

Но когда приложение Ann выполняет трансляцию в Chromecast, оба эти уведомления не запускаются.

1 Ответ

3 голосов
/ 13 июня 2019

Использование порта USB-C для подключения iPad к внешнему дисплею для творческих задач. Эта функция, о которой мало кто знает, уже существует на всех устройствах iOS.

С помощью этого небольшого количества кода вы можете прослушивать подключение / отключение дисплеев и настраивать отдельное окно и просматривать иерархию контроллера для внешнего дисплея для дополнения основного контента вашего приложения.

import UIKit

class ViewController: UIViewController {

    // For demo purposes. We're just showing a string description
    // of each UIScreen object on each screen's view controller
    @IBOutlet var screenLabel: UILabel!

    static func makeFromStoryboard() -> ViewController {
        return UIStoryboard(name: "Main", 
                            bundle: nil)
            .instantiateInitialViewController() as! ViewController
    }
}

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    // The main window shown on the device's display
    // The main storyboard will set this up automatically
    var window: UIWindow?

    // References to our windows that we're creating
    var windowsForScreens = [UIScreen: UIWindow]()

    // Create our view controller and add text to our test label
    private func addViewController(to window: UIWindow, text: String) {
        let vc = ViewController.makeFromStoryboard()

        // When we need to finish loading the view before accessing
        // the label outlet on the view controller
        vc.loadViewIfNeeded()
        vc.screenLabel.text = text

        window.rootViewController = vc
    }

    // Create and set up a new window with our view controller as the root
    private func setupWindow(for screen: UIScreen) {
        let window = UIWindow()
        addViewController(to: window, text: String(describing: screen))
        window.screen = screen
        window.makeKeyAndVisible()

        windowsForScreens[screen] = window
    }

    // Hide the window and remove our reference to it so it will be deallocated
    private func tearDownWindow(for screen: UIScreen) {
        guard let window = windowsForScreens[screen] else { return }
        window.isHidden = true
        windowsForScreens[screen] = nil
    }

    func application(_ application: UIApplication, 
                     didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
        ) -> Bool {

        // Set up the device's main screen UI
        addViewController(to: window!, text: String(describing: UIScreen.main))

        // We need to set up the other screens that are already connected
        let otherScreens = UIScreen.screens.filter { $0 != UIScreen.main }
        otherScreens.forEach { (screen) in
            setupWindow(for: screen)
        }

        // Listen for the screen connection notification
        // then set up the new window and attach it to the screen
        NotificationCenter.default
            .addObserver(forName: UIScreen.didConnectNotification, 
                         object: nil, 
                         queue: .main) { (notification) in

                            // UIKit is nice enough to hand us the screen object 
                            // that represents the newly connected display
                            let newScreen = notification.object as! UIScreen

                            self.setupWindow(for: newScreen)
        }

        // Listen for the screen disconnection notification.
        NotificationCenter.default.addObserver(forName: UIScreen.didDisconnectNotification, 
                                               object: nil, 
                                               queue: .main) { (notification) in

                                                let newScreen = notification.object as! UIScreen
                                                self.tearDownWindow(for: newScreen)
        }

        return true
    }
}
...