Objective-C - how-to

Below, you will find some Objective-C example code, to call the API in Objective-C. In this sample, we use librairies in order to do some Base64 Encoding and RSA Encryption.

Source code


    
    /* COMMON VARIABLES */
    NSString *userLogin = @"username";
    NSString *userCryptedPassword = [self encodePassword:@"password"]; // RSA Encryption with encodePassword
    NSString *serverUrlString = @"http://demo.adelya.com";
    NSString *apiKey = @"YOUR-APIKEY";
    NSString *cardnumber= @"10001234";
    
    /* Building a JSON Request using full NSString */
    NSString *jsonString = [NSString stringWithFormat:@"json_data={\"FidelityMember\":{\"cardnumber\":\"%@\"}}", cardnumber ];
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    
    // Build encoded Basic Auth with Basic login:encoded_password
    NSString *basicAuthLogin = [NSString stringWithFormat:@"%@;%@:%@", apiKey, userLogin, userCryptedPassword];
    
    // Transform NSString "login:password" into NSData, then encode it in a Base64 string
    NSData *encodedLogin = [basicAuthLogin dataUsingEncoding:NSUTF8StringEncoding];
    NSString *basicAuth = [NSString stringWithFormat:@"Basic %@", [encodedLogin base64Encoding]];
    
    // Build URLRequest
    NSString *fullUrlString = [NSString stringWithFormat:@"%@/apiv1/webapi.do", serverUrlString];
    NSURL *url = [NSURL URLWithString:fullUrlString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    
    // Set Post method, add basic authentication as a parameter and json request in http body
    [request setHTTPMethod:@"POST"];
    [request addValue:basicAuth forHTTPHeaderField:@"Authorization"];
    [request setHTTPBody:jsonData];
    
    // Do JSON Call
    NSURLResponse *response = nil;
    NSError *error;
    
    // Send synchronous request in order to get the response from our server
    NSData *response_data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    
    // Get the response data
    NSString *responseString = [[NSString alloc] initWithData:response_data encoding:NSUTF8StringEncoding];