forked from east520/SinaWeibo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HomeViewController.m
executable file
·265 lines (190 loc) · 7.33 KB
/
HomeViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//
// HomeViewController.m
// SinaWeibo
//
// Created by Liu East on 11-8-19.
// Copyright 2011 eshore. All rights reserved.
//
#import "HomeViewController.h"
#import "AFNetworking.h"
#ifndef kWBSDKDemoAppKey
#define kWBSDKDemoAppKey @"216747118"
#endif
#ifndef kWBSDKDemoAppSecret
#define kWBSDKDemoAppSecret @"d286230908d3c968640c135cb608af8c"
#endif
#ifndef kWBSDKDemoUsername
#define kWBSDKDemoUsername @"[email protected]"
#endif
#ifndef kWBSDKDemoPassword
#define kWBSDKDemoPassword @"439439439"
#endif
#define kWBAuthorizeURL @"https://api.weibo.com/oauth2/authorize"
#define kWBAccessTokenURL @"https://api.weibo.com/oauth2/access_token"
#define kWBAlertViewLogInTag 101
@interface HomeViewController (Private)
- (void)dismissTimelineViewController;
- (void)presentTimelineViewController:(BOOL)animated;
- (void)presentTimelineViewControllerWithoutAnimation;
- (void)refreshTimeline;
@end
@implementation HomeViewController
@synthesize weiBoEngine;
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
}
return self;
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
timeLine = [[NSMutableArray alloc] init];
timeLineTableView = [[UITableView alloc] init];
[timeLineTableView setDelegate:self];
[timeLineTableView setDataSource:self];
[timeLineTableView setBackgroundColor:[UIColor whiteColor]];
BOOL hasStatusBar = ![UIApplication sharedApplication].statusBarHidden;
int height = ((hasStatusBar) ? 20 : 0);
[timeLineTableView setFrame:CGRectMake(0, 0, 320, 480 - height - 32)];
[self.view addSubview:timeLineTableView];
[self startAuth];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
return YES;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
[weiBoEngine setDelegate:nil];
[weiBoEngine release], weiBoEngine = nil;
[timeLine release], timeLine = nil;
[timeLineTableView setDelegate:nil];
[timeLineTableView setDataSource:nil];
[timeLineTableView release], timeLineTableView = nil;
}
#pragma -
#pragma custom method
- (void) startAuth{
WBEngine *engine = [[WBEngine alloc] initWithAppKey:kWBSDKDemoAppKey appSecret:kWBSDKDemoAppSecret];
[engine setRootViewController:self];
[engine setDelegate:self];
[engine setRedirectURI:@"http://"];
[engine setIsUserExclusive:NO];
self.weiBoEngine = engine;
[engine release];
if ([weiBoEngine isLoggedIn] && ![weiBoEngine isAuthorizeExpired])
{
[self performSelector:@selector(presentTimelineViewControllerWithoutAnimation) withObject:nil afterDelay:0.0];
}
else
{
[weiBoEngine logIn];
}
}
- (void)presentTimelineViewControllerWithoutAnimation
{
[self presentTimelineViewController:NO];
}
- (void)presentTimelineViewController:(BOOL)animated
{
// WBSDKTimelineViewController *controller = [[WBSDKTimelineViewController alloc] initWithAppKey:kWBSDKDemoAppKey appSecret:kWBSDKDemoAppSecret];
//
// self.timeLineViewController = controller;
UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithTitle:@"登出" style:UIBarButtonItemStylePlain
target:self
action:@selector(onLogOutButtonPressed)];
[self.navigationItem setLeftBarButtonItem:leftBtn];
[leftBtn release];
UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithTitle:@"写微博" style:UIBarButtonItemStylePlain
target:self
action:@selector(onSendButtonPressed)];
[self.navigationItem setRightBarButtonItem:rightBtn];
[rightBtn release];
[self.navigationItem setTitle:@"微博"];
// UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];
// [controller release];
// [self presentModalViewController:navController animated:animated];
// [navController release];
// timeLineTableView = [[UITableView alloc] init];
// [timeLineTableView setDelegate:self];
// [timeLineTableView setDataSource:self];
// [timeLineTableView setBackgroundColor:[UIColor whiteColor]];
// [self.view addSubview:timeLineTableView];
[self refreshTimeline];
}
- (void)engineDidLogIn:(WBEngine *)engine
{
//[indicatorView stopAnimating];
UIAlertView* alertView = [[UIAlertView alloc]initWithTitle:nil
message:@"登录成功!"
delegate:self
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alertView setTag:kWBAlertViewLogInTag];
[alertView show];
[alertView release];
}
#pragma mark - WBSDKTimelineViewController Private Methods
- (void)refreshTimeline
{
[weiBoEngine loadRequestWithMethodName:@"statuses/home_timeline.json"
httpMethod:@"GET"
params:nil
httpHeaderFields:nil];
}
#pragma mark - UITableViewDelegate Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 66;
}
#pragma mark - UITableViewDataSource Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [timeLine count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Timeline Cell"];
if (!cell)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Timeline Cell"] autorelease];
}
NSDictionary *detail = [timeLine objectAtIndex:indexPath.row];
[cell.textLabel setText:[[detail objectForKey:@"user"] objectForKey:@"screen_name"]];
[cell.detailTextLabel setText:[detail objectForKey:@"text"]];
return cell;
}
#pragma mark - WBEngineDelegate Methods
- (void)engine:(WBEngine *)engine requestDidSucceedWithResult:(id)result
{
NSLog(@"requestDidSucceedWithResult: %@", result);
if ([result isKindOfClass:[NSDictionary class]])
{
NSDictionary *dict = (NSDictionary *)result;
[timeLine addObjectsFromArray:[dict objectForKey:@"statuses"]];
[timeLineTableView reloadData];
}
}
@end