Pull request Compare This branch is 679 commits behind pubnub:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Pubnub 3.3

Pubnub is an iOS ARC support Objective-C library wrapper for the Pubnub realtime messaging service Pubnub.com.

Using Pubnub in your project

  1. Add the Cipher.h/m,Base64.h/m,JSONKit.h/m,JSONE.h/m and Common.h/m supported file to your project

  2. Add CEPubnub.h/.m to your project

  3. Import CEPubnub.h to your class header

    #import "CEPubnub.h"
  4. If you wish to consider connection retry interval and retry cycle settings (for example, when the network connection is temporarily lost): Configure values in CEPubnub.m for max retry cycles (default = -1, infinite) and delay between retries in seconds (default = 5)

    #define kMinRetryInterval 5.0 //In seconds
    #define kMinRetry -1
  5. Make your class follow the PubNubDelegate protocol

    @interface iPhoneTest : UIViewController <CEPubnubDelegate>
  6. Implement the CEPubnubDelegate methods (they are all optional):

    - (void) pubnub:(CEPubnub*)pubnub didSucceedPublishingMessageToChannel:(NSString*)channel withResponce: (id)responce;
    - (void) pubnub:(CEPubnub*)pubnub didFailPublishingMessageToChannel:(NSString*)channel error:(NSString*)error message:(id)message;  // "error" may be nil
    - (void)pubnub:(CEPubnub *)pubnub subscriptionDidReceiveDictionary:(NSDictionary *)message onChannel:(NSString *)channel;
    - (void)pubnub:(CEPubnub *)pubnub subscriptionDidFailWithResponse:(NSString *)message onChannel:(NSString *)channel;
    - (void)pubnub:(CEPubnub *)pubnub subscriptionDidReceiveString:(NSString *)message onChannel:(NSString *)channel;
    - (void)pubnub:(CEPubnub *)pubnub subscriptionDidReceiveArray:(NSArray *)message onChannel:(NSString *)channel;
    - (void) pubnub:(CEPubnub*)pubnub didFetchHistory:(NSArray*)messages forChannel:(NSString*)channel;
    - (void) pubnub:(CEPubnub*)pubnub didFailFetchHistoryOnChannel:(NSString*)channel;
    - (void) pubnub:(CEPubnub*)pubnub didFetchDetailedHistory:(NSArray*)messages forChannel:(NSString*)channel;
    - (void) pubnub:(CEPubnub*)pubnub didFailFetchDetailedHistoryOnChannel:(NSString*)channel withError:(id)error;
    - (void) pubnub:(CEPubnub*)pubnub didReceiveTime:(NSTimeInterval)time;
    - (void) pubnub:(CEPubnub*)pubnub ConnectToChannel:(NSString*)channel ;
    - (void) pubnub:(CEPubnub*)pubnub DisconnectToChannel:(NSString*)channel ;
    - (void) pubnub:(CEPubnub*)pubnub Re_ConnectToChannel:(NSString*)channel ;
    - (void)pubnub:(CEPubnub *)pubnub presence:(NSDictionary *)message onChannel:(NSString *)channel;
  7. Instantiate Pubnub in your class so you can publish and subscribe to messages and assign delegate:self:

    pubnub = [[CEPubnub alloc] initWithPublishKey:@"demo" subscribeKey:@"demo" secretKey:@"demo" cipherKey:nil useSSL:NO];   // cipherKey is optional, supply nil to disable.
    [pubnub setDelegate:self];
  8. Publish NSString:

    NSString * text=@"My name is pubnub";[pubnub publish:[NSDictionary dictionaryWithObjectsAndKeys:@"hello_world",@"channel",text,@"message", nil]];
  9. Publish NSArray:

    [pubnub publish:[NSDictionary dictionaryWithObjectsAndKeys:@"hello_world",@"channel",[NSArray arrayWithObjects:@"seven", @"eight", 
    [NSDictionary dictionaryWithObjectsAndKeys:@"Cheeseburger",@"food",@"Coffee",@"drink", nil], nil],@"message", nil]];
  10. Publish NSDictionaries:

    [pubnub publishMessage:[NSDictionary dictionaryWithObjectsAndKeys:
    [NSDictionary dictionaryWithObjectsAndKeys:@"X-code->ÇÈ°∂@#$%^&*()!",@"Editer",@"Objective-c",@"Language", nil],@"message",
  11. Subscribe to multiple message channels:

    [pubnub subscribe: @"hello_world_1"];
    [pubnub subscribe: @"hello_world_2"];
    [pubnub subscribe: @"hello_world_3"];
  12. Get a history of messages on a channel: (Deprecated, See Detailed History Below)

    NSInteger limit = 3;
    NSNumber * aWrappedInt = [NSNumber numberWithInteger:limit];
    [pubnub fetchHistory:[NSDictionary dictionaryWithObjectsAndKeys: aWrappedInt,@"limit", @"hello_world",@"channel",nil]];
  13. Detailed History: Load Previously Published Messages in Detail.

    [pubnub detailedHistory:[NSDictionary dictionaryWithObjectsAndKeys: aCountInt,@"count", @"hello_world",@"channel", nil]];

Required args:

'channel' - The channel name

Optional args:

'count' - Max number of returned results. Default and max is 100.
'start' - Start timetoken
'end' - End timetoken
'reverse' - Default is false, which is oldest first. Use true to return newest first.
  1. Get the time, Time receive in NSTimeInterval:

    [pubnub getTime];
  2. Get UUID:

    NSLog(@"UUID::: %@",[CEPubnub getUUID]);
  3. here_now: Ability to get count of subscribed programs to a particular channel.

    [pubnub here_now: @"hello_world"];

  4. Presence: To join a subscriber list on a channel. Callback events can be, Join - Shows availability on a channel or Leave - Disconnected to channel means removed from the list of subscribers.

    [pubnub presence: @"hello_world"];

Tests and an example Xcode project has been included in the project which uses all the above examples.