Copyright (C) iPhoneアプリ開発備忘録 All rights reserved.
ブログ内で記したコード、内容の正確性は保証いたしません。
記載内容を実装したことにより発生した不具合・損害等の責任は一切負いません。



2011年07月09日

Evernote連携 添付ファイル書込み編



クラウドサービスEvernoteに添付ファイルを書込む方法。

Evernote APIキー取得と、ライブラリの組み込みは下記を参照。

http://iphone-app-developer.seesaa.net/article/211515958.html

Notebook作成は、上記記事の5.と基本的な部分は同一。
添付ファイルは< en-media >要素で記述する。

RootViewController.h
#import "RootViewController.h"
#import "THTTPClient.h"
#import "TBinaryProtocol.h"
#import "UserStore.h"
#import "NoteStore.h"

#define USERNAM @"YOUR_USER_NAME" // Sandboxのアカウント
#define PASSWORD @"YOUR_PASSWORD" // Sandboxのパスワード
#define CONSUMER_KEY @"YOUR_CONSUMER_KEY" // 取得したconsumer key
#define CONSUMER_SECRET @"YOUR_CONSUMER_SECRET" // 取得したconsumer secret

@implementation RootViewController

- (NSString*)dataToHex:(NSData*)data {
NSMutableString *hexData = [[NSMutableString alloc]init];
for (long i=0; i<[data length];i++) {
UInt8 intVal = 0xff & *((UInt8*)[data bytes]+i);
[hexData appendString:[NSString stringWithFormat:@"%02x",intVal]];

}
return hexData;
}


- (void) putEvernote {

URL = [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];

// 添付データを作成
// EDAMData *eDamData = 添付データ(ここではpngファイルを設定するコードを自由に記述);

EDAMResourceAttributes *atts1 = [[EDAMResourceAttributes alloc]init]; // リソースの属性
[atts1 setFileName:@"test.png"];

EDAMResource *resource =[[EDAMResource alloc]init];
[resource autorelease];
[resource setData:eDamData];
[resource setMime:@"image/png"];
[resource setAttributes:atts1];

NSString *hashHex = [self dataToHex:[[resource data] bodyHash]]; // hash処理


// Notebookの作成

EDAMNotebook* defaultNotebook = NULL;

EDAMNote *note = [[[EDAMNote alloc] init] autorelease];
[note setNotebookGuid:[defaultNotebook guid]];
[note setTitle:@"Test note from iPhone Develop Device."];
NSMutableString* contentString = [[[NSMutableString alloc] init] autorelease];
[contentString setString: @"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"];
[contentString appendString:@"<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml.dtd\">"];
[contentString appendString:@" <en-note>Test note.
"];
[contentString appendString:[NSString stringWithFormat:@"<en-media type=\"%@\" hash=\"%@\" />", @"image/png",hashHex]];
[contentString appendString:@" </en-note>"];

[note setContent:contentString];
[note setCreated:(long long)[[NSDate date] timeIntervalSince1970] * 1000];
EDAMNote *createdNote = [noteStore createNote:authToken :note];
if (createdNote != NULL)
{
NSLog(@"Created note: %@", [createdNote title]);
}

}
@catch (NSException * e) {
NSLog(@"Caught %@: %@", [e name], [e reason]);
}
@finally {
}

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

- (void)viewDidLoad
{
[super viewDidLoad];
}

- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[self performSelector:@selector(putEvernote) withObject:nil afterDelay:0.2];
}
上記はEvernote APIアクセスに関係する必要最小限のコードのみ記載しているのであしからず。


なお、EDAMResourceとして設定できる添付ファイルは、Evernote API Overview によると、下記の通りとなっている。
The service currently accepts Resources of the following types (defined in Limits.thrift) for all accounts, both Premium and free:

・ image/gif
・ image/jpeg
・ image/png
・ audio/wav
・ audio/mpeg
・ audio/amr
・ application/vnd.evernote.ink
・ application/pdf

text/plain や、text/tab-separated-values 、text/comma-separated-values といったMIME Typeのデータは、Evernoteのフリーアカウントユーザを考慮して<en-note > 属性に直接記述する。



■参考記事:
http://www.evernote.com/about/developer/api/evernote-api.htm
http://digitalpericope.net/?p=27
http://d.hatena.ne.jp/yuum3/20110706/1309926567


■参考書籍:
iPhoneSDK開発のレシピ
iPhoneSDK開発のレシピ
posted with amazlet at 11.06.23
高山 恭介 広部 一弥 松浦 晃洋
秀和システム
売り上げランキング: 25253


タグ:iPhone
posted by mobileDeveloper at 22:59 | Comment(0) | TrackBack(0) | 外部連携 はてなブックマーク - Evernote連携 添付ファイル書込み編 | このブログの読者になる | 更新情報をチェックする
この記事へのコメント
コメントを書く
お名前:

メールアドレス:

ホームページアドレス:

コメント:

認証コード: [必須入力]


※画像の中の文字を半角で入力してください。
※ブログオーナーが承認したコメントのみ表示されます。

この記事へのトラックバック
Apple、Appleのロゴ、App Store、iPodのロゴ、iTunesは、米国および他国のApple Inc.の登録商標です。
iPhone、iPod touch、iPadはApple Inc.の商標です。
iPhone商標は、アイホン株式会社のライセンスに基づき使用されています。
その他、本ブログに記載されている製品名、会社名は、それぞれ各社の商標または登録商標です。