检测应用程序中Google chromecast服务的使用情况

时间:2019-06-05 13:16:46

标签: ios swift frameworks chromecast

我正在尝试检测图书馆中googles chromecast服务的使用情况。有人找到实现此目标的方法吗?可以通过以下方式检测iOS设备当前是否正在流式传输到AppleTv:

UIScreen.didConnectNotification, UIScreen.didDisconnectNotification

但是当an App投射到chromecast时,这两个通知都不会触发。

1 个答案:

答案 0 :(得分:3)

使用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
    }
}