【发布时间】:2015-02-08 16:54:00
【问题描述】:
我最近开始在 iOS 中使用 Google Cast API,我正在尝试让它与 Apple 新的编程语言 Swift 一起使用。 我在https://github.com/googlecast/CastHelloText-ios 用作参考Google 示例应用程序,问题是当我启动模拟器时,我收到一条错误消息,名为:"-[GCKDeviceManager init]: unrecognized selector sent to instance 0x7fcd7a52d340" I知道我忘记了一些东西,但是我需要把它放在什么地方??
为了更容易帮助我,这是我的代码。
class SettingsController: UIViewController, UITextFieldDelegate, GCKDeviceScannerListener, GCKDeviceManagerDelegate, GCKMediaControlChannelDelegate {
var deviceScanner : GCKDeviceScanner!
var deviceManager : GCKDeviceManager!
var mediaInformation : GCKMediaInformation!
var applicationMetadata : GCKApplicationMetadata!
var mediaControlChannel : GCKMediaControlChannelDelegate!
var selectedDevice : GCKDevice!
var textChannelVar : textChannel!
var chromecastButton : UIButton!
var btnImage : UIImage!
var btnImageSelected : UIImage!
var kReceiverAppID = "642B7ADB"
func chooseDevice() {
if self.selectedDevice == nil {
let alertController = UIAlertController(title: "Choose an device..", message: "Tap on a prefered device", preferredStyle: .ActionSheet)
for selectedDevice in self.deviceScanner.devices {
alertController.addAction(UIAlertAction(title: selectedDevice.friendlyName, style: .Default, handler: { alertAction in
self.connectToDevice()
}))
}
alertController.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: { AlertAction in
alertController.dismissViewControllerAnimated(true, completion: nil)
}))
self.presentViewController(alertController, animated: true, completion: { () -> Void in
if self.selectedDevice != nil {
self.connectToDevice()
}
})
}
else {
self.updateButtonStates()
self.mediaInformation.metadata.stringForKey(kGCKMetadataKeyTitle)
var alertController = UIAlertController(title: "Casting to: \(self.selectedDevice.friendlyName)", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
alertController.addAction(UIAlertAction(title: "Disconnect", style: .Default, handler: { alertAction in
println("de waarde van self.mediaInformation is : \(self.mediaInformation)")
if self.mediaInformation != nil {
println("else uiactionsheet")
(self.mediaInformation != nil ? 1 : 0)
alertController.dismissViewControllerAnimated(true, completion: nil)
}
}))
alertController.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: { alertAction in
if self.mediaInformation != nil {
println("else uiactionsheet")
(self.mediaInformation != nil ? 2 : 1)
alertController.dismissViewControllerAnimated(true, completion: nil)
}
}))
self.presentViewController(alertController, animated: true, completion: nil)
}
}
func isConnected() -> Bool {
return self.deviceManager.isConnected
}
func connectToDevice() {
if self.selectedDevice == nil {
self.deviceManager = GCKDeviceManager(device: self.selectedDevice, clientPackageName:"CFBundleIdentifier")
self.deviceManager.delegate = self
self.deviceManager.connect()
return
}
}
func deviceDisconnected() {
self.deviceManager = nil
self.selectedDevice = nil
self.textChannelVar = nil
NSLog("Device disconneted: \(self.selectedDevice.friendlyName)")
}
func updateButtonStates() {
if (self.deviceScanner.devices.count == 0) {
chromecastButton.setImage(btnImage, forState: .Normal)
chromecastButton.hidden = true
}
else {
chromecastButton.setImage(btnImage, forState: .Normal)
chromecastButton.hidden = false
if (self.deviceManager != nil) {
if self.deviceManager.isConnected {
chromecastButton.setImage(btnImageSelected, forState: .Normal)
}
}
else {
chromecastButton.setImage(btnImageSelected, forState: .Normal)
}
}
}
func deviceDidComeOnline(device: GCKDevice!) {
NSLog("device found! \(device.friendlyName)")
self.updateButtonStates()
}
func deviceDidGoOffline(device: GCKDevice!) {
self.updateButtonStates()
}
func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {
if self.selectedDevice == nil {
if buttonIndex < self.deviceScanner.devices.count {
self.selectedDevice = self.deviceScanner.devices[buttonIndex] as GCKDevice
NSLog("Selecting device: \(self.selectedDevice.friendlyName)")
self.connectToDevice()
}
}
else {
if buttonIndex == 1 {
NSLog("Disconnecting device: \(self.selectedDevice.friendlyName)")
self.deviceManager.leaveApplication()
self.deviceManager.disconnect()
self.deviceDisconnected()
self.updateButtonStates()
}
else if buttonIndex == 0 {
}
}
}
func deviceManagerDidConnect(deviceManager: GCKDeviceManager!) {
NSLog("Connected!")
self.updateButtonStates()
self.deviceManager.launchApplication(kReceiverAppID)
}
func deviceManager(deviceManager: GCKDeviceManager!, didConnectToCastApplication applicationMetadata: GCKApplicationMetadata!, sessionID: String!, launchedApplication: Bool) {
NSLog("application has launched \(launchedApplication)")
}
func deviceManager(deviceManager: GCKDeviceManager!, didFailToConnectToApplicationWithError error: NSError!) {
self.showError(error)
self.deviceDisconnected()
self.updateButtonStates()
}
func deviceManager(deviceManager: GCKDeviceManager!, didFailToConnectWithError error: NSError!) {
self.showError(error)
self.deviceDisconnected()
self.updateButtonStates()
}
func deviceManager(deviceManager: GCKDeviceManager!, didDisconnectWithError error: NSError!) {
NSLog("Received notification that device disconnected")
if error != nil {
self.showError(error)
}
self.deviceDisconnected()
self.updateButtonStates()
}
func showError(error: NSError) {
var alert = UIAlertController(title: "Something went wrong", message: error.description, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
kReceiverAppID = kGCKMediaDefaultReceiverApplicationID
btnImage = UIImage(named: "icon-cast-identified.png")
btnImageSelected = UIImage(named: "icon-cast-connected")
self.chromecastButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
self.chromecastButton.addTarget(self, action: "chooseDevice", forControlEvents: .TouchDown)
self.chromecastButton.frame = CGRectMake(0, 0, 39, 34)
self.chromecastButton.setImage(nil, forState: .Normal)
self.chromecastButton.hidden = true
self.deviceScanner = GCKDeviceScanner()
self.deviceScanner.addListener(self)
self.deviceScanner.startScan()
self.view.addSubview(self.chromecastButton)
}
我的主要目标是在大屏幕上显示网页,我们通常在 webview 中看到的内容。 对不起,长代码。如果您需要更多信息来帮助我,请随时询问。 谢谢!
【问题讨论】:
标签: ios google-cast