iOS 㧠Evernote API ã使ã(åç·¨)
iPad ãã Evernote ã«ã¢ã¯ã»ã¹ããã¢ããªãä½ããã¨æãã試ãã¦ã¿ã¾ããã
1. Evernote APIãç解ãã
a. ããã¥ã¡ã³ãã«ç®ãéã
Evernote API Overview | Evernote Corporation ãã¼ã¸ã«ãã API Overview (PDF)ãçºããã¨ã Evernoteã®ãã¼ã¿æ§é ã APIã®æ¦ç¥ã解ãã¾ãã
b. APIã¨ãµã³ãã«ã³ã¼ãããã¦ã³ãã¼ããã¦çºãã
Evernote API Overview | Evernote Corporation ãã¼ã¸ãã Download the API SDK ãã API, ãµã³ãã«ã³ã¼ãã®å ¥ã£ã zipãã¡ã¤ã«ããã¦ã³ãã¼ãã§ãã¾ããç¾å¨ã®ãã¼ã¸ã§ã³ã¯1.17ã§ããä¸ã«ã¯ãããããªè¨èªã§æ¸ããããµã³ãã«ãããã¾ãã®ã§ãèªåã®å¥½ããªè¨èªã®ãµã³ãã«ãçºãã¦ã¿ã¾ããç§ã¯å½ç¶Rubyã§ããã
c. ãµã³ãã«ãåããçºã®API Keyãåå¾ãã
Evernote API㯠OAuthã使ã£ã¦ã¯ã©ã¤ã¢ã³ãã½ãããèªèããã®ã§ãOAuthç¨ã®consumer key, consumer secretãåå¾ããå¿
è¦ãããã¾ãã
Keyã®åå¾ã¯ Evernote API Overview | Evernote Corporation ãã¼ã¸ã®Request an API Keyãã©ã¼ã ãè¨è¿°ãSubmitãã¿ã³ãæ¼ãã¨ãconsumer key , consumer secret ãç»é¢ã«è¡¨ç¤ºããã¾ãã®ã§ã¡ã¢ãã¦ããã¾ãããã
d. Sandboxã«ãã¹ããã¼ã¿ãæºåãã
éçºç¨ã«ã¢ã¯ã»ã¹ã§ããã®ã¯é常ã®Evernoteã§ã¯ãªããSandbox ã§ãã ここ ã§ãã¹ãç¨ã®ã¢ã«ã¦ã³ããä½æãããã¹ãç¨ã®ã³ã³ãã³ããæ ¼ç´ãã¦ä¸ããã
e. ãµã³ãã«ã³ã¼ããåããã¦ã¿ã
b. ã§å±éãããµã³ãã«ã³ã¼ãã« c. ã§åå¾ãã consumer key , consumer secret ãæ¸ãè¾¼ã㨠Sandbox ã® Evernoteãã¢ã¯ã»ã¹ã§ãã¾ãã
å®è¡ä¾ï¼
% cd evernote-api-1.17/sample/ruby/client % vi EDAMTest.rb â consumer key , consumer secret ãæ¸ãè¾¼ã % ruby EDAMTest.rb Sandboxã®ã¢ã«ã¦ã³ã Sandboxã®ãã¹ã¯ã¼ã Unable to load thrift_native extension. Defaulting to pure Ruby libraries. warning: peer certificate won't be verified in this SSL session Is my EDAM protocol version up to date? true warning: peer certificate won't be verified in this SSL session Authentication was successful for yuumi3 Authentication token = S=s1:U=5381:E=130f97c8d8a:C=130f9459f0e:P=37:A=yuumi3:H=0bdbe82e2e87bf54ac6121ff0e3246e1 warning: peer certificate won't be verified in this SSL session Found 1 notebooks: * yuumi3 ã®ãã¼ããã㯠Creating a new note in the default notebook: yuumi3 ã®ãã¼ããã㯠warning: peer certificate won't be verified in this SSL session Successfully created a new note with GUID: 64983248-7d3f-438e-bbb0-7eeb0409c4f4
2. iOSç¨ã©ã¤ãã©ãªã¼ã®ä½æ
ãããããiOSç¨ã©ã¤ãã©ãªã¼ãä½ãã¾ããããæé 㯠Evernote APIを使うiPhoneアプリの作りかた - the time ship ãåèã«ãã¾ããããããã¨ãããã¾ãï¼
- Xcode4 ã§æ°è¦ããã¸ã§ã¯ã(Navigation-based Application)ãä½æãã¾ã(Xcodeï¼ã¨ã¯ããã¸ã§ã¯ãã®ãã£ã¬ã¯ããªã¼æ§é ãéãã¾ãã)ã
- Evernote APIãå ¥ãããã©ã«ãã¼/ã°ã«ã¼ããä½æã evernote-api-1.17/src/cocoa/thrift, evernote-api-1.17/src/cocoa/edam ã以ä¸ã®æ§ã«ã³ãã¼ãã¾ã
- Build Settingsã® Header Search Path ã« ./EvernoteTest/Library/thrift (Recusiveããã§ãã¯) ã追å
- ãã«ããã¨ã©ã¼ãç¡ããã°æå
3. iOSç¨ãã¹ãã³ã¼ãã®ä½æ
Evenoteã®ãµã³ãã«ã³ã¼ãã«ã¯iOSã Mac OS Xç¨ããã¾ããããã㧠Evernote APIã使ãiPhoneã¢ããªã®ä½ããã - the time ship] ã®ã³ã¼ãããã¼ã¹ã« Evernoteã«æ ¼ç´ããã¦ãããã¼ãåã®ä¸è¦§ããã¼ãã«ã§è¡¨ç¤ºãããã¹ãã³ã¼ããä½ã£ã¦ã¿ã¾ããã
Evernoteã¢ã¯ã»ã¹ã®ä¸»è¦é¨åã¯ä»¥ä¸ã®ãããªãã¾ãã APIã®è©³ç´°ã¯ Thrift API Reference ãåç §ãã¦ä¸ããã
- user(ã¢ã«ã¦ã³ã)æ å ±ã®åå¾
- èªè¨¼ç¨ãã¼ã¯ã³ã®åå¾
- Noteæ
å ±ã®åå¾
- Noteã®ä¸è¦§ã¯ findNotes ã§è¡ãã¾ãããã®ã¡ã½ããã¯æ¸¡ãããæ¤ç´¢æ¡ä»¶(EDAMNoteFilterãªãã¸ã§ã¯ã)ã«ããããããã¼ãã®ä¸è¦§ãåå¾ãã¾ã
- ä»åã¯å ¨ä»¶ã®åå¾ã§ããããããããªæ¡ä»¶ãè¨å®ã§ããããã§ãã詳細㯠API Overview ã Thrift API Reference ãåç §ãã¦ä¸ããã
- RootViewController.m ã®ä¸»è¦é¨å
#import "RootViewController.h" #import "THTTPClient.h" #import "TBinaryProtocol.h" #import "UserStore.h" #import "NoteStore.h" #define USERNAM @"XXXXXX" // Sandboxã®ã¢ã«ã¦ã³ã #define PASSWORD @"XXXXXX" // Sandboxã®ãã¹ã¯ã¼ã #define CONSUMER_KEY @"XXXXX" // åå¾ããconsumer key #define CONSUMER_SECRET @"XXXXXX" // åå¾ããconsumer secret @implementation RootViewController @synthesize noteTitles; - (void) getEvernoteTitles { NSURL *userStoreURL = [NSURL URLWithString:@"https://sandbox.evernote.com/edam/user"]; NSString *noteStoreURLBase = @"http://sandbox.evernote.com/edam/note/"; // useræ å ±ãæ±ããªãã¸ã§ã¯ãã®ä½æ THTTPClient *userStoreHTTPClient = [[[THTTPClient alloc] initWithURL:userStoreURL] autorelease]; TBinaryProtocol *userStoreProtocol = [[[TBinaryProtocol alloc] initWithTransport:userStoreHTTPClient] autorelease]; EDAMUserStoreClient *userStore = [[[EDAMUserStoreClient alloc] initWithProtocol:userStoreProtocol] autorelease]; // éä¿¡ãããã³ã«ã®ãã¼ã¸ã§ã³ã®ãã§ã㯠BOOL versionOK = [userStore checkVersion:@"EDMATest" :[EDAMUserStoreConstants EDAM_VERSION_MAJOR] :[EDAMUserStoreConstants EDAM_VERSION_MINOR]]; if(!versionOK) { NSLog(@"checkVersion error"); return; } @try { // èªè¨¼ç¨ãã¼ã¯ã³ã®ä½æ EDAMAuthenticationResult *authResult = [userStore authenticate:USERNAM :PASSWORD :CONSUMER_KEY :CONSUMER_SECRET]; EDAMUser *user = [authResult user]; NSString *authToken = [authResult authenticationToken]; // Noteæ å ±ãæ±ããªãã¸ã§ã¯ãã®ä½æ NSURL *noteStoreURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", noteStoreURLBase, [user shardId]]]; THTTPClient *noteStoreHTTPClient = [[[THTTPClient alloc] initWithURL:noteStoreURL] autorelease]; TBinaryProtocol *noteStoreProtocol = [[[TBinaryProtocol alloc] initWithTransport:noteStoreHTTPClient] autorelease]; EDAMNoteStoreClient *noteStore = [[[EDAMNoteStoreClient alloc] initWithProtocol:noteStoreProtocol] autorelease]; // Notebookã®ä¸è¦§åå¾ NSArray *notebooks = [noteStore listNotebooks:authToken]; for(int i = 0; i < [notebooks count]; i++) { EDAMNotebook *notebook = (EDAMNotebook*)[notebooks objectAtIndex:i]; NSLog(@"%@", [notebook name]); } // Noteæ¤ç´¢ç¨ãã£ã«ã¿ã¼ã®ä½æ EDAMNoteFilter *allFileter = [[[EDAMNoteFilter alloc] initWithOrder:0 ascending:YES words:nil notebookGuid:nil tagGuids:nil timeZone:nil inactive:NO] autorelease]; // Noteã®ä¸è¦§åå¾ EDAMNoteList *list = [noteStore findNotes:authToken :allFileter :0 :1000]; NSArray *notes = [list notes]; NSLog(@"notes count %d", [notes count]); [noteTitles removeAllObjects]; for (EDAMNote *note in notes) { // Noteåã®åå¾ NSLog(@"-- %@", [note title]); [noteTitles addObject:[note title]]; } [self.tableView reloadData]; } @catch (NSException * e) { NSLog(@"Caught %@: %@", [e name], [e reason]); } @finally { } [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; } ã»ã»ã»ã»ã»ã»
- RootViewController.h
#import <UIKit/UIKit.h> @interface RootViewController : UITableViewController { NSMutableArray *noteTitles; } @property(nonatomic, retain) NSMutableArray *noteTitles; @end
- RootViewController.m ãã®ä»
ã»ã»ã»ã»ã»ã» - (void)viewDidLoad { [super viewDidLoad]; self.noteTitles = [NSMutableArray array]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; [self performSelector:@selector(getEvernoteTitles) withObject:nil afterDelay:0.2]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [noteTitles count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } cell.textLabel.text = [noteTitles objectAtIndex:[indexPath row]]; return cell; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; self.noteTitles = nil; } - (void)viewDidUnload { [super viewDidUnload]; self.noteTitles = nil; } - (void)dealloc { [noteTitles release]; [super dealloc]; } @end