【发布时间】:2014-12-11 12:45:58
【问题描述】:
我最近开始使用 Apple 新语言 Swift 开发和 Chromecast 应用程序。 但我一直无法与 Chromecast 设备建立连接。到目前为止它做了什么,它可以在网络上看到 Chromecast。之后会出现一个AlertController(一个AlertController 和一个ActionSheet 一样) 我之所以使用AlertController 是因为ActionSheet 被Apple 弃用了。起初我以为是 ActionSheet 让它不起作用。之后我尝试了不同版本的 ActionController/ActionSheet,但到目前为止没有运气。作为在 Swift 中创建它的参考 我使用了目标 C 中的 Google Cast 示例应用程序。 https://github.com/googlecast/CastHelloText-ios
--更新--
在弹出警报控制器后,我单击一个设备,然后它正在连接并成功。当我尝试断开连接时,它给了我一个异常错误“unexpectedly found nil while unwrapping an Optional value”。我在这行代码中遇到了这个错误。
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() 可能有问题?磨损的是我从未在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)
}
【问题讨论】:
-
苦苦挣扎,能否请您在 github 上发布演示代码?我看不到共享应用程序屏幕的适当示例(我不需要播放视频)。请..
标签: ios swift chromecast