2015-02-09 70 views
2

我想將PubNub納入我的swift應用程序。我已經通過CocoaPods安裝了它,並且未能使基本的「Hello World」應用程序正常工作。無法連接到PubNub從迅速的應用程序

我有符合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,上面應足以得到這個工作。任何人都可以幫助我確定缺少什麼?

謝謝!

編輯:相關信息;我目前正在模擬器上運行它。沒有使用實際設備會有什麼問題嗎?

回答

2

你不想使用PubNub的單版本pubnub的一個實例混合。在上面的代碼中,有時使用實例,有時使用sharedInstance ...引用不一樣。試試這個:(通知subscribeOn..instance)

var pnConfiguration: PNConfiguration! 
var pubNub: PubNub! 
pnConfiguration = PNConfiguration(origin: "pubsub.pubnub.com" 
     ,publishKey: "demo" 
     ,subscribeKey: "demo" 
     ,secretKey: "" 
     ,cipherKey: "") 

pubNub=PubNub.clientWithConfiguration(pnConfiguration,andDelegate:self) 
     PNLogger.loggerEnabled(true) 
     pubNub.connect() 
pubNub.subscribeOn([chnlGroup]) 
pubNub.observationCenter.addMessageReceiveObserver(self){ (message: PNMessage!) -> Void in 
      println("message go instance: { channel: \(message.channel), group: \(message.channelGroup), \nmsg: \(message.message)}"); 
     } 
+0

哇哦,我覺得傻,現在...感謝您指出我的巨大的愚蠢:)這也許可以關閉題外話然後。 – lbrendanl 2015-02-09 17:36:50

+0

我做了完全相同的事情.. took我幾個想出來:) – 2015-02-09 23:18:46

+0

@FrederickBrock你能幫我弄清楚這一點嗎?看來新PubNub它是從PNMessage改爲PNMessageResult http://stackoverflow.com/questions/31920108/access-pnmessageresult-in-pubnub-swift – Nigilan 2015-08-10 13:27:52