-
Notifications
You must be signed in to change notification settings - Fork 1
/
RootViewController.m
567 lines (453 loc) · 20.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
#import "RootViewController.h"
#import "GTMOAuth2ViewControllerTouch.h"
#import "GTMOAuth2SignIn.h"
#import "GTLUtilities.h"
#import "GTMHTTPFetcherLogging.h"
#import "TaskListViewController.h"
#define kViewTag 1 // for tagging our embedded controls for removal at cell recycle time
@interface RootViewController()
@property (readonly) GTLServiceTasks *tasksService;
@property (nonatomic, strong, readonly) UISwitch *shouldSaveInKeychainSwitch;
- (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController
finishedWithAuth:(GTMOAuth2Authentication *)auth
error:(NSError *)error;
- (void)incrementNetworkActivity:(NSNotification *)notify;
- (void)decrementNetworkActivity:(NSNotification *)notify;
- (void)signInNetworkLostOrFound:(NSNotification *)notify;
- (void)doAnAuthenticatedAPIFetch;
- (BOOL)shouldSaveInKeychain;
@end
@implementation RootViewController
//@synthesize mTableView,
@synthesize shouldSaveInKeychainSwitch;
@synthesize auth = mAuth;
-(id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Listen for network change notifications
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(incrementNetworkActivity:) name:kGTMOAuth2WebViewStartedLoading object:nil];
[nc addObserver:self selector:@selector(decrementNetworkActivity:) name:kGTMOAuth2WebViewStoppedLoading object:nil];
[nc addObserver:self selector:@selector(incrementNetworkActivity:) name:kGTMOAuth2FetchStarted object:nil];
[nc addObserver:self selector:@selector(decrementNetworkActivity:) name:kGTMOAuth2FetchStopped object:nil];
[nc addObserver:self selector:@selector(signInNetworkLostOrFound:) name:kGTMOAuth2NetworkLost object:nil];
[nc addObserver:self selector:@selector(signInNetworkLostOrFound:) name:kGTMOAuth2NetworkFound object:nil];
// Fill in the Client ID and Client Secret text fields
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// First, we'll try to get the saved Google authentication, if any, from
// the keychain
// Normal applications will hardcode in their client ID and client secret,
// but the sample app allows the user to enter them in a text field, and
// saves them in the preferences
NSString *clientID = [defaults stringForKey:kGoogleClientIDKey];
NSString *clientSecret = [defaults stringForKey:kGoogleClientSecretKey];
GTMOAuth2Authentication *auth = nil;
if (clientID && clientSecret) {
auth = [GTMOAuth2ViewControllerTouch authForGoogleFromKeychainForName:kKeychainItemName
clientID:clientID
clientSecret:clientSecret];
}
// Save the authentication object, which holds the auth tokens and
// the scope string used to obtain the token. For Google services,
// the auth object also holds the user's email address.
self.auth = auth;
// self.mTableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
// self.mTableView.dataSource = self;
// self.mTableView.delegate = self;
// [self.view addSubview:self.mTableView];
BOOL isRemembering = [self shouldSaveInKeychain];
self.shouldSaveInKeychainSwitch.on = isRemembering;
[SSThemeManager customizeTableView:self.tableView];
}
return self;
}
- (void)awakeFromNib {
// Listen for network change notifications
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(incrementNetworkActivity:) name:kGTMOAuth2WebViewStartedLoading object:nil];
[nc addObserver:self selector:@selector(decrementNetworkActivity:) name:kGTMOAuth2WebViewStoppedLoading object:nil];
[nc addObserver:self selector:@selector(incrementNetworkActivity:) name:kGTMOAuth2FetchStarted object:nil];
[nc addObserver:self selector:@selector(decrementNetworkActivity:) name:kGTMOAuth2FetchStopped object:nil];
[nc addObserver:self selector:@selector(signInNetworkLostOrFound:) name:kGTMOAuth2NetworkLost object:nil];
[nc addObserver:self selector:@selector(signInNetworkLostOrFound:) name:kGTMOAuth2NetworkFound object:nil];
// Fill in the Client ID and Client Secret text fields
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// First, we'll try to get the saved Google authentication, if any, from
// the keychain
// Normal applications will hardcode in their client ID and client secret,
// but the sample app allows the user to enter them in a text field, and
// saves them in the preferences
NSString *clientID = [defaults stringForKey:kGoogleClientIDKey];
NSString *clientSecret = [defaults stringForKey:kGoogleClientSecretKey];
GTMOAuth2Authentication *auth = nil;
if (clientID && clientSecret) {
auth = [GTMOAuth2ViewControllerTouch authForGoogleFromKeychainForName:kKeychainItemName
clientID:clientID
clientSecret:clientSecret];
}
// Save the authentication object, which holds the auth tokens and
// the scope string used to obtain the token. For Google services,
// the auth object also holds the user's email address.
self.auth = auth;
// self.mTableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
// self.mTableView.dataSource = self;
// self.mTableView.delegate = self;
// [self.view addSubview:self.mTableView];
BOOL isRemembering = [self shouldSaveInKeychain];
self.shouldSaveInKeychainSwitch.on = isRemembering;
[SSThemeManager customizeTableView:self.tableView];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation {
// Returns non-zero on iPad, but backward compatible to SDKs earlier than 3.2.
if (UI_USER_INTERFACE_IDIOM()) {
return YES;
}
return [super shouldAutorotateToInterfaceOrientation:orientation];
}
// I think this one matters.
- (BOOL)isSignedIn {
BOOL isSignedIn = self.auth.canAuthorize;
return isSignedIn;
}
- (void)signInOutClicked:(id)sender {
if (![self isSignedIn]) {
[self signInToGoogle];
} else {
// Sign out
[self signOut];
}
[self updateUI];
}
- (void)fetchClicked:(id)sender {
// Just to prove we're signed in, we'll attempt an authenticated fetch for the
// signed-in user
[self doAnAuthenticatedAPIFetch];
}
- (void)expireNowClicked:(id)sender {
NSDate *date = self.auth.expirationDate;
if (date) {
self.auth.expirationDate = [NSDate dateWithTimeIntervalSince1970:0];
[self updateUI];
}
}
// UISwitch does the toggling for us. We just need to read the state.
- (void)toggleShouldSaveInKeychain:(UISwitch *)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:sender.isOn forKey:kShouldSaveInKeychainKey];
[defaults synchronize];
}
- (void)signOut {
if ([self.auth.serviceProvider isEqual:kGTMOAuth2ServiceProviderGoogle]) {
// remove the token from Google's servers
[GTMOAuth2ViewControllerTouch revokeTokenForGoogleAuthentication:self.auth];
}
// remove the stored Google authentication from the keychain, if any
[GTMOAuth2ViewControllerTouch removeAuthFromKeychainForName:kKeychainItemName];
// remove the stored DailyMotion authentication from the keychain, if any
// Discard our retained authentication object.
self.auth = nil;
[self updateUI];
}
- (void)signInToGoogle {
[self signOut];
NSString *keychainItemName = nil;
if ([self shouldSaveInKeychain]) {
keychainItemName = kKeychainItemName;
}
// For Google APIs, the scope strings are available
// in the service constant header files.
NSString *scope = @"https://www.googleapis.com/auth/tasks";
// Typically, applications will hardcode the client ID and client secret
// strings into the source code; they should not be user-editable or visible.
//
// But for this sample code, they are editable.
NSString *clientID = myClientId; // self.clientIDField.text;
NSString *clientSecret = mySecretKey; //self.clientSecretField.text;
// Note:
// GTMOAuth2ViewControllerTouch is not designed to be reused. Make a new
// one each time you are going to show it.
// Display the autentication view.
SEL finishedSel = @selector(viewController:finishedWithAuth:error:);
GTMOAuth2ViewControllerTouch *viewController;
viewController = [GTMOAuth2ViewControllerTouch controllerWithScope:scope
clientID:clientID
clientSecret:clientSecret
keychainItemName:keychainItemName
delegate:self
finishedSelector:finishedSel];
// You can set the title of the navigationItem of the controller here, if you
// want.
// If the keychainItemName is not nil, the user's authorization information
// will be saved to the keychain. By default, it saves with accessibility
// kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly, but that may be
// customized here. For example,
//
// viewController.keychainItemAccessibility = kSecAttrAccessibleAlways;
// During display of the sign-in window, loss and regain of network
// connectivity will be reported with the notifications
// kGTMOAuth2NetworkLost/kGTMOAuth2NetworkFound
//
// See the method signInNetworkLostOrFound: for an example of handling
// the notification.
// Optional: Google servers allow specification of the sign-in display
// language as an additional "hl" parameter to the authorization URL,
// using BCP 47 language codes.
//
// For this sample, we'll force English as the display language.
NSDictionary *params = @{@"hl": @"en"};
viewController.signIn.additionalAuthorizationParameters = params;
// By default, the controller will fetch the user's email, but not the rest of
// the user's profile. The full profile can be requested from Google's server
// by setting this property before sign-in:
//
// viewController.signIn.shouldFetchGoogleUserProfile = YES;
//
// The profile will be available after sign-in as
//
// NSDictionary *profile = viewController.signIn.userProfile;
// Optional: display some html briefly before the sign-in page loads
NSString *html = @"<html><body bgcolor=silver><div align=center>Loading sign-in page...</div></body></html>";
viewController.initialHTMLString = html;
[self presentViewController:viewController animated:YES completion:nil];
// [[self navigationController] pushViewController:viewController animated:YES];
// The view controller will be popped before signing in has completed, as
// there are some additional fetches done by the sign-in controller.
// The kGTMOAuth2UserSignedIn notification will be posted to indicate
// that the view has been popped and those additional fetches have begun.
// It may be useful to display a temporary UI when kGTMOAuth2UserSignedIn is
// posted, just until the finished selector is invoked.
}
- (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController
finishedWithAuth:(GTMOAuth2Authentication *)auth
error:(NSError *)error {
if (error != nil) {
// Authentication failed (perhaps the user denied access, or closed the
// window before granting access)
DebugLog(@"Authentication error: %@", error);
NSData *responseData = [error userInfo][@"data"]; // kGTMHTTPFetcherStatusDataKey
if ([responseData length] > 0) {
// show the body of the server's authentication failure response
NSString *str = [[NSString alloc] initWithData:responseData
encoding:NSUTF8StringEncoding];
DebugLog(@"%@", str);
}
self.auth = nil;
} else {
// Authentication succeeded
//
//
[self dismissViewControllerAnimated:YES completion:nil];
DebugLog(@"auth: succesfully!");
// if (error == nil) {
// self.tasksService.authorizer = auth;
// TaskListViewController *tasksListViewController = [[TaskListViewController alloc] initWithStyle:UITableViewStylePlain];
// tasksListViewController.tasksService = self.tasksService;
//
// [self.navigationController pushViewController:tasksListViewController animated:YES];
//
// DebugLog(@"sign in succesfuuly");
// } else {
// self.taskListsFetchError = error;
// [self updateUI];
// }
// save the authentication object
self.auth = auth;
}
[self updateUI];
}
- (void)doAnAuthenticatedAPIFetch {
NSString *urlStr = @"https://www.googleapis.com/tasks/v1/users/@me/lists";
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[self.auth authorizeRequest:request
completionHandler:^(NSError *error) {
NSString *output = nil;
if (error) {
output = [error description];
} else {
self.tasksService.authorizer = self.auth;
TaskListViewController *tasksListViewController = [[TaskListViewController alloc] initWithStyle:UITableViewStylePlain];
tasksListViewController.tasksService = self.tasksService;
[self.navigationController pushViewController:tasksListViewController animated:YES];
}
}];
}
#pragma mark -
- (void)incrementNetworkActivity:(NSNotification *)notify {
++mNetworkActivityCounter;
if (mNetworkActivityCounter == 1) {
UIApplication *app = [UIApplication sharedApplication];
[app setNetworkActivityIndicatorVisible:YES];
}
}
- (void)decrementNetworkActivity:(NSNotification *)notify {
--mNetworkActivityCounter;
if (mNetworkActivityCounter == 0) {
UIApplication *app = [UIApplication sharedApplication];
[app setNetworkActivityIndicatorVisible:NO];
}
}
- (void)signInNetworkLostOrFound:(NSNotification *)notify {
if ([[notify name] isEqual:kGTMOAuth2NetworkLost]) {
// network connection was lost; alert the user, or dismiss
// the sign-in view with
[[[notify object] delegate] cancelSigningIn];
} else {
// network connection was found again
}
}
#pragma mark -
- (void)updateUI {
// update the text showing the signed-in state and the button title
[self.tableView reloadData];
}
- (BOOL)shouldSaveInKeychain {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL flag = [defaults boolForKey:kShouldSaveInKeychainKey];
return flag;
}
#pragma mark -
// get a service object with the current username/password
//
// A "service" object handles networking tasks. Service objects
// contain user authentication information as well as networking
// state information (such as cookies and the "last modified" date for
// fetched data.)
- (GTLServiceTasks *)tasksService {
static GTLServiceTasks *service = nil;
if (!service) {
service = [[GTLServiceTasks alloc] init];
// Have the service object set tickets to fetch consecutive pages
// of the feed so we do not need to manually fetch them
service.shouldFetchNextPages = YES;
// Have the service object set tickets to retry temporary error conditions
// automatically
service.retryEnabled = YES;
}
return service;
}
#pragma mark - uitableview datasource & delegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return (section == 0) ?44.0:20.0;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return (section == 0)?1:4;
}
- (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];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
} else
{
// the cell is being recycled, remove old embedded controls
UIView *viewToRemove = nil;
viewToRemove = [cell.contentView viewWithTag:kViewTag];
if (viewToRemove)
[viewToRemove removeFromSuperview];
}
// Configure the cell...
NSString *title = @"";
switch (indexPath.section) {
case 0:
title = [self isSignedIn]?self.auth.userEmail:NSLocalizedString(@"YOU_ARE_NOT_SIGNED_IN", @"You are not signed in");
break;
case 1:
{
switch (indexPath.row) {
case 0:
title = [self isSignedIn]? NSLocalizedString(@"SIGN_OUT", @"Sign Out"): NSLocalizedString(@"SIGN_IN", @"Sign In");
break;
case 1:
{
title = NSLocalizedString(@"SHOULD_SAVE", @"Should Save?");
UIControl *control = [self shouldSaveInKeychainSwitch];
cell.accessoryView = control;
BOOL isRemembering = [self shouldSaveInKeychain];
self.shouldSaveInKeychainSwitch.on = isRemembering;
break;
}
case 2:
title = NSLocalizedString(@"SHOW_LIST", @"show list");
break;
case 3:
title = NSLocalizedString(@"EXPIRE_NOW", @"Expire Now");
break;
default:
break;
}
break;
}
default:
break;
}
// todo:
if ([self isSignedIn]) {
// NSAttributedString
} else {
}
cell.textLabel.text = title;
cell.textLabel.font = SYSTEM_TEXT_FONT;
cell.textLabel.textColor = SYSTEM_TEXT_COLOR;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
} else {
switch (indexPath.row) {
case 0:
[self signInOutClicked:nil];
break;
case 2:
{
if ([self isSignedIn])
[self fetchClicked:nil];
else
[tableView deselectRowAtIndexPath:indexPath animated:NO];
break;
}
case 3:
{
if ([self isSignedIn])
[self expireNowClicked:nil];
else
[tableView deselectRowAtIndexPath:indexPath animated:NO];
break;
}
default:
[tableView deselectRowAtIndexPath:indexPath animated:NO];
break;
}
}
}
- (UISwitch *)shouldSaveInKeychainSwitch
{
if (shouldSaveInKeychainSwitch == nil)
{
CGRect frame = CGRectMake(198.0, 12.0, 94.0, 27.0);
shouldSaveInKeychainSwitch = [[UISwitch alloc] initWithFrame:frame];
[shouldSaveInKeychainSwitch addTarget:self action:@selector(toggleShouldSaveInKeychain:) forControlEvents:UIControlEventValueChanged];
// in case the parent view draws with a custom color or gradient, use a transparent color
shouldSaveInKeychainSwitch.backgroundColor = [UIColor clearColor];
[shouldSaveInKeychainSwitch setAccessibilityLabel:NSLocalizedString(@"StandardSwitch", @"")];
shouldSaveInKeychainSwitch.tag = kViewTag; // tag this view for later so we can remove it from recycled table cells
}
return shouldSaveInKeychainSwitch;
}
@end