Upload
jiro-nagashima
View
5.113
Download
7
Embed Size (px)
DESCRIPTION
iPhoneアプリ開発でWebサービスのクライアント実装をサポートしてくれるフレームワーク「RestKit」の紹介。
Citation preview
永島 次朗 / Jiro Nagashima
iOS Developer @ nana music, inc.
@hedjirog
RestKit / Examples / RKTwitter
サンプルプロジェクト•動作手順• リポジトリを複製 (masterブランチ)• Examplesディレクトリ下のプロジェクトを開く• Build & Run
$ git clone -b master --recursive git://github.com/RestKit/RestKit.git
$ open RestKit/Examples/RKTwitter/RKTwitter.xcodeproj
※ 同時に複数のサンプルプロジェクトを開くと、(サブプロジェクトとして含まれるRestKitの参照に失敗して)
ビルドエラーになる場合があるので注意
Submoduleを含むリポジトリ複製
recursiveオプションを付与(clone後のsubmodule更新)
$ git clone -b master --recursive git://github.com/RestKit/RestKit.git
$ open RestKit/Examples/RKTwitter/RKTwitter.xcodeproj
サンプルプロジェクト(補足)
Agenda• RestKitとは• 導入手順 • 注意点
•基本的な使い方• Networking, Object Mapping, Core Data
RestKitとは
RestKitとは
RestKitObject Mapping
Networking
Core Data
RestKitとは
RestKitObject Mapping
Networking
Core Data
Webサービスのクライアント実装を強力にサポート!!
RestKitとは
RestKit
• RestKit• http://restkit.org/
• Github project page• https://github.com/RestKit/
RestKit
導入手順• CocoaPodsを利用(推奨)• https://github.com/RestKit/RestKit#via-
cocoapods• http://cocoapods.org/?q=RestKit
注意点• iOS 5.0 以上が必須• ARC• 最新バージョンに“pre”の表記 (2013.01.26現在)• https://github.com/RestKit/RestKit/tags
• Apache License 2.0
Agenda✓RestKitとは✓導入手順 ✓注意点
•基本的な使い方• Networking, Object Mapping, Core Data
基本的な使い方※ サンプルプロジェクト(RKTwitter, RKTwitterCoreData)から抜粋
Networking (1/6)• RKObjectManager• 通信処理の共通設定
NSURL *baseURL = [NSURL URLWithString:@"http://twitter.com"];
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:baseURL];
Networking (2/6)• RKObjectManager• 共通設定からリクエストを作成
RKObjectManager *objectManager = [RKObjectManager sharedManager]; [objectManager getObjectsAtPath:@"/status/user_timeline/RestKit" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { NSArray* statuses = [mappingResult array]; NSLog(@"Loaded statuses: %@", statuses); } failure:^(RKObjectRequestOperation *operation, NSError *error) { NSLog(@"Hit error: %@", error); }];
Object Mapping (3/6)• RKObjectMapping• マッピング対象のクラスを指定• レスポンスで受け取るJSONデータのキーと、
クラスのプロパティをマッピングRKObjectMapping *statusMapping;statusMapping = [RKObjectMapping mappingForClass:[RKTweet class]];
[statusMapping addAttributeMappingsFromDictionary:@{ @"id" : @"statusID", @"created_at" : @"createdAt", @"text" : @"text", @"url" : @"urlString", @"in_reply_to_screen_name" : @"inReplyToScreenName", @"favorited" : @"isFavorited", }];
※ CoreData利用時のマッピングは後述(RKEntityMapping)
Object Mapping (4/6)• RKObjectMapping• マッピングを適用するレスポンスの状態を
RKResponseDescriptorで設定• RKObjectManagerへ設定内容を引き渡し
RKResponseDescriptor *responseDescriptor;responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:statusMapping pathPattern:@"/status/user_timeline/:username" keyPath:nil statusCodes:[NSIndexSet indexSetWithIndex:200]];
RKObjectManager *objectManager = [RKObjectManager sharedManager];[objectManager addResponseDescriptor:responseDescriptor];
Core Data (5/6)• RKManagedObjectStore• Core Dataスタックの管理
NSManagedObjectModel *managedObjectModel;managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
RKManagedObjectStore *managedObjectStore;managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
RKObjectManager *objectManager = [RKObjectManager sharedManager];objectManager.managedObjectStore = managedObjectStore;
Core Data (6/6)• RKEntityMapping• マッピング対象のエンティティと、
RKManagedObjectSotreインスタンスを指定• 値が一意となる属性を指定(オブジェクト更新時
に利用される)RKEntityMapping *tweetMapping;tweetMapping = [RKEntityMapping mappingForEntityForName:@"Tweet" inManagedObjectStore:managedObjectStore];
tweetMapping.identificationAttributes = @[ @"statusID" ];[tweetMapping addAttributeMappingsFromDictionary:@{ @"id": @"statusID", @"created_at": @"createdAt", @"text": @"text", @"url": @"urlString", @"in_reply_to_screen_name": @"inReplyToScreenName", @"favorited": @"isFavorited", }];
Agenda✓RestKitとは✓導入手順 ✓注意点
✓基本的な使い方✓Networking, Object Mapping, Core Data
関連• API Reference• https://github.com/RestKit/RestKit#api-
quickstart• http://restkit.org/api/latest/
• Wiki• https://github.com/RestKit/RestKit/wiki