ios - 在应用程序中检测 Googles chromecast 服务的使用情况

标签 ios swift frameworks chromecast

我正在尝试检测图书馆内 google 的 chromecast 服务的使用情况。有没有人找到一种方法来完成这个?可以通过以下方式检测 iOS 设备当前是否正在流式传输到 AppleTv:

UIScreen.didConnectNotification, UIScreen.didDisconnectNotification

但是当 ann App 正在转换到 chromecast 时,这两个通知都不会被触发。

最佳答案

使用 USB-C 端口将 iPad 连接到外部显示器以执行创意任务。这是一项鲜为人知的功能,所有 iOS 设备均已存在。

使用这一小段代码,您可以监听显示器的连接/断开连接,并为外部显示器设置单独的窗口和 View Controller 层次结构,以扩充应用的主要内容。

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

关于ios - 在应用程序中检测 Googles chromecast 服务的使用情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56461287/

相关文章:

iOS:for循环中对象的属性为空?

ios - 表格 View 单元格未在模拟器中显示

ios - 当前位置的注释图钉未出现

swift - 了解 Spritekit 运行 block ,变量不符合预期

ios - $T7?? Swift 中没有成员 'Generator'

ios - 如何在不复制项目的情况下嵌入框架

asp.net - 用于开发新的Windows Azure管理门户的框架?

iphone - iPad 和 MapKit - 无法拖动可拖动标记

swift - iPhone X、iPhone XS 等相机变焦问题

ios - Xcode: "No such module"嵌入式框架