Skip to content

Commit 68439c6

Browse files
committed
When in memory token cache still valid callback directly
1 parent 19f5ab0 commit 68439c6

File tree

3 files changed

+55
-49
lines changed

3 files changed

+55
-49
lines changed

LinkedinSwift.podspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Pod::Spec.new do |s|
22

33
s.name = "LinkedinSwift"
4-
s.version = "1.4"
4+
s.version = "1.5"
55
s.summary = "Linkedin Oauth Helper, depend on Linkedin Native App installed or not, using Linkdin IOS SDK or UIWebView to login, support Swift with iOS 7"
66

77
s.homepage = "https://github.com/tonyli508/LinkedinSwift.git"

LinkedinSwift/LinkedinSwift/sources/LinkedinSwiftHelper.m

+54-48
Original file line numberDiff line numberDiff line change
@@ -42,67 +42,73 @@ - (instancetype)initWithConfiguration:(LinkedinSwiftConfiguration*)_configuratio
4242

4343
- (void)authorizeSuccess:(LinkedinSwiftAuthRequestSuccessCallback)successCallback error:(LinkedinSwiftRequestErrorCallback)errorCallback cancel:(LinkedinSwiftRequestCancelCallback)cancelCallback {
4444

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]) {
5553

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];
5958

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) {
6261

63-
// refresh session
64-
session = [[LISDKSessionManager sharedInstance] session];
6562
this->lsAccessToken = [[LSLinkedinToken alloc] initWithAccessToken:session.accessToken.accessTokenValue expireDate:session.accessToken.expiration fromMobileSDK: YES];
6663
successCallback(this->lsAccessToken);
64+
} else {
6765

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 {
8784

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) {
8993

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+
}];
92104

93-
this->lsAccessToken = [[LSLinkedinToken alloc] initWithAccessToken:accessToken expireDate:[NSDate dateWithTimeIntervalSinceNow:expiresInSec.doubleValue] fromMobileSDK: NO];
94-
successCallback(this->lsAccessToken);
105+
} cancel:^{
106+
cancelCallback();
95107
} failure:^(NSError *error) {
96108
errorCallback(error);
97109
}];
98-
99-
} cancel:^{
100-
cancelCallback();
101-
} failure:^(NSError *error) {
102-
errorCallback(error);
103-
}];
110+
}
104111
}
105-
106112
}
107113

108114
#pragma mark -

0 commit comments

Comments
 (0)