简体   繁体   English

在Swift中使用Chromecast初始化

[英]Init with Chromecast in Swift

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. 我最近开始在iOS中使用Google Cast API,并且正在尝试使其与Apple新的编程语言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?? 我在https://github.com/googlecast/CastHelloText-ios上用作Google示例应用程序的参考,问题是当我启动模拟器时,我收到一条错误消息: “-[GCKDeviceManager init]:无法识别的选择器发送到实例0x7fcd7a52d340“,我知道我忘了一些东西,但是我需要在什么地方放呢?

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! 谢谢!

I haven't used this library, but in the link you provide they're initializing the GCKDeviceManager like this: 我没有使用过这个库,但是在您提供的链接中,他们正在像这样初始化GCKDeviceManager:

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"])

This means your code that looks like this is invalid (or at least the GCKDeviceManager init): 这意味着您的看起来像这样的代码无效(或者至少是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. 否则,您可以将类型切换为可选。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM