Skip to content

Ios sample walkthrough

RajeevRShephertz edited this page May 23, 2013 · 6 revisions

We will use a sample project to show the features and how to use the AppWarp SDK.

Lets browse through the source code and walkthrough some of the key concepts to start with AppWarp.

We wrote a helper class, viz. AppWarpHelper, which wraps around the functionality exposed by the AppWarp_iOS_SDK. Open up the source code you downloaded from the above link, and navigate through the class "AppWarpHelper.m". This is a singleton class which has methods to initialize the AppWarp as well as responsible for managing every interaction between the sample project and the SDK. To start with AppWarp you need app_key and secret_key that you can get after creating the app from http://apphq.shephertz.com.

Initializing AppWarp

-(void)initializeAppWarp
{
    [WarpClient initWarp:@"APPWARP_APP_KEY" secretKey:@"APPWARP_SECRET_KEY"];
    WarpClient *warpClient = [WarpClient getInstance];
    
    ConnectionListener *connectionListener = [[ConnectionListener alloc] initWithHelper:self];
    [warpClient addConnectionRequestListener:connectionListener];
    [connectionListener release];
    
    ZoneListener *zoneListener = [[ZoneListener alloc] initWithHelper:self];
    [warpClient addZoneRequestListener:zoneListener];
    [zoneListener release];
    
    RoomListener *roomListener = [[RoomListener alloc]initWithHelper:self];
    [warpClient addRoomRequestListener:roomListener];
    [roomListener release];
    
    NotificationListener *notificationListener = [[NotificationListener alloc]initWithHelper:self];
    [warpClient addNotificationListener:notificationListener];
    [notificationListener release];
}

WarpClient is the main interface class in the AppWarp SDK through which every request has to be sent to the AppWarp server.AppWarp has predefined callback listeners which provides the response of a request. So our initializeAppWarp method initializes AppWarp for the given app credentials and adds the callback listeners to the WarpClient.

Here we have added ConnectionListener to receive the connection related callbacks, ZoneListener to receive the zone related callbacks,RoomListener to receive the room related callbacks, and NotificationListener to receive the info about any activity happening in the room.To get familiar with the Zone, Room, Lobby etc., you can check here.

Our ViewController class has the interface for the requests that has to be made to the AppWarp server.So we called initializeAppWarp method in the viewDidLoad method of ViewController class.

Connecting to AppWarp

Once you are done with initialization, you can proceed to connect to the AppWarp by providing user name in the TextField on the first screen when you run the application and then click on the Connect button which calls connectToWarp method of AppWarpHelper class,

-(void)connectToWarp
{
    [[WarpClient getInstance] connectWithUserName:userName];
}

The onConnectDone method of ConnectionListener class will be called in response.

-(void)onConnectDone:(ConnectEvent*) event
{
    NSLog(@"%s...name=%@...%d",__FUNCTION__,[helper userName],event.result);
    if (event.result==SUCCESS)
    {
        NSLog(@"Connection Done");
    }
    else
    {
        NSLog(@"Connection failed");
    }
}

Creating a Room

To create a room click on the Create Room button which will ask you room name, room owner, maxUser, and properties parameter where last one is optional. After providing these parameters, click on the Create button which calls createRoom method of AppWarpHelper class,

-(void)createRoom
{
    [[WarpClient getInstance] createRoomWithRoomName:roomName roomOwner:roomOwner properties:properties     maxUsers:maxUser];
}

The onCreateRoomDone method of ZoneListener class will be called in response.

-(void)onCreateRoomDone:(RoomEvent*)roomEvent
{
    if (roomEvent.result == SUCCESS)
    {
        RoomData *roomData = roomEvent.roomData;
        NSLog(@"roomEvent result = %i",roomEvent.result);
        NSLog(@"room id = %@",roomData.roomId);
        [helper setRoomId:roomData.roomId];
    }
    else
    {
        
    }
}

Joining/Subscribing a Room

To be involved/aware with activities happening in a room, you have to join/subscribe the room. To join/subscribe a room all you need is a roomId of a room you want to join/subscribe. There are two ways to get roomIds,

  • Create a room then in response you will get roomId of newly created room
  • Use one of these, getAllRooms/getRoomWithNUser/getRoomWithProperties, APIs to get the IDs of existing rooms.

Here we can join/subscribe the room we created above after clicking on the Join/subscribe Room buttons which calls joinRoom/subscribeRoom method of AppWarpHelper class,

-(void)joinRoom
{
    [[WarpClient getInstance] joinRoom:roomId];
}

-(void)subscribeRoom
{
    [[WarpClient getInstance] subscribeRoom:roomId];
}

The onJoinRoomDone/onSubscribeRoomDone method of RoomListener class will be called in response.

-(void)onJoinRoomDone:(RoomEvent*)roomEvent
{
   NSLog(@".onJoinRoomDone..on Join room listener called");
    
    if (roomEvent.result == SUCCESS)
    {
        RoomData *roomData = roomEvent.roomData;
            
        NSLog(@".onJoinRoomDone..on Join room listener called Success");
    }
    else
    {
        NSLog(@".onJoinRoomDone..on Join room listener called failed");
    }
}

-(void)onSubscribeRoomDone:(RoomEvent*)roomEvent
{
    
    if (roomEvent.result == SUCCESS)
    {
        RoomData *roomData = roomEvent.roomData;
        NSLog(@"onSubscribeRoomDone  SUCCESS");
    }
    else
    {
        NSLog(@"onSubscribeRoomDone  Failed");
    }
}

As you joined a room just now, so you are now able to send the data to the remote members of the room as well as you will receive the data sent by the remote members of the room. To send the data, you have to convert you data into NSData format and then call sendUdpUpdatePeers API of WarpClient. Here we are sending a dummy dictionary created and converted to NSData when you click on Update Peers button which calls updatePeers method of AppWarpHelper class

-(void)updatePeers:(NSDictionary *)dataDict
{
    if(!dataDict)
		return;
    
	NSError *error = nil;
	//converting the content to plist supported binary format.
	NSData *convertedData = [NSPropertyListSerialization dataWithPropertyList:dataDict format:NSPropertyListXMLFormat_v1_0 options:0 error:&error];
	
	if(error || ! convertedData)
	{
		NSLog(@"DataConversion Failed.ErrorDescription: %@",[error description]);
		return;
	}
    [[WarpClient getInstance] sendUdpUpdatePeers:convertedData];
}

When you or the other members of a room sends data then all the room members receive the same data through the callback onUpdatePeersReceived of NotificationListener.

-(void)onUpdatePeersReceived:(UpdateEvent*)updateEvent
{
    
    NSData *data = updateEvent.update;
    NSError *error = nil;
	NSPropertyListFormat plistFormat;
    //converting the data into plist supported object.
	if(!data)
	{
		return;
	}
	
	id contentObject = [NSPropertyListSerialization propertyListWithData:data options:0 format:&plistFormat error:&error];
	
	if(error)
	{
		NSLog(@"DataConversion Failed. ErrorDescription: %@",[error description]);
		return;
	}
    NSLog(@"Data received--- %@",contentObject);
}

In the same fashion, you can join the lobby or you can send messages to the members of the room you have joined using the other methods of AppWarpHelper like,

-(void)subscribeLobby
{
    [[WarpClient getInstance] subscribeLobby];
}

-(void)joinLobby
{
    [[WarpClient getInstance] joinLobby];
}

-(void)sendChat:(NSString*)message
{
    [[WarpClient getInstance] sendChat:message];
}

In response, callbacks of the related listeners will be called.

Clone this wiki locally