-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAppDelegate.m
109 lines (76 loc) · 3.88 KB
/
AppDelegate.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
#import "AppDelegate.h"
#import "RootViewController.h"
#import "LoginViewController.h"
#import "GTMOAuth2ViewControllerTouch.h"
#import "TaskListViewController.h"
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(UIApplication *)application {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
__block UINavigationController *navController = nil;
// [[[UIAlertView alloc] initWithTitle:@"auth desc" message:[self auth].debugDescription delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil] show];
if([self auth].canAuthorize){
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;
navController = [[UINavigationController alloc] initWithRootViewController:tasksListViewController];
self.window.rootViewController = navController;
}
}];
} else {
LoginViewController *loginViewController = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
navController = [[UINavigationController alloc] initWithRootViewController:loginViewController];
self.window.rootViewController = navController;
}
[self.window makeKeyAndVisible];
}
- (void)applicationWillTerminate:(UIApplication *)application {
[[NSUserDefaults standardUserDefaults] synchronize];
}
+(AppDelegate *)appDelegate
{
return [[UIApplication sharedApplication] delegate];
}
- (void)signOut {
[GTMOAuth2ViewControllerTouch revokeTokenForGoogleAuthentication:self.auth];
// remove the stored Google authentication from the keychain, if any
[GTMOAuth2ViewControllerTouch removeAuthFromKeychainForName:kKeychainItemName];
LoginViewController *loginViewController = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
self.window.rootViewController = loginViewController;
[self.window makeKeyAndVisible];
}
- (GTMOAuth2Authentication *)auth{
// First, we'll try to get the saved Google authentication, if any, from
// the keychain
NSString *clientID = myClientId;
NSString *clientSecret = mySecretKey;
GTMOAuth2Authentication *auth = nil;
if (clientID && clientSecret) {
auth = [GTMOAuth2ViewControllerTouch authForGoogleFromKeychainForName:kKeychainItemName
clientID:clientID
clientSecret:clientSecret];
}
return auth;
}
- (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;
}
@end