Connecting with Evernote iOS application.

This post is a first one of the 3 part series. Just to set some direction, the earlier post we were looking at an application for users who  would want to take advantage of their cloud services like evernote/dropbox.  First there is already a great tutorial from evernote developers journal to connect with various Evernote API's. This post is just to take a crack at how to simply integrate Evernote and post the challenges I faced.

Here is the link to connect to Evernote API

First thing to connect with Evernote is sign-up as a developer in their account. Evernote provides two modes of operation, a developer mode and the production mode. Once you have battle tested your application, you can request evernote to move your application to Prod mode. During the developer mode, evernote will be allowing you to develop apps in their sandbox environment. All the data that you will be sending will be routed to a sandbox URL. In my case the sandbox URL is link

Here is what you will see according to Evernote's steps to sucess

Screen Shot 2012 06 04 at 10 04 40 PM

Once you get the API key from Evernote. You can start rolling. Before we start looking into my implementation, here is deeper dive link of Evernote's Github repository 

 Now that you will have to download the files from Github, look for the sub-heading that says iOS sample.

The two popular files that come with example are evernote.h and evernote.m files. The first thing we have to address in the implementation file is define our credentials during API key registration and specify the sandbox url

Here is the code sample for the changes

NSString * const consumerKey  = @"JohnDoe";

NSString * const consumerSecret = @"sshsecretkey";

 

NSString * const userStoreUri = @"https://sandbox.evernote.com/edam/user";

NSString * const noteStoreUriBase = @"https://sandbox.evernote.com/edam/note/"


As recommended by Evernote, make sure you will have only one instance of object code, i.e. here is a code for an shared instance

 

+ (Evernote *)sharedInstance {

 

    if (sharedEvernoteManager == nil) {        

        sharedEvernoteManager = [[Evernotealloc] init];

    }

    

    returnsharedEvernoteManager;

    

}

 

Now that we have the ground work done, next ideal step would be to make sure we can code to establish connection and here is  snippet for the connect information

-(BOOL)connect{

    if (authToken == nil

        {      

            // In the case we are not connected we don't have an authToken

            // Instantiate the Thrift objects

            UsynkDataUtils *dataUtls=[[UsynkDataUtils alloc]init];

            Deviceauth *devAuth=[dataUtls selectCloudCredentials:[dataUtls selectEntityDeviceId:[[UIDevice currentDevice] name]    withCloudName:EVERNOTE]];

            _username=[devAuth clouduser_id];

            _password=[devAuth password];

 

            NSURL * NSURLuserStoreUri = [[[NSURL alloc] initWithString: userStoreUri] autorelease];

            

            THTTPClient *userStoreHttpClient = [[[THTTPClient alloc] initWithURL:  NSURLuserStoreUri] autorelease];

            TBinaryProtocol *userStoreProtocol = [[[TBinaryProtocol alloc] initWithTransport:userStoreHttpClient] autorelease];

 

            EDAMUserStoreClient *userStore = [[[EDAMUserStoreClient alloc] initWithProtocol:userStoreProtocol] autorelease];

            

            ..,

            // Check that we can talk to the server

            ..,                

            }

            EDAMAuthenticationResult* authResult;

            @try{

            // Returned result from the Evernote servers after authentication

                authResult =[userStore authenticate:_username :_password : consumerKey :consumerSecret];

            }@catch (NSException *nse) {

                NSLog(@"Credentials for Evernote not correct");

                return FALSE;

            }@finally {

                NSLog(@"Finally clean-up for sign-up");

            }

            

            // User object describing the account

            self.user = [authResult user];

            // We are going to save the authentication token

            self.authToken = [authResult authenticationToken];

            // and the shard id

            self.shardId = [user shardId];

            

           ..,

            // Creating the User-Agent

            UIDevice *device = [UIDevice currentDevice];

            NSString * userAgent = [NSString stringWithFormat:@"%@/%@;%@(%@)/%@", applicationName,applicationVersion, [device systemName], [device         model], [device systemVersion]]; 

            

            ..,

            TBinaryProtocol *noteStoreProtocol = [[[TBinaryProtocol alloc] initWithTransport:noteStoreHttpClient] autorelease];

            noteStore = [[[EDAMNoteStoreClient alloc] initWithProtocol:noteStoreProtocol] retain];

            return TRUE;

        }

}

 

Whola the winds up our connection method will connect evernote, an integration will have to used according to specification. In this case what I ended up doing was connect with ID and password with the implementation view: Here is the sample

 

/********************************************

 Default Evernote upload method information.*

 ********************************************/

-(void)defualtEvernoteUpload:(id)fileName withText:(id)tempText{

    EDAMNote *edamNote=[[EDAMNote alloc]init];

    edamNote.title=fileName; 

    NSString * ENML = [[[NSStringalloc] initWithString: @"<?xml version="1.0" encoding="UTF-8"?>n<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">n<en-note>"]stringByAppendingString:tempText];

    ENML = [NSString stringWithFormat:@"%@%@", ENML, @"</en-note>"];

    [edamNote setContent:ENML];

    [_everNote createNote:edamNote];

}

 

Now we should have evernote talking with our application and uploading information to the sandbox

Finally here is the sample code to upload a note to Evernote

/************************************************************

 *

 *  Creating a note

 *

 ************************************************************/

 

- (BOOL) createNote: (EDAMNote *) note {

    // Checking the connection

    if ([self connect]==TRUE) {

        [noteStore createNote:authToken :note];

        return TRUE;

    }else {

        NSLog(@"No Evernote Sync Credentials validated by user");

        returnFALSE;

    }

}

 

As always if you have questions with this process, feel free to contact. Until the next post Adios.