SelectPubNub Xamarin.iOS SDK 3.8.7

These docs are for version 3.8.7 of the Xamarin.iOS SDK. To check out the docs for the latest version Click Here.
 

PubNub 4.0 for C# is our latest and greatest! Please click here for our PubNub 4.0 for C# docs and SDK.

PubNub 3.x for C# will encounter End Of Life (EOL) Aug 1st, 2018. All users currently on 3.x should begin migrating to 4.x. The 3.x SDK will continue to work. We will no longer support it and if you run into an issue, we will request that you upgrade to the latest version before solving the issue.

If you have questions about the PubNub for C# SDK, please contact us at support@pubnub.com.

 
For iOS 9.0 Compatibility, you need to set SSL as true when initiating the Pubnub instance. Please see this on how to enable SSL.




Add the code from the folder PubNubMessaging to your project and then use this
using PubNubMessaging.Core;
 
If you are using Visual Studio or XamarinStudio on Windows environment please ensure that you target Mono framework or add __MonoCS__ to Project settings -> Build -> Conditional compilation symbols.
Pubnub pubnub = new Pubnub( "demo", "demo");

pubnub.Subscribe<string>(
	channel:"my_channel", 
	DisplaySubscribeReturnMessage, 
	DisplaySubscribeConnectStatusMessage, 
	DisplayErrorMessage); 

void DisplaySubscribeConnectStatusMessage(string connectMessage) {
    Console.WriteLine("SUBSCRIBE CONNECT CALLBACK");
    pubnub.Publish<string>(
		channel:"my_channel", 
		publishMsg:"Hello from the PubNub C# SDK", 
		DisplayReturnMessage, 
		DisplayErrorMessage); 
}

void DisplaySubscribeReturnMessage(string result) {
    Console.WriteLine("SUBSCRIBE REGULAR CALLBACK:"); 
    Console.WriteLine(result);
    if (!string.IsNullOrEmpty(result) && !string.IsNullOrEmpty(result.Trim()))
            {
                List<object> deserializedMessage = pubnub.JsonPluggableLibrary.DeserializeToListOfObject(result);
                if (deserializedMessage != null && deserializedMessage.Count > 0)
                {
                    object subscribedObject = (object)deserializedMessage[0];
                    if (subscribedObject != null)
                    {
                        //IF CUSTOM OBJECT IS EXCEPTED, YOU CAN CAST THIS OBJECT TO YOUR CUSTOM CLASS TYPE
                        string resultActualMessage = pubnub.JsonPluggableLibrary.SerializeToJsonString(subscribedObject);
                   }
                }
            }
}

void DisplayErrorMessage(PubnubClientError pubnubError)
{
    Console.WriteLine(pubnubError.StatusCode);
}

void DisplayReturnMessage(string result)
{
    Console.WriteLine("PUBLISH STATUS CALLBACK");
    Console.WriteLine(result);
}
In addition to the Hello World sample code, we also provide some copy and paste snippets of common API functions:
Instantiate a new Pubnub instance. Only the subscribeKey is mandatory. Also include publishKey if you intend to publish from this instance, and the secretKey if you wish to perform PAM administrative operations from this Xamarin.iOS instance.
 

It is not a best practice to include the secret key in client-side code for security reasons.

When you init with secretKey, you get root permissions for the Access Manager. With this feature you don't have to grant access to your servers to access channel data. The servers get all access on all channels.

Pubnub pubnub = new Pubnub("demo", "demo");
Call Time to verify the client connectivity to the origin:
pubnub.Time<string>(
    DisplayReturnMessage,
    DisplayErrorMessage
);
void DisplayReturnMessage(string result)
{
    Console.WriteLine("TIME CALLBACK");
    Console.WriteLine(result);
}
void DisplayErrorMessage(PubnubClientError pubnubError)
{
    Console.WriteLine(pubnubError.StatusCode);
}
Subscribe (listen on) a channel (it's async!):
pubnub.Subscribe<string>(
   "my_channel",
    DisplaySubscribeReturnMessage,
    DisplaySubscribeConnectStatusMessage,
    DisplayErrorMessage
);
void DisplaySubscribeReturnMessage(string result) {
    Console.WriteLine("SUBSCRIBE REGULAR CALLBACK:"); 
    Console.WriteLine(result);
    if (!string.IsNullOrEmpty(result) && !string.IsNullOrEmpty(result.Trim()))
            {
                List<object> deserializedMessage = pubnub.JsonPluggableLibrary.DeserializeToListOfObject(result);
                if (deserializedMessage != null && deserializedMessage.Count > 0)
                {
                    object subscribedObject = (object)deserializedMessage[0];
                    if (subscribedObject != null)
                    {
                        //IF CUSTOM OBJECT IS EXCEPTED, YOU CAN CAST THIS OBJECT TO YOUR CUSTOM CLASS TYPE
                        string resultActualMessage = pubnub.JsonPluggableLibrary.SerializeToJsonString(subscribedObject);
                   }
                }
            }
}
void DisplaySubscribeConnectStatusMessage(string result)
{
    Console.WriteLine("SUBSCRIBE CONNECT CALLBACK");
}
void DisplayErrorMessage(PubnubClientError pubnubError) {
    Console.WriteLine(pubnubError.StatusCode);
}
Publish a message to a channel:
pubnub.Publish<string>(
      channel="my_channel",
      publishMsg="Hello from the PubNub C# SDK!",
      DisplayReturnMessage,
      DisplayErrorMessage
);
void DisplayReturnMessage(string result)
{
    Console.WriteLine("PUBLISH STATUS CALLBACK");
    Console.WriteLine(result);
}
void DisplayErrorMessage(PubnubClientError pubnubError) {
    Console.WriteLine(pubnubError.StatusCode);
}
Get occupancy of who's here now on the channel by UUID:
Requires that the Presence add-on is enabled for your key. How do I enable add-on features for my keys? - see http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys
pubnub.HereNow <string>(
    "my_channel",
    DisplayReturnMessage,
    DisplayErrorMessage
);
Subscribe to realtime Presence events, such as join, leave, and timeout, by UUID. Setting the presence attribute to a callback will subscribe to presents events on my_channel:
Requires that the Presence add-on is enabled for your key. How do I enable add-on features for my keys? - see http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys
pubnub.Presence<string>(
    channel = "my_channel",
    DisplayPresenceReturnMessage,
    DisplayPresenceConnectStatusMessage,
    DisplayErrorMessage1);

void DisplayPresenceReturnMessage(string result) {
	Console.WriteLine("PRESENCE REGULAR CALLBACK:");
	Console.WriteLine(result);
}
void DisplayPresenceConnectStatusMessage(string result) {
	Console.WriteLine("PRESENCE CONNECT CALLBACK:");
	Console.WriteLine(result);
}
void DisplayErrorMessage1(PubnubClientError pubnubError) {
	Console.WriteLine(pubnubError.StatusCode);
}
Retrieve published messages from archival storage:
Requires that the Storage and Playback add-on is enabled for your key. How do I enable add-on features for my keys? - see http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys
// retrieve last 100 messages

pubnub.DetailedHistory<string>(
    "history_channel",
    100,
    DisplayReturnMessage,
    DisplayErrorMessage);
void DisplayReturnMessage(string result)
{
    Console.WriteLine(result);
    if (!string.IsNullOrEmpty(result) && !string.IsNullOrEmpty(result.Trim()))
            {
                List<object> deserializedMessage = pubnub.JsonPluggableLibrary.DeserializeToListOfObject(result);
                if (deserializedMessage != null && deserializedMessage.Count > 0)
                {
                    object[] message = pubnub.JsonPluggableLibrary.ConvertToObjectArray(deserializedMessage[0]);
                    if (message != null)
                    {
                        if (message.Length >= 0)
                        {
                            foreach (object item in message)
                            {
                                Console.WriteLine(item);
    //IF CUSTOM OBJECT IS EXCEPTED, YOU CAN CAST THIS OBJECT TO YOUR CUSTOM CLASS TYPE
                            }
                        }
                    }
                }
            }
}
void DisplayErrorMessage(PubnubClientError pubnubError)
{
    Console.WriteLine(pubnubError.StatusCode);
}
Stop subscribing (listening) to a channel.
pubnub.Unsubscribe<string>(
    channel="my_channel",
    DisplayReturnMessage,
    DisplaySubscribeConnectStatusMessage,
    DisplaySubscribeDisconnectStatusMessage,
    DisplayErrorMessage
);