forked from facebook/facebook-ios-sdk
/
RootViewController.m
453 lines (388 loc) · 16.5 KB
/
RootViewController.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*
* Copyright 2010 Facebook
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import "RootViewController.h"
#import "HackbookAppDelegate.h"
#import "FBConnect.h"
#import "APICallsViewController.h"
@implementation RootViewController
@synthesize permissions;
@synthesize backgroundImageView;
@synthesize menuTableView;
@synthesize mainMenuItems;
@synthesize headerView;
@synthesize nameLabel;
@synthesize profilePhotoImageView;
- (void)dealloc {
[permissions release];
[backgroundImageView release];
[loginButton release];
[menuTableView release];
[mainMenuItems release];
[headerView release];
[nameLabel release];
[profilePhotoImageView release];
[super dealloc];
}
- (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.
}
#pragma mark - Facebook API Calls
/**
* Make a Graph API Call to get information about the current logged in user.
*/
- (void)apiFQLIMe {
// Using the "pic" picture since this currently has a maximum width of 100 pixels
// and since the minimum profile picture size is 180 pixels wide we should be able
// to get a 100 pixel wide version of the profile picture
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"SELECT uid, name, pic FROM user WHERE uid=me()", @"query",
nil];
HackbookAppDelegate *delegate = (HackbookAppDelegate *)[[UIApplication sharedApplication] delegate];
[[delegate facebook] requestWithMethodName:@"fql.query"
andParams:params
andHttpMethod:@"POST"
andDelegate:self];
}
- (void)apiGraphUserPermissions {
HackbookAppDelegate *delegate = (HackbookAppDelegate *)[[UIApplication sharedApplication] delegate];
[[delegate facebook] requestWithGraphPath:@"me/permissions" andDelegate:self];
}
#pragma - Private Helper Methods
/**
* Show the logged in menu
*/
- (void)showLoggedIn {
[self.navigationController setNavigationBarHidden:NO animated:NO];
self.backgroundImageView.hidden = YES;
loginButton.hidden = YES;
self.menuTableView.hidden = NO;
[self apiFQLIMe];
}
/**
* Show the logged in menu
*/
- (void)showLoggedOut {
[self.navigationController setNavigationBarHidden:YES animated:NO];
self.menuTableView.hidden = YES;
self.backgroundImageView.hidden = NO;
loginButton.hidden = NO;
// Clear personal info
nameLabel.text = @"";
// Get the profile image
[profilePhotoImageView setImage:nil];
[[self navigationController] popToRootViewControllerAnimated:YES];
}
/**
* Show the authorization dialog.
*/
- (void)login {
HackbookAppDelegate *delegate = (HackbookAppDelegate *)[[UIApplication sharedApplication] delegate];
if (![[delegate facebook] isSessionValid]) {
[[delegate facebook] authorize:permissions];
} else {
[self showLoggedIn];
}
}
/**
* Invalidate the access token and clear the cookie.
*/
- (void)logout {
HackbookAppDelegate *delegate = (HackbookAppDelegate *)[[UIApplication sharedApplication] delegate];
[[delegate facebook] logout];
}
/**
* Helper method called when a menu button is clicked
*/
- (void)menuButtonClicked:(id)sender {
// Each menu button in the UITableViewController is initialized
// with a tag representing the table cell row. When the button
// is clicked the button is passed along in the sender object.
// From this object we can then read the tag property to determine
// which menu button was clicked.
APICallsViewController *controller = [[APICallsViewController alloc]
initWithIndex:[sender tag]];
pendingApiCallsController = controller;
[self.navigationController pushViewController:controller animated:YES];
[controller release];
}
#pragma mark - View lifecycle
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
UIView *view = [[UIView alloc] initWithFrame:[UIScreen
mainScreen].applicationFrame];
[view setBackgroundColor:[UIColor whiteColor]];
self.view = view;
[view release];
// Initialize permissions
permissions = [[NSArray alloc] initWithObjects:@"offline_access", nil];
// Main menu items
mainMenuItems = [[NSMutableArray alloc] initWithCapacity:1];
HackbookAppDelegate *delegate = (HackbookAppDelegate *)[[UIApplication sharedApplication] delegate];
NSArray *apiInfo = [[delegate apiData] apiConfigData];
for (NSUInteger i=0; i < [apiInfo count]; i++) {
[mainMenuItems addObject:[[apiInfo objectAtIndex:i] objectForKey:@"title"]];
}
// Set up the view programmatically
self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.title = @"Hackbook for iOS";
self.navigationItem.backBarButtonItem =
[[[UIBarButtonItem alloc] initWithTitle:@"Back"
style:UIBarButtonItemStyleBordered
target:nil
action:nil] autorelease];
// Background Image
backgroundImageView = [[UIImageView alloc]
initWithFrame:CGRectMake(0,0,
self.view.bounds.size.width,
self.view.bounds.size.height)];
[backgroundImageView setImage:[UIImage imageNamed:@"Default.png"]];
//[backgroundImageView setAlpha:0.25];
[self.view addSubview:backgroundImageView];
// Login Button
loginButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
CGFloat xLoginButtonOffset = self.view.center.x - (318/2);
CGFloat yLoginButtonOffset = self.view.bounds.size.height - (58 + 13);
loginButton.frame = CGRectMake(xLoginButtonOffset,yLoginButtonOffset,318,58);
[loginButton addTarget:self
action:@selector(login)
forControlEvents:UIControlEventTouchUpInside];
[loginButton setImage:
[UIImage imageNamed:@"FBConnect.bundle/images/LoginWithFacebookNormal@2x.png"]
forState:UIControlStateNormal];
[loginButton setImage:
[UIImage imageNamed:@"FBConnect.bundle/images/LoginWithFacebookPressed@2x.png"]
forState:UIControlStateHighlighted];
[loginButton sizeToFit];
[self.view addSubview:loginButton];
// Main Menu Table
menuTableView = [[UITableView alloc] initWithFrame:self.view.bounds
style:UITableViewStylePlain];
[menuTableView setBackgroundColor:[UIColor whiteColor]];
menuTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
menuTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
menuTableView.dataSource = self;
menuTableView.delegate = self;
menuTableView.hidden = YES;
//[self.view addSubview:menuTableView];
// Table header
headerView = [[UIView alloc]
initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 100)];
headerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
headerView.backgroundColor = [UIColor whiteColor];
CGFloat xProfilePhotoOffset = self.view.center.x - 25.0;
profilePhotoImageView = [[UIImageView alloc]
initWithFrame:CGRectMake(xProfilePhotoOffset, 20, 50, 50)];
profilePhotoImageView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
[headerView addSubview:profilePhotoImageView];
nameLabel = [[UILabel alloc]
initWithFrame:CGRectMake(0, 75, self.view.bounds.size.width, 20.0)];
nameLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
nameLabel.textAlignment = UITextAlignmentCenter;
nameLabel.text = @"";
[headerView addSubview:nameLabel];
menuTableView.tableHeaderView = headerView;
[self.view addSubview:menuTableView];
pendingApiCallsController = nil;
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated {
//[self.navigationController setNavigationBarHidden:YES animated:animated];
[super viewWillAppear:animated];
HackbookAppDelegate *delegate = (HackbookAppDelegate *)[[UIApplication sharedApplication] delegate];
if (![[delegate facebook] isSessionValid]) {
[self showLoggedOut];
} else {
[self showLoggedIn];
}
}
- (void)viewWillDisappear:(BOOL)animated {
[self.navigationController setNavigationBarHidden:NO animated:animated];
[super viewWillDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
#pragma mark - UITableViewDatasource and UITableViewDelegate Methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 60.0;
}
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [mainMenuItems count];
}
// Customize the appearance of table view cells.
- (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.selectionStyle = UITableViewCellSelectionStyleNone;
}
//create the button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(20, 20, (cell.contentView.frame.size.width-40), 44);
button.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;
[button setBackgroundImage:[[UIImage imageNamed:@"MenuButton.png"]
stretchableImageWithLeftCapWidth:9 topCapHeight:9]
forState:UIControlStateNormal];
[button setTitle:[mainMenuItems objectAtIndex:indexPath.row]
forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(menuButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
button.tag = indexPath.row;
[cell.contentView addSubview:button];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
- (void)storeAuthData:(NSString *)accessToken expiresAt:(NSDate *)expiresAt {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:accessToken forKey:@"FBAccessTokenKey"];
[defaults setObject:expiresAt forKey:@"FBExpirationDateKey"];
[defaults synchronize];
}
#pragma mark - FBSessionDelegate Methods
/**
* Called when the user has logged in successfully.
*/
- (void)fbDidLogin {
[self showLoggedIn];
HackbookAppDelegate *delegate = (HackbookAppDelegate *)[[UIApplication sharedApplication] delegate];
[self storeAuthData:[[delegate facebook] accessToken] expiresAt:[[delegate facebook] expirationDate]];
[pendingApiCallsController userDidGrantPermission];
}
-(void)fbDidExtendToken:(NSString *)accessToken expiresAt:(NSDate *)expiresAt {
NSLog(@"token extended");
[self storeAuthData:accessToken expiresAt:expiresAt];
}
/**
* Called when the user canceled the authorization dialog.
*/
-(void)fbDidNotLogin:(BOOL)cancelled {
[pendingApiCallsController userDidNotGrantPermission];
}
/**
* Called when the request logout has succeeded.
*/
- (void)fbDidLogout {
pendingApiCallsController = nil;
// Remove saved authorization information if it exists and it is
// ok to clear it (logout, session invalid, app unauthorized)
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:@"FBAccessTokenKey"];
[defaults removeObjectForKey:@"FBExpirationDateKey"];
[defaults synchronize];
[self showLoggedOut];
}
/**
* Called when the session has expired.
*/
- (void)fbSessionInvalidated {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Auth Exception"
message:@"Your session has expired."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil,
nil];
[alertView show];
[alertView release];
[self fbDidLogout];
}
#pragma mark - FBRequestDelegate Methods
/**
* Called when the Facebook API request has returned a response.
*
* This callback gives you access to the raw response. It's called before
* (void)request:(FBRequest *)request didLoad:(id)result,
* which is passed the parsed response object.
*/
- (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response {
//NSLog(@"received response");
}
/**
* Called when a request returns and its response has been parsed into
* an object.
*
* The resulting object may be a dictionary, an array or a string, depending
* on the format of the API response. If you need access to the raw response,
* use:
*
* (void)request:(FBRequest *)request
* didReceiveResponse:(NSURLResponse *)response
*/
- (void)request:(FBRequest *)request didLoad:(id)result {
if ([result isKindOfClass:[NSArray class]]) {
result = [result objectAtIndex:0];
}
// This callback can be a result of getting the user's basic
// information or getting the user's permissions.
if ([result objectForKey:@"name"]) {
// If basic information callback, set the UI objects to
// display this.
nameLabel.text = [result objectForKey:@"name"];
// Get the profile image
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[result objectForKey:@"pic"]]]];
// Resize, crop the image to make sure it is square and renders
// well on Retina display
float ratio;
float delta;
float px = 100; // Double the pixels of the UIImageView (to render on Retina)
CGPoint offset;
CGSize size = image.size;
if (size.width > size.height) {
ratio = px / size.width;
delta = (ratio*size.width - ratio*size.height);
offset = CGPointMake(delta/2, 0);
} else {
ratio = px / size.height;
delta = (ratio*size.height - ratio*size.width);
offset = CGPointMake(0, delta/2);
}
CGRect clipRect = CGRectMake(-offset.x, -offset.y,
(ratio * size.width) + delta,
(ratio * size.height) + delta);
UIGraphicsBeginImageContext(CGSizeMake(px, px));
UIRectClip(clipRect);
[image drawInRect:clipRect];
UIImage *imgThumb = UIGraphicsGetImageFromCurrentImageContext();
[imgThumb retain];
[profilePhotoImageView setImage:imgThumb];
[self apiGraphUserPermissions];
} else {
// Processing permissions information
HackbookAppDelegate *delegate = (HackbookAppDelegate *)[[UIApplication sharedApplication] delegate];
[delegate setUserPermissions:[[result objectForKey:@"data"] objectAtIndex:0]];
}
}
/**
* Called when an error prevents the Facebook API request from completing
* successfully.
*/
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error {
NSLog(@"Err message: %@", [[error userInfo] objectForKey:@"error_msg"]);
NSLog(@"Err code: %d", [error code]);
}
@end