Skip to content

Commit

Permalink
Merge pull request #70 from tonyli508/master
Browse files Browse the repository at this point in the history
Add support for AFNetworking 3+ and backwards support for <3
  • Loading branch information
jeyben committed May 26, 2016
2 parents c96bc70 + c998de3 commit ccf8e34
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
18 changes: 16 additions & 2 deletions IOSLinkedInAPI/LIALinkedInHttpClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,28 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#import "AFHTTPRequestOperationManager.h"
/**
* Check if before AFNetworking 3.0
*/
#if __has_include_next("AFNetworking/AFHTTPRequestOperationManager.h")

#import <AFNetworking/AFHTTPRequestOperationManager.h>
#define AFHTTPManager AFHTTPRequestOperationManager

#elif __has_include_next("AFNetworking/AFHTTPSessionManager.h")

#import <AFNetworking/AFHTTPSessionManager.h>
#define AFHTTPManager AFHTTPSessionManager
#define isSessionManager 1

#endif

@class LIALinkedInApplication;

/**
* A LinkedIn client is created using a `LIALinkedInApplication` and is the network instance that will perform all requests to the LinkedIn API.
**/
@interface LIALinkedInHttpClient : AFHTTPRequestOperationManager
@interface LIALinkedInHttpClient : AFHTTPManager

/** ************************************************************************************************ **
* @name Initializers
Expand Down
34 changes: 25 additions & 9 deletions IOSLinkedInAPI/LIALinkedInHttpClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,39 @@ - (void)getAccessToken:(NSString *)authorizationCode success:(void (^)(NSDiction
NSString *accessTokenUrl = @"/uas/oauth2/accessToken?grant_type=authorization_code&code=%@&redirect_uri=%@&client_id=%@&client_secret=%@";
NSString *url = [NSString stringWithFormat:accessTokenUrl, authorizationCode, [self.application.redirectURL LIAEncode], self.application.clientId, self.application.clientSecret];

[self POST:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
#ifdef isSessionManager // check if should use AFHTTPSessionManager or AFHTTPRequestOperationManager
[self POST:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {

[self storeCredentials:responseObject];
success(responseObject);

} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
failure(error);
}];
#else
[self POST:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

[self storeCredentials:responseObject];
success(responseObject);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
failure(error);
}];
#endif

}

- (void)storeCredentials:(id _Nullable)responseObject {
NSString *accessToken = [responseObject objectForKey:@"access_token"];
NSTimeInterval expiration = [[responseObject objectForKey:@"expires_in"] doubleValue];

// store credentials
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

[userDefaults setObject:accessToken forKey:LINKEDIN_TOKEN_KEY];
[userDefaults setDouble:expiration forKey:LINKEDIN_EXPIRATION_KEY];
[userDefaults setDouble:[[NSDate date] timeIntervalSince1970] forKey:LINKEDIN_CREATION_KEY];
[userDefaults synchronize];

success(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
failure(error);
}];

}

- (void)getAuthorizationCode:(void (^)(NSString *))success cancel:(void (^)(void))cancel failure:(void (^)(NSError *))failure {
Expand Down

0 comments on commit ccf8e34

Please sign in to comment.