使用Swift连接/断开Chromecast设备

我最近开始用苹果新语言Swift开发Chromecast应用程序。 但我坚持要与Chromecast设备build立连接。 到目前为止,它可以在networking上看到Chromecast。 之后会出现一个AlertController(一个AlertController与ActionSheet相同)我使用AlertController的原因是,因为ActionSheet被Apple弃用。 起初,我认为这将是ActionSheet,使其无法正常工作。 之后,我尝试了不同版本的ActionController / ActionSheet,但没有运气到目前为止。作为在Swift中创build这个的参考,我使用了Google Cast示例应用程序Objective C中的什么。https://github.com/ googlecast / CastHelloText-IOS

更新

Alertcontrollerpopup后,我点击一个设备,然后连接并成功。 当我试图断开它给我一个exception错误“ 意外地发现零,而解包可选值 ”。 我在这行代码中得到这个错误。

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

这就是我如何与Chromecastbuild立联系。 在connectToDevice()deviceManagerDidConnect()可能是错误的? 这个问题是我从来没有在deviceManagerDidConnect()deviceManagerDidConnect() “Connected”消息

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

我想你需要在UIAlertAction的处理程序中设置self.selectedDevice

例如

  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)