Objective-C Example (outdated)

(Shima Amin Sharifi & Navid Gooranourimi)

ContextData_1.0_iOS.zip

To integrate these classes into a Xcode project, just drag and drop the files in the project directory and select “Copy items if needed” from the window that will appear after the drag&drop.

Add the following code to the header of each class in which you need to use these classes:

 

 

#import "ServerConnection.h"
#import "NSString+NSString_Extended.h"

After this, the functionalities of these classes are available to your classes. The "ServerConnection.h" class will take care of user registration, http GET and http POST with the following methods:

// Simple method to register a user on the learning context server
- (void) setRegisterUserRequestWithEmail:(NSString*)email username:(NSString*)username password:(NSString*)password;
// HTTP GET method to send GET requests on the learning context server
- (void) getWithInterface:(NSString*)interface WithData:(NSString*)data WithUsername:(NSString*)username WithPassword:(NSString*)password;
// HTTP POST method to send POST requests on the learning context server
- (void) postWithInterface:(NSString*)interface WithData:(NSString*)data WithUsername:(NSString*)username WithPassword:(NSString*)password;

The "NSString+NSString_Extended.h" category over NSString will take care of url encoding which needs to be done to send data to the learning context server with the following method:

- (NSString *)urlencode;

To encode any url string, you could simply call the “urlencode” method over any NSString object.

Note: you don’t need to urlencode any “dataString” you pass through the GET and POST method of this library because it has been taken care of on the library itself.

There are 2 constants which need to be set according to your project settings before you can use the library in the "ServerConnection.m” file:

//TODO: Change these constants to your specific ones
#define kAppId [NSString stringWithFormat:@"CHANGE_THIS_TO_YOUR_APP_ID"]
#define kAppSecret [NSString stringWithFormat:@"CHANGE_THIS_TO_YOUR_APP_SECRET"]

After setting these constants you can use the three methods with their delegates that the ServerConnection class provides.

To register a user on the learning context server do as following:

ServerConnection *serverConnection = [[ServerConnection alloc] init];
[serverConnection setRegisterUserRequestWithEmail:@"email" username:@"username" password:@"password"];

Note: you should replace each parameter with your specific settings.

When the registration process has got result, its delegate method would be called if your class conforms to the “ServerConnectionDelegate” protocol on its header file and the JSON result would be on the “registerUserResultData” which has been passed through delegate method. The delegate method would be:

// Delegate method for when the user register request got the result
- (void)registerUserRequestGotResult:(NSData*)registerUserResultData;

To send a GET request to the learning context server do as following:

ServerConnection *serverConnection = [[ServerConnection alloc] init];
[serverConnection getWithInterface:@"LearningContextApiGetUrl" WithData:@"dataString" WithUsername:@"username" WithPassword:@"password"];

Note: you should create the “dataString” according to API documentation and also replace each parameter with your specific settings.

When the GET request has got result, its delegate method would be called if your class conforms to the “ServerConnectionDelegate” protocol on its header file and the JSON result would be on the “getRequestJsonResultData” which has been passed through delegate method. The delegate method would be:

// Delegate method for when the HTTP GET request got the result
- (void)getRequestGotResult:(NSData*)getRequestJsonResultData;

To send a POST request to the learning context server do as following:

ServerConnection *serverConnection = [[ServerConnection alloc] init];
[serverConnection postWithInterface:@"LearningContextApiPostUrl” WithData:@"dataString" WithUsername:@"username" WithPassword:@"password"];

Note: you should create the “dataString” according to API documentation and also replace each parameter with your specific settings.

When the POST request has got result, its delegate method would be called if your class conforms to the “ServerConnectionDelegate” protocol on its header file and the JSON result would be on the “postRequestJsonResultData” which has been passed through delegate method. The delegate method would be:

// Delegate method for when the HTTP POST request got the result
- (void)postRequestGotResult:(NSData*)postRequestJsonResultData;