1

I was asked to build an iOS app based on Fedena (School management system). Their API sample codes are in HTML-JS or RUBY. What I have to do is to pass the arguments (token, URI, etc..) programatically, and upon sending them, the request will return an XML file which I can then parse and view in a tableView.

  1. How can I pass parameters by code, using AFNetworking?
  2. How can I fetch the XML file?

This is an example of the API code provided:

<html>
  <head>
    xhr.open('GET', fedena_server+"/api/users/"+username);
    xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    xhr.setRequestHeader('Authorization', 'Token token="'+token+'"');
    xhr.send();
    ......
Hussein
  • 407
  • 5
  • 16
  • 1
    You shouldn't consider sending them in HTML/JS. If you don't have experience using a REST API, check out https://github.com/AFNetworking/AFNetworking The API call you need is clear from the code you shared, you have to send a Get request and add an HTTPHeaderField for the authentication. Let me know if you need further help – Jad Feitrouni Sep 13 '15 at 16:49
  • @JadFeitrouni Could you please give an example, regarding my code? Would be very thankful – Hussein Sep 13 '15 at 16:57

1 Answers1

2

First you will have to add AFNetworking to your project. The easiest way to do so is using pods info can be found here : https://guides.cocoapods.org/using/getting-started.html

the following code will fetch the response for the mentioned API call:

NSString *url = [NSString stringWithFormat:@"%@/api/users/%@",fedenaServer,username];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSString *token = [NSString stringWithFormat:@"Token token=%@",token];
[manager.requestSerializer token forHTTPHeaderField:@"Authorisation"];
[manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    // your code to parse the response
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

You'll have to do a bit of research for the whole thing to work, like parsing the XML response Giving you more than that would be doing the job for you :P Hope it helps.

Jad Feitrouni
  • 637
  • 6
  • 12