Last active
August 29, 2015 14:02
-
-
Save scalabl3/e736706b1b38e510fa14 to your computer and use it in GitHub Desktop.
PubNub Objective-C Subscribe to Channel and Add Receive Observer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Message Receive Event for All Subscribed Channels | |
- (void)pubnubClient:(PubNub *)client didReceiveMessage:(PNMessage *)msg | |
{ | |
NSLog(@"Message Received:\n Channel:%@\n Message:%@", msg.channel.name, [msg message]); | |
} | |
- (void) subscribeToChannel:(NSString*)channelName | |
{ | |
PNChannel* channel = [PNChannel channelWithName:channelName]; | |
// Subscribe to the Channel, on Subscribe, Create Observer | |
[PubNub subscribeOnChannel:pnChannel withCompletionHandlingBlock:^(PNSubscriptionProcessState state, NSArray *a, PNError *e) { | |
switch(state) { | |
case PNSubscriptionProcessWillRestoreState: | |
break; | |
case PNSubscriptionProcessRestoredState: | |
break; | |
// Subscribe Completed & Succeeded | |
case PNSubscriptionProcessSubscribedState: | |
NSLog(@"Subscribed to \"%@\" channel... done!", channelName); | |
// Specific Channel Observer Pattern (Add Observer) | |
[[PNObservationCenter defaultCenter] | |
addMessageReceiveObserver:nil | |
withBlock:^(PNMessage * msg) | |
{ | |
if ([msg.channel.name isEqualToString:channelName]) { | |
// Do Stuff with Message Here | |
NSLog(@"\"%@\" Received Message:\n Message:%@", msg.channel.name, [msg message]); | |
} | |
break; | |
// Subscribe Failed | |
case PNSubscriptionProcessNotSubscribedState: | |
NSLog(@"Subscribing to \"%@\" channel... error!", channelName); | |
break; | |
} | |
}]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment