Ios 使用Swift连接/断开与Chromecast设备的连接

Ios 使用Swift连接/断开与Chromecast设备的连接,ios,swift,chromecast,Ios,Swift,Chromecast,我最近开始用苹果新语言Swift开发和安装Chromecast应用程序。 但我一直在和Chromecast设备建立连接。到目前为止,它可以在网络上看到Chromecast。之后会出现一个AlertController,AlertController与ActionSheet相同。我使用AlertController的原因是,因为ActionSheet已被Apple弃用。一开始我以为是行动单让它不起作用。在此之后,我尝试分配不同版本的ActionController/ActionSheet,但到目前

我最近开始用苹果新语言Swift开发和安装Chromecast应用程序。 但我一直在和Chromecast设备建立连接。到目前为止,它可以在网络上看到Chromecast。之后会出现一个AlertController,AlertController与ActionSheet相同。我使用AlertController的原因是,因为ActionSheet已被Apple弃用。一开始我以为是行动单让它不起作用。在此之后,我尝试分配不同版本的ActionController/ActionSheet,但到目前为止运气不佳。。作为在Swift中创建此文件的参考 我使用了Google Cast示例应用程序Objective C中的内容。

-更新-

在Alertcontroller弹出窗口后,我单击一个设备,然后它正在连接并成功。当我尝试断开连接时,它会给我一个异常错误,在打开可选值时意外发现nil。我在这行代码中遇到了这个错误

self.mediaInformation.metadata.stringForKey(kGCKMetadataKeyTitle)
所以基本上是说mediaInformation=nil

self.mediaInformation.metadata.stringForKey(kGCKMetadataKeyTitle!)
所以我想让它成为可选的,但那不起作用。有人知道它为什么不起作用吗

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)
    }
}
这就是我如何与Chromecast建立连接的方式。connectToDevice或DeviceManagerdConnection中可能有问题?令人厌倦的是,我从未在设备管理器连接中连接到消息


我认为您需要在处理程序中执行一些操作,以便UIAlertAction设置self.selectedDevice

乙二醇


如果您未铸造任何媒体,则媒体标题将为零,请按以下方式使用: 声明:

用法:


谢谢你的评论,但不是这样:你现在已经改变了问题,所以我的回答与现在的问题内容无关。我在你回答后改变了问题。事实上,你改变了,我看不出你有什么理由不提出另一个问题。谢谢你的回答。但它仍然是nill,这意味着我无法断开与已连接的Chromecast设备的连接。这意味着你仍然收到错误,应用程序正在崩溃?因为现在即使它为零,它也不会抛出任何错误,因为它是可选的。谢谢你的评论。不,它不再崩溃了。我的意思是,代码继续,然后调用UIAlertController,当我在UIAlertController中按device disconnect时,什么也没有发生。这不是断开连接。它以某种奇怪的方式保持着联系。也许是因为MediaTitle还是零?或者连接的设备是否未正确连接?老实说,我不知道该去哪里找。。我迷路了:那么这不是因为媒体标题会解释更多关于这方面的问题,你能在github上发布演示代码吗?我没有看到合适的例子来分享应用程序屏幕,我不需要播放视频。请
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)
}
    for selectedDevice in self.deviceScanner.devices {
        alertController.addAction(UIAlertAction(title: selectedDevice.friendlyName, style: .Default, handler: { action in
            self.selectedDevice = device
            self.connectToDevice()
        }))
    }
var mediaInformation : GCKMediaInformation?
let mediaTitle = self.mediaInformation?.metadata.stringForKey(kGCKMetadataKeyTitle)