Skip to content
This repository has been archived by the owner on Dec 26, 2017. It is now read-only.

Getting Started

mattrubin edited this page Mar 9, 2013 · 2 revisions

Authentication

An ADNAuthenticationRequest can be used to construct a URL for the web-based authentication flow. A working example of authentication and extracting an access_token from the callback URL can be seen in NDAAuthenticationViewController.

ADNAuthenticationRequest *authRequest = [ADNAuthenticationRequest new];
authRequest.clientId = <#yourClientId#>;
authRequest.responseType = ADNAuthenticationResponseTypeToken;
authRequest.redirectURI = @"yourapp://callback";
authRequest.scopes = ADNScopeBasic | ADNScopeFiles;
authRequest.appStoreCompliant = YES;

NSURL *authURL = authRequest.URL;
// load the authURL in a UIWebView and figure out when auth is finished based on what URL the web view tries to load next.

Setting up ADNClient

The ADNClient class is responsible for communicating with App.net. It has a method corresponding to each API endpoint. The simplest way to make use of the client is through the shared instance [ADNClient sharedClient].

Once an access token is acquired, it should be passed to the client:

[[ADNClient sharedClient] setAccessToken:accessToken];

To support multiple accounts, you can create multiple instances of ADNClient, each with their own access token.

Making API Requests

Each method on ADNClient takes whatever parameters are required by that API endpoint, as well as an optional completion block. The completion block is passed three objects: the object or collection of objects which were returned from the server, an ADNMetadata object which contains the meta information from the response envelope, and possibly an NSError if something went wrong. The completion block is run on the main thread by default.

Fetching a user:

[[ADNClient sharedClient] getUser:@"@mattrubin"
            withCompletionHandler:^(ADNUser *user, ADNMetadata *meta, NSError *error)
{
    if (user) {
        NSLog(@"This user's name is %@", user.name);
    } else {
        // Handle the error...
        NSLog(@"meta: %@", meta);
        // Useful info might be in the meta object returned from ADN
    }
}];

Fetching new files:

NSDictionary *parameters = self.maxId ? @{@"since_id":self.maxId} : nil;

[[ADNClient sharedClient] getMyFilesWithParameters:parameters
                                 completionHandler:^(NSArray *files, ADNMetadata *meta, NSError *error)
 {
     if (!error) {
         NSLog(@"Fetched %i new files...", files.count);
         
         if (files.count) { // If there are new files
             self.maxId = meta.maxId;
             
             [self.files insertObjects:files
                             atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, files.count)]];
             // Update the UI to show the new files...
         }
     } else {
         // Handle the error...
         NSLog(@"error: %@", error);
         NSLog(@"meta: %@", meta);
     }
 }];

Creating a Public Post:

In this example, the individual steps are:

  1. Getting the sharedClient (in this example it is assumed that the accessToken is already set on the client object).
  2. Preparing an ADNPost instance to post.
  3. Posting this instance using the sharedClient.
- (void)postPostWithText:(NSString *)text
{
    ADNClient *myClient = [ADNClient sharedClient];
    ADNPost *myPost = [[ADNPost alloc] init];
    myPost.text = text;
    [myClient postPost:myPost completionHandler:NULL];
}

Retrieving Information About the Currently Authorized User

This example shows how to use the completion block to interact with the rest of the application. The strategy here is:

  1. Retrieve the sharedClient and set its accessToken.
  2. Call the getUser method with the special username me.
  3. Use the completion handler to place a new operation on the main queue to update the UI.
- (void)retrieveUserInformation
{
    ADNClient *myClient = [ADNClient sharedClient];
    myClient.accessToken = userAccessToken;
    [myClient getUser:@"me" withCompletionHandler:^(ADNUser *user, ADNMetadata *meta, NSError *error){
        [self.usernameLabel setText:user.username];
        [self.userIdLabel setText:user.userId];
    }];
}