Retrieve content from PubNub messages

ios, nsdictionary, objective-c, pubnub

Solution

@sabin, here is an example, using an observer -- you could do a similar thing via a delegate:

[[PNObservationCenter defaultCenter] addMessageReceiveObserver:self
                                                     withBlock:^(PNMessage *message) {

     id messageData = message.message;
     if ([messageData isKindOfClass:[NSDictionary class]]) {

     NSString *messageString = [NSString stringWithFormat:@"foo: %@, and bar: <%@>",
             [(NSDictionary *)messageData valueForKey:@"foo"],
             [(NSDictionary *)messageData valueForKey:@"bar"]];
     }
 }];

If you then published this from the Web Console (pubnub.com/console):

{"foo":"hey", "bar":"you!"}

You would be able to render a string from the above code (via messageString) that looked like this:

foo: hey, and bar: you!

Let me know if that helped!

Problem

I can successfully send and receive messages with PubNub, the problem comes when i try to display content from a message and load it a `UITableViewCell`'s `UITextView`. The `Second TEST LOG` writes out the whole message, that i send from my iPhone (i've already tried it with the Dev Console), but after this the app crashes. ``` [__NSCFDictionary length]: unrecognized selector sent to instance ``` I know there is something wrong with a dictionary, but i can't figure it out. I'm using only one `NSDictionary` for the message i send via PubNub and it "arrives" to the console, therefore I think it works properly. As you can see in the code i've tried some variations, but without any success. UPDATE It's working if i send NSString instead of NSDictionary. ``` @interface ViewController () @property (nonatomic, strong) NSString *myIncomeMessage; @property (nonatomic, strong) NSString *messageFromDict; @property (nonatomic, strong) NSArray *twoChannels; @property (nonatomic, strong) NSDictionary *messagePbnb; //@property (nonatomic, strong) PNMessage *messageNew; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; PNChannel *channel_2 = [PNChannel channelWithName:current.username shouldObservePresence:NO]; PNChannel *channel_1 = [PNChannel channelWithName:self.messageRecipient shouldObservePresence:NO]; [PubNub subscribeOnChannels:self.twoChannels]; [PubNub requestHistoryForChannel:channel_1 from:nil to:nil limit:100 reverseHistory:YES]; [PubNub requestHistoryForChannel:channel_2 from:nil to:nil limit:100 reverseHistory:YES]; [[PNObservationCenter defaultCenter] addMessageReceiveObserver:self withBlock:^(PNMessage *message) { NSLog(@"OBSERVER: Channel: %@, Message: %@", message.channel.name, message.message); NSLog(@"Sample TEST LOG %@", message.message); self.myIncomeMessage = message.message; NSLog(@"Second TEST LOG %@", self.myIncomeMessage); // self.messageFromDict = [NSString stringWithFormat:keyMessage, self.messagePbnb]; // self.messageFromDict = [NSString stringWithFormat:keyMessage, message]; }]; [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(reloadTable) userInfo:nil repeats:YES]; [self setupUIForInput]; } - (IBAction) inputContent { NSString *messageContent = self.textView.text; PNChannel *channel_1 = [PNChannel channelWithName:self.messageRecipient shouldObservePresence:NO]; PNChannel *channel_2 = [PNChannel channelWithName:senderUser.username shouldObservePresence:NO]; self.twoChannels = @[channel_1,channel_2]; [PubNub subscribeOnChannels: self.twoChannels]; self.messagePbnb = @{ @"keyMessage": messageContent, @"keySenderUser": self.senderUser.username, @"keyRecieverChannel": self.messageRecipient} ; [PubNub sendMessage: self.messagePbnb toChannel:channel_1]; [PubNub sendMessage: self.messagePbnb toChannel:channel_2]; [self.textView resignFirstResponder]; [self reloadInputViews]; } -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 10; } -(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { OutputTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellThree"]; cell.textOutput.text = self.myIncomeMessage; -(void)reloadTable{ [tableViewThree reloadData]; } ```

Original source