使用Swift与Chromecast设备连接/断开连接 [英] Connecting/disconnecting to/from an Chromecast Device with Swift

查看:202
本文介绍了使用Swift与Chromecast设备连接/断开连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用Apple新语言Swift开发和Chromecast应用程序。
但是我一直无法与Chromecast设备建立连接。到目前为止,它可以做什么,它可以在网络上看到Chromecast。之后,将出现一个AlertController(AlertController与ActionSheet相同)之所以使用AlertController,是因为Apple不赞成使用ActionSheet。起初,我认为这是ActionSheet使其无法正常工作的原因。之后,我尝试分配不同版本的ActionController / ActionSheet,但到目前为止还没有运气。作为在Swift
中创建此代码的参考,我使用了Google Cast示例应用,这在Objective C中是什么。
https://github.com/googlecast/CastHelloText-ios

I recently started to develop and Chromecast application with Apple new language Swift. But I'm stuck at making an connection with the Chromecast device. What it does so far that It can see an Chromecast on the network. After that there will appear an AlertController(an AlertController is the same as an ActionSheet) The reason why I use AlertController is, because ActionSheet is deprecated by Apple. At first I thought it would be the ActionSheet thats making it not work. After that I've tried allot of different version of ActionController/ActionSheet, but no luck so far.. As an reference for creating this in Swift I used Google Cast Example apps what is in Objective C. https://github.com/googlecast/CastHelloText-ios

-更新-

在Alertcontroller弹出窗口后,我单击一个设备,该设备将连接并成功。当我尝试断开连接时,出现异常错误 在展开可选值时意外发现nil 。我在此代码行上遇到此错误。

After the Alertcontroller popup I click an device it's then connecting and succeeds. When I'm trying to disconnect it's giving me an exception error "unexpectedly found nil while unwrapping an Optional value". I'm getting this error on this line of code.

self.mediaInformation.metadata.stringForKey(kGCKMetadataKeyTitle)

所以基本上说的是mediaInformation = nil,

So basically it's saying the mediaInformation = nil,

self.mediaInformation.metadata.stringForKey(kGCKMetadataKeyTitle!)

所以我想法让它成为可选项,但这没有用。有人知道为什么它不起作用吗?

so I thought lets make it optional, but that didn't work. Does anyone know why it's not working?

func chooseDevice() {
    if selectedDevice == nil {
        let alertController = UIAlertController(title: "Choose an device..", message: "Click on your chromecast!", preferredStyle: .ActionSheet)

        for device in deviceScanner.devices {
            alertController.addAction(UIAlertAction(title: device.friendlyName, style: .Default, handler: { alertAction in
                self.selectedDevice = device as GCKDevice
                self.connectToDevice()
            }))
        }

        let addCancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: { alertAction in
            alertController.dismissViewControllerAnimated(true, completion: nil)
        })

        // Add action to the controller
        alertController.addAction(addCancelAction)

        // Finaly present the action controller
        presentViewController(alertController, animated: true, completion: nil)
    }
    else {
        updateButtonStates()

        var mediaTitle = GCKMediaInformation()
        mediaTitle.metadata.stringForKey(self.textFieldUrl.text)

        let alertController = UIAlertController(title: "Casting to: \(selectedDevice.friendlyName)", message: nil, preferredStyle: .ActionSheet)

        let addDisconnectingAction = UIAlertAction(title: "Disconnect device", style: .Destructive, handler: { alertAction in
            println("De waarde van mediaInformation is: \(self.mediaInformation)")
            if self.mediaInformation != nil {
                (self.mediaInformation != nil ? 1 : 0)
                alertController.dismissViewControllerAnimated(true, completion: nil)
                println("the else UIAlertController")
            }
        })

        let addCancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: { alertAction in
            println("De waarde van mediaInformation is: \(self.mediaInformation)")
            if self.mediaInformation != nil {
                (self.mediaInformation != nil ? 2 : 1)
                alertController.dismissViewControllerAnimated(true, completion: nil)
                println("else uiactionsheet")
            }
        })

        alertController.addAction(addDisconnectingAction)
        alertController.addAction(addCancelAction)

        self.presentViewController(alertController, animated: true, completion: nil)
    }
}

这就是我与色度网播。 connectToDevice() deviceManagerDidConnect()可能有问题吗?令人讨厌的是,我从未在 deviceManagerDidConnect()

This is how I make an connection with the Chromecast. It could be something wrong in connectToDevice() or in deviceManagerDidConnect()? The weard thing is that I never get the message "Connected" in deviceManagerDidConnect()

func connectToDevice() {
    if selectedDevice != nil {
        var info = NSBundle.mainBundle().infoDictionary?["CFBundleVersion"] as? String
        deviceManager = GCKDeviceManager(device: selectedDevice, clientPackageName: info)

        NSLog("De waarde van info: \(info)")
        NSLog("De waarde van deviceManager in connectToDevice() is: \(deviceManager)")

        deviceManager = GCKDeviceManager(device: deviceScanner.devices[0] as GCKDevice, clientPackageName: info)
        deviceManager.delegate = self
        deviceManager.connect()
    }
}

    func deviceManagerDidConnect(deviceManager: GCKDeviceManager!) {
        NSLog("Connected!")

        updateButtonStates()

        deviceManager.launchApplication(kReceiverAppID)
}


推荐答案

如果您不投射任何媒体,则媒体标题将为nil,请按以下方式使用它:

声明:

Media title will be nil if you are not casting any media so use it in this way:
Declaration:

var mediaInformation : GCKMediaInformation?

用法:

let mediaTitle = self.mediaInformation?.metadata.stringForKey(kGCKMetadataKeyTitle)

这篇关于使用Swift与Chromecast设备连接/断开连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆