我正在尝试将PubNub纳入我的swift应用程序。我已经通过CocoaPods安装了它,但基本的“HelloWorld”应用程序无法正常运行。
我有一个符合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
,以上内容应该足以使此工作正常。有人能帮我确定缺少什么吗?
谢谢
编辑:相关信息;我目前正在模拟器上运行这个。不使用实际设备会有问题吗?