【发布时间】:2015-02-09 06:20:21
【问题描述】:
我正在尝试将 PubNub 合并到我的 swift 应用程序中。我已经通过 CocoaPods 安装了它,但无法让基本的“Hello World”应用程序正常工作。
我有一个符合 PNDelegate 协议的简单视图控制器:
class MessageViewController: CustomViewController, PNDelegate
在这个控制器的 ViewDidLoadMethod 中,我添加了以下内容:
var config: PNConfiguration = PNConfiguration(forOrigin: "pubsub.pubnub.com", publishKey: Constants.PubNubPublishKey, subscribeKey: Constants.PubNubSubscribeKey, secretKey: Constants.PubNubSecretKey)
var pubNub: PubNub = PubNub.clientWithConfiguration(config, andDelegate: self)
pubNub.connect()
// Define Channel
var channel: PNChannel = PNChannel.channelWithName("TestChannel", shouldObservePresence: true) as PNChannel
// Subscribe on the channel
PubNub.subscribeOn([channel])
// Subscribe on the channel
PubNub.sendMessage("Hello world", toChannel: channel)
我还在同一个视图控制器中添加了以下协议方法:
func pubnubClient(client: PubNub!, didReceiveMessage message: PNMessage!) {
println(message.message)
}
当我运行应用程序时,大多数时候,一切都执行了,但 didReceiveMessage 函数从未被调用。有时,应用程序崩溃,并显示以下消息:
// Verify that reachability callback was called for correct client
NSCAssert([(__bridge NSObject *)info isKindOfClass:[PNReachability class]],
@"Wrong instance has been sent as reachability observer");
根据基本的PubNub tutorial,以上应该足以让它工作。谁能帮我确定缺少什么?
谢谢!
编辑:相关信息;我目前正在模拟器上运行它。不使用实际设备会有什么问题吗?
【问题讨论】: