Swift Pusher 示例什么都不做
Swift Pusher example doing nothing
我正在使用 Pusher 提供的入门代码,当我将其放入基本 swift 项目并向其发送一条消息时,没有任何反应。我也安装了 cocoa pod 包。
应打印“收到数据”语句。
也没有错误。
您是否将 Pusher 对象初始化为 var?您需要维护对该对象的强引用,否则它可能会被释放。
例如,它应该是这样的:
class ViewController: UIViewController {
var pusher: Pusher!
override func viewDidLoad() {
super.viewDidLoad()
let options = PusherClientOptions(
host: .cluster("mycluster")
)
pusher = Pusher(
key: "app_key",
options: options
)
// subscribe to channel and bind to event
let channel = pusher.subscribe("my-channel")
let _ = channel.bind(eventName: "my-event", callback: { (data: Any?) -> Void in
if let data = data as? [String : AnyObject] {
if let message = data["message"] as? String {
print(message)
}
}
})
pusher.connect()
}
}
我正在使用 Pusher 提供的入门代码,当我将其放入基本 swift 项目并向其发送一条消息时,没有任何反应。我也安装了 cocoa pod 包。
应打印“收到数据”语句。
也没有错误。
您是否将 Pusher 对象初始化为 var?您需要维护对该对象的强引用,否则它可能会被释放。
例如,它应该是这样的:
class ViewController: UIViewController {
var pusher: Pusher!
override func viewDidLoad() {
super.viewDidLoad()
let options = PusherClientOptions(
host: .cluster("mycluster")
)
pusher = Pusher(
key: "app_key",
options: options
)
// subscribe to channel and bind to event
let channel = pusher.subscribe("my-channel")
let _ = channel.bind(eventName: "my-event", callback: { (data: Any?) -> Void in
if let data = data as? [String : AnyObject] {
if let message = data["message"] as? String {
print(message)
}
}
})
pusher.connect()
}
}