-
Notifications
You must be signed in to change notification settings - Fork 3
Getting Started
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.
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.
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.
[[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
}
}];
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);
}
}];
In this example, the individual steps are:
- Getting the
sharedClient
(in this example it is assumed that theaccessToken
is already set on the client object). - Preparing an
ADNPost
instance to post. - 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];
}
This example shows how to use the completion block to interact with the rest of the application. The strategy here is:
- Retrieve the
sharedClient
and set itsaccessToken
. - Call the
getUser
method with the special usernameme
. - 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];
}];
}