@@ -42,67 +42,73 @@ - (instancetype)initWithConfiguration:(LinkedinSwiftConfiguration*)_configuratio
42
42
43
43
- (void )authorizeSuccess : (LinkedinSwiftAuthRequestSuccessCallback)successCallback error : (LinkedinSwiftRequestErrorCallback)errorCallback cancel : (LinkedinSwiftRequestCancelCallback)cancelCallback {
44
44
45
- __block LinkedinSwiftHelper *this = self;
46
- if ([LinkedinSwiftHelper isLinkedinAppInstalled ]) {
47
-
48
- /* *
49
- * If Linkedin app installed, use Linkedin sdk
50
- */
51
- __block LISDKSession *session = [[LISDKSessionManager sharedInstance ] session ];
52
-
53
- // check if session is still cached
54
- if (session && session.isValid ) {
45
+ /* *
46
+ * If previous token still in memory callback directly
47
+ */
48
+ if (lsAccessToken != nil && [lsAccessToken.expireDate timeIntervalSinceNow ] > 0 ) {
49
+ successCallback (lsAccessToken);
50
+ } else {
51
+ __block LinkedinSwiftHelper *this = self;
52
+ if ([LinkedinSwiftHelper isLinkedinAppInstalled ]) {
55
53
56
- this->lsAccessToken = [[LSLinkedinToken alloc ] initWithAccessToken: session.accessToken.accessTokenValue expireDate: session.accessToken.expiration fromMobileSDK: YES ];
57
- successCallback (this->lsAccessToken );
58
- } else {
54
+ /* *
55
+ * If Linkedin app installed, use Linkedin sdk
56
+ */
57
+ __block LISDKSession *session = [[LISDKSessionManager sharedInstance ] session ];
59
58
60
- // no cache, create a new session
61
- [LISDKSessionManager createSessionWithAuth: configuration.permissions state: @" GET-ACCESS-TOKEN " showGoToAppStoreDialog: YES successBlock: ^( NSString *returnState ) {
59
+ // check if session is still cached
60
+ if (session && session. isValid ) {
62
61
63
- // refresh session
64
- session = [[LISDKSessionManager sharedInstance ] session ];
65
62
this->lsAccessToken = [[LSLinkedinToken alloc ] initWithAccessToken: session.accessToken.accessTokenValue expireDate: session.accessToken.expiration fromMobileSDK: YES ];
66
63
successCallback (this->lsAccessToken );
64
+ } else {
67
65
68
- } errorBlock: ^(NSError *error) {
69
- // error code 3 means user cancelled, LISDKErrorCode.USER_CANCELLED doesn't work
70
- if (error.code == 3 ) {
71
- cancelCallback ();
72
- } else {
73
- errorCallback (error);
74
- }
75
- }];
76
- }
77
- } else {
78
-
79
- /* *
80
- * If Linkedin app is not installed, present a model webview to let use login
81
- *
82
- * WARNING: here we can check the cache save api call as well,
83
- * but there is a problem when you login on other devices the accessToken you cached will invalid,
84
- * and only you use this will be notice this, so I choose don't use this cache
85
- */
86
- [httpClient getAuthorizationCode: ^(NSString *code) {
66
+ // no cache, create a new session
67
+ [LISDKSessionManager createSessionWithAuth: configuration.permissions state: @" GET-ACCESS-TOKEN" showGoToAppStoreDialog: YES successBlock: ^(NSString *returnState) {
68
+
69
+ // refresh session
70
+ session = [[LISDKSessionManager sharedInstance ] session ];
71
+ this->lsAccessToken = [[LSLinkedinToken alloc ] initWithAccessToken: session.accessToken.accessTokenValue expireDate: session.accessToken.expiration fromMobileSDK: YES ];
72
+ successCallback (this->lsAccessToken );
73
+
74
+ } errorBlock: ^(NSError *error) {
75
+ // error code 3 means user cancelled, LISDKErrorCode.USER_CANCELLED doesn't work
76
+ if (error.code == 3 ) {
77
+ cancelCallback ();
78
+ } else {
79
+ errorCallback (error);
80
+ }
81
+ }];
82
+ }
83
+ } else {
87
84
88
- [this->httpClient getAccessToken: code success: ^(NSDictionary *dictionary) {
85
+ /* *
86
+ * If Linkedin app is not installed, present a model webview to let use login
87
+ *
88
+ * WARNING: here we can check the cache save api call as well,
89
+ * but there is a problem when you login on other devices the accessToken you cached will invalid,
90
+ * and only you use this will be notice this, so I choose don't use this cache
91
+ */
92
+ [httpClient getAuthorizationCode: ^(NSString *code) {
89
93
90
- NSString *accessToken = [dictionary objectForKey: @" access_token" ];
91
- NSNumber *expiresInSec = [dictionary objectForKey: @" expires_in" ];
94
+ [this->httpClient getAccessToken: code success: ^(NSDictionary *dictionary) {
95
+
96
+ NSString *accessToken = [dictionary objectForKey: @" access_token" ];
97
+ NSNumber *expiresInSec = [dictionary objectForKey: @" expires_in" ];
98
+
99
+ this->lsAccessToken = [[LSLinkedinToken alloc ] initWithAccessToken: accessToken expireDate: [NSDate dateWithTimeIntervalSinceNow: expiresInSec.doubleValue] fromMobileSDK: NO ];
100
+ successCallback (this->lsAccessToken );
101
+ } failure: ^(NSError *error) {
102
+ errorCallback (error);
103
+ }];
92
104
93
- this-> lsAccessToken = [[LSLinkedinToken alloc ] initWithAccessToken: accessToken expireDate: [ NSDate dateWithTimeIntervalSinceNow: expiresInSec.doubleValue] fromMobileSDK: NO ];
94
- successCallback (this-> lsAccessToken );
105
+ } cancel: ^{
106
+ cancelCallback ( );
95
107
} failure: ^(NSError *error) {
96
108
errorCallback (error);
97
109
}];
98
-
99
- } cancel: ^{
100
- cancelCallback ();
101
- } failure: ^(NSError *error) {
102
- errorCallback (error);
103
- }];
110
+ }
104
111
}
105
-
106
112
}
107
113
108
114
#pragma mark -
0 commit comments