Commit c89b9dfd authored by Eloy Duran's avatar Eloy Duran

Import RestKit example RKTwitter.

parent d2bf1d8c
//
// RKTStatus.h
// RKTwitter
//
// Created by Blake Watters on 9/5/10.
// Copyright 2010 Two Toasters. All rights reserved.
//
#import "RKTUser.h"
@interface RKTStatus : NSObject {
NSNumber* _statusID;
NSDate* _createdAt;
NSString* _text;
NSString* _urlString;
NSString* _inReplyToScreenName;
NSNumber* _isFavorited;
RKTUser* _user;
}
/**
* The unique ID of this Status
*/
@property (nonatomic, retain) NSNumber* statusID;
/**
* Timestamp the Status was sent
*/
@property (nonatomic, retain) NSDate* createdAt;
/**
* Text of the Status
*/
@property (nonatomic, retain) NSString* text;
/**
* String version of the URL associated with the Status
*/
@property (nonatomic, retain) NSString* urlString;
/**
* The screen name of the User this Status was in response to
*/
@property (nonatomic, retain) NSString* inReplyToScreenName;
/**
* Is this status a favorite?
*/
@property (nonatomic, retain) NSNumber* isFavorited;
/**
* The User who posted this status
*/
@property (nonatomic, retain) RKTUser* user;
@end
//
// RKTStatus.m
// RKTwitter
//
// Created by Blake Watters on 9/5/10.
// Copyright 2010 Two Toasters. All rights reserved.
//
#import "RKTStatus.h"
@implementation RKTStatus
@synthesize statusID = _statusID;
@synthesize createdAt = _createdAt;
@synthesize text = _text;
@synthesize urlString = _urlString;
@synthesize inReplyToScreenName = _inReplyToScreenName;
@synthesize isFavorited = _isFavorited;
@synthesize user = _user;
- (NSString*)description {
return [NSString stringWithFormat:@"%@ (ID: %@)", self.text, self.statusID];
}
- (void)dealloc {
[_statusID release];
[_createdAt release];
[_text release];
[_urlString release];
[_inReplyToScreenName release];
[_user release];
[super dealloc];
}
@end
//
// RKTUser.h
// RKTwitter
//
// Created by Blake Watters on 9/5/10.
// Copyright 2010 Two Toasters. All rights reserved.
//
@interface RKTUser : NSObject {
NSNumber* _userID;
NSString* _name;
NSString* _screenName;
}
@property (nonatomic, retain) NSNumber* userID;
@property (nonatomic, retain) NSString* name;
@property (nonatomic, retain) NSString* screenName;
@end
//
// RKTUser.m
// RKTwitter
//
// Created by Blake Watters on 9/5/10.
// Copyright 2010 Two Toasters. All rights reserved.
//
#import "RKTUser.h"
@implementation RKTUser
@synthesize userID = _userID;
@synthesize name = _name;
@synthesize screenName = _screenName;
- (void)dealloc {
[_userID release];
[_name release];
[_screenName release];
[super dealloc];
}
@end
//
// RKTwitterAppDelegate.h
// RKTwitter
//
// Created by Blake Watters on 9/5/10.
// Copyright Two Toasters 2010. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface RKTwitterAppDelegate : NSObject <UIApplicationDelegate> {
}
@end
//
// RKTwitterAppDelegate.m
// RKTwitter
//
// Created by Blake Watters on 9/5/10.
// Copyright Two Toasters 2010. All rights reserved.
//
#import <RestKit/RestKit.h>
#import "RKTwitterAppDelegate.h"
#import "RKTwitterViewController.h"
#import "RKTStatus.h"
#import "RKTUser.h"
@implementation RKTwitterAppDelegate
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
RKLogConfigureByName("RestKit/Network*", RKLogLevelTrace);
// Initialize RestKit
RKObjectManager* objectManager = [RKObjectManager objectManagerWithBaseURL:@"http://twitter.com"];
// Enable automatic network activity indicator management
//objectManager.client.requestQueue.showsNetworkActivityIndicatorWhenBusy = YES;
// Setup our object mappings
RKObjectMapping* userMapping = [RKObjectMapping mappingForClass:[RKTUser class]];
[userMapping mapKeyPath:@"id" toAttribute:@"userID"];
[userMapping mapKeyPath:@"screen_name" toAttribute:@"screenName"];
[userMapping mapAttributes:@"name", nil];
RKObjectMapping* statusMapping = [RKObjectMapping mappingForClass:[RKTStatus class]];
[statusMapping mapKeyPathsToAttributes:@"id", @"statusID",
@"created_at", @"createdAt",
@"text", @"text",
@"url", @"urlString",
@"in_reply_to_screen_name", @"inReplyToScreenName",
@"favorited", @"isFavorited",
nil];
[statusMapping mapRelationship:@"user" withMapping:userMapping];
// Update date format so that we can parse Twitter dates properly
// Wed Sep 29 15:31:08 +0000 2010
[statusMapping.dateFormatStrings addObject:@"E MMM d HH:mm:ss Z y"];
// Register our mappings with the provider
[objectManager.mappingProvider setMapping:userMapping forKeyPath:@"user"];
[objectManager.mappingProvider setMapping:statusMapping forKeyPath:@"status"];
// Uncomment this to use XML, comment it to use JSON
// objectManager.acceptMIMEType = RKMIMETypeXML;
// [objectManager.mappingProvider setMapping:statusMapping forKeyPath:@"statuses.status"];
// Create Window and View Controllers
RKTwitterViewController* viewController = [[[RKTwitterViewController alloc] initWithNibName:nil bundle:nil] autorelease];
UINavigationController* controller = [[UINavigationController alloc] initWithRootViewController:viewController];
UIWindow* window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[window addSubview:controller.view];
[window makeKeyAndVisible];
return YES;
}
- (void)dealloc {
[super dealloc];
}
@end
//
// RKTwitterViewController.h
// RKTwitter
//
// Created by Blake Watters on 9/5/10.
// Copyright Two Toasters 2010. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <RestKit/RestKit.h>
@interface RKTwitterViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, RKObjectLoaderDelegate> {
UITableView* _tableView;
NSArray* _statuses;
}
@end
//
// RKTwitterViewController.m
// RKTwitter
//
// Created by Blake Watters on 9/5/10.
// Copyright Two Toasters 2010. All rights reserved.
//
#import "RKTwitterViewController.h"
#import "RKTStatus.h"
@interface RKTwitterViewController (Private)
- (void)loadData;
@end
@implementation RKTwitterViewController
- (void)loadTimeline {
// Load the object model via RestKit
RKObjectManager* objectManager = [RKObjectManager sharedManager];
objectManager.client.baseURL = @"http://www.twitter.com";
[objectManager loadObjectsAtResourcePath:@"/status/user_timeline/RestKit" delegate:self block:^(RKObjectLoader* loader) {
// Twitter returns statuses as a naked array in JSON, so we instruct the loader
// to user the appropriate object mapping
if ([objectManager.acceptMIMEType isEqualToString:RKMIMETypeJSON]) {
loader.objectMapping = [objectManager.mappingProvider objectMappingForClass:[RKTStatus class]];
}
}];
}
- (void)loadView {
[super loadView];
// Setup View and Table View
self.title = @"RestKit Tweets";
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackTranslucent;
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(loadTimeline)] autorelease];
UIImageView* imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"BG.png"]] autorelease];
imageView.frame = CGRectOffset(imageView.frame, 0, -64);
[self.view insertSubview:imageView atIndex:0];
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480-64) style:UITableViewStylePlain];
_tableView.dataSource = self;
_tableView.delegate = self;
_tableView.backgroundColor = [UIColor clearColor];
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:_tableView];
[self loadTimeline];
}
- (void)dealloc {
[_tableView release];
[_statuses release];
[super dealloc];
}
#pragma mark RKObjectLoaderDelegate methods
- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response {
NSLog(@"Loaded payload: %@", [response bodyAsString]);
}
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects {
NSLog(@"Loaded statuses: %@", objects);
[_statuses release];
_statuses = [objects retain];
[_tableView reloadData];
}
- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error {
UIAlertView* alert = [[[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
[alert show];
NSLog(@"Hit error: %@", error);
}
#pragma mark UITableViewDelegate methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGSize size = [[[_statuses objectAtIndex:indexPath.row] text] sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(300, 9000)];
return size.height + 10;
}
#pragma mark UITableViewDataSource methods
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
return [_statuses count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString* reuseIdentifier = @"Tweet Cell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (nil == cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier] autorelease];
cell.textLabel.font = [UIFont systemFontOfSize:14];
cell.textLabel.numberOfLines = 0;
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"listbg.png"]];
}
cell.textLabel.text = [[_statuses objectAtIndex:indexPath.row] text];
return cell;
}
@end
This diff is collapsed.
dependency 'RestKit-ObjectMapping'
dependency 'RestKit-JSON-JSONKit'
This diff is collapsed.
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "self:RKTwitter.xcodeproj">
</FileRef>
</Workspace>
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "group:Pods/Pods.xcodeproj">
</FileRef>
<FileRef
location = "group:RKTwitter.xcodeproj">
</FileRef>
</Workspace>
//
// Prefix header for all source files of the 'RKTwitter' target in the 'RKTwitter' project
//
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#endif
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleDisplayName</key>
<string>${PRODUCT_NAME}</string>
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
<key>CFBundleIconFile</key>
<string></string>
<key>CFBundleIdentifier</key>
<string>com.yourcompany.${PRODUCT_NAME:rfc1034identifier}</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>${PRODUCT_NAME}</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1.0</string>
<key>LSRequiresIPhoneOS</key>
<true/>
</dict>
</plist>
//
// main.m
// RKTwitter
//
// Created by Blake Watters on 9/5/10.
// Copyright Two Toasters 2010. All rights reserved.
//
#import <UIKit/UIKit.h>
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, @"RKTwitterAppDelegate");
[pool release];
return retVal;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment