在Swift中使用Chromecast初始化 [英] Init with Chromecast in Swift

查看:66
本文介绍了在Swift中使用Chromecast初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始在iOS中使用Google Cast API,并且试图将其与Apple新的编程语言Swift一起使用. 我在 https://github.com/googlecast/CastHelloText-ios并且问题是当我启动模拟器时,我收到一条错误消息:-[GCKDeviceManager init]:无法识别的选择器发送到实例0x7fcd7a52d340" 我知道我忘记了一些东西,但是在哪里做什么我需要放它吗?

I recently started to work with Google Cast API in iOS and I'm trying to get it to work with Apple new programming language Swift. I used as a reference Google Sample apps at https://github.com/googlecast/CastHelloText-ios and the problem is when I start the emulator I get an error message called: "-[GCKDeviceManager init]: unrecognized selector sent to instance 0x7fcd7a52d340" I know that I forgot something, but what and where do I need to put it??

下面是我的代码,它可以帮助我更加轻松.

For making it easer to help me, here is my code.

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

我的主要目标是在大屏幕上显示网页,这是我们通常在网络视图中看到的内容. 很抱歉输入长代码.如果您需要更多帮助我的信息,请随时询问. 谢谢!

What my main goal is to show a webpage on the big screen, what we normally see in an webview. Sorry for the long code. If you need more info for helping me, don't hesitate to ask. Thank you!

推荐答案

我没有使用此库,但是在您提供的链接中,他们正在像这样初始化GCKDeviceManager:

I haven't used this library, but in the link you provide they're initializing the GCKDeviceManager like this:

self.deviceManager =
  [[GCKDeviceManager alloc] initWithDevice:self.selectedDevice
                         clientPackageName:[info objectForKey:@"CFBundleIdentifier"]];

这应该转换为以下快速代码:

This should translate into this swift code:

self.deviceManager = GCKDeviceManager(self.selectedDevice, clientPackageName:info["CFBundleIdentifier"])

这意味着您的看起来像这样的代码无效(或至少是GCKDeviceManager init):

This means your code that looks like this is invalid (or at least the GCKDeviceManager init):

var deviceScanner = GCKDeviceScanner()
var deviceManager = GCKDeviceManager()
var mediaInformation = GCKMediaInformation()
var selectedDevice = GCKDevice()

您应该将其更改为如下形式:

You should change it to look like this instead:

var deviceScanner : GCKDeviceScanner!
var deviceManager : GCKDeviceManager!
var mediaInformation : GCKMediaInformation!
var selectedDevice : GCKDevice!

在访问它们之前,您需要确保已将这些值分配给它们,否则它将崩溃.否则,您可以将类型切换为可选.

You'll need to ensure these values are assigned to before accessing them or it'll crash. Otherwise, you can switch the types to be optionals.

这篇关于在Swift中使用Chromecast初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆