我最近开始使用Apple新语言Swift开发和Chromecast应用程序。 但我一直坚持与Chromecast设备建立联系。到目前为止,它可以在网络上看到Chromecast。之后会出现一个AlertController(一个AlertController与ActionSheet相同)我之所以使用AlertController,是因为Apple不推荐使用ActionSheet。起初我认为将是ActionSheet使它无法正常工作。之后我尝试了不同版本的ActionController / ActionSheet,但到目前为止没有运气..作为在Swift中创建它的参考 我在Objective C中使用了Google Cast示例应用程序。 https://github.com/googlecast/CastHelloText-ios
- 的更新 -
在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()
或deviceManagerDidConnect()
可能有问题吗?很遗憾的是,我从来没有收到消息" Connected"在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)
}
答案 0 :(得分:0)
我认为你需要在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()
}))
}
答案 1 :(得分:0)
如果您没有播放任何媒体,媒体标题将为零,因此请以这种方式使用它:
声明:
var mediaInformation : GCKMediaInformation?
用法:的
let mediaTitle = self.mediaInformation?.metadata.stringForKey(kGCKMetadataKeyTitle)