Skip to content

Commit

Permalink
Merge pull request #9 from tijoinc/ios9-compatibility
Browse files Browse the repository at this point in the history
iOS 9 Compatibility
  • Loading branch information
Brian Dorfman committed Jun 16, 2015
2 parents dce4c4a + dd2cb26 commit 0a354fb
Showing 1 changed file with 44 additions and 11 deletions.
55 changes: 44 additions & 11 deletions FSOAuth.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,7 @@ + (FSOAuthStatusCode)authorizeUserUsingClientId:(NSString *)clientID
return FSOAuthStatusErrorFoursquareNotInstalled;
}

NSString *urlEncodedCallbackString = (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)callbackURIString,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8);
NSString *urlEncodedCallbackString = [self urlEncodedStringForString:callbackURIString];

NSURL *authURL = [NSURL URLWithString:[NSString stringWithFormat:@"foursquareauth://authorize?client_id=%@&v=%@&redirect_uri=%@", clientID, kFoursquareOAuthRequiredVersion, urlEncodedCallbackString]];

Expand Down Expand Up @@ -131,15 +127,11 @@ + (void)requestAccessTokenForCode:(NSString *)accessCode clientId:(NSString *)cl
&& [clientSecret length] > 0
&& completionBlock) {

NSString *urlEncodedCallbackString = (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)callbackURIString,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8);
NSString *urlEncodedCallbackString = [self urlEncodedStringForString:callbackURIString];

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://foursquare.com/oauth2/access_token?client_id=%@&client_secret=%@&grant_type=authorization_code&redirect_uri=%@&code=%@", clientID, clientSecret, urlEncodedCallbackString, accessCode]]];

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
[self sendAsynchronousRequest:request completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (data && [[response MIMEType] isEqualToString:@"application/json"]) {
id jsonObj = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
if ([jsonObj isKindOfClass:[NSDictionary class]]) {
Expand Down Expand Up @@ -198,4 +190,45 @@ + (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewContr
[viewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}
#endif

+ (NSString *)urlEncodedStringForString:(NSString *)string {
NSString *urlEncodedString = nil;
// Introduced in iOS 7, -stringByAddingPercentEncodingWithAllowedCharacters: replaces CFURLCreateStringByAddingPercentEscapes (deprecated in iOS 9).
if ([NSString instancesRespondToSelector:@selector(stringByAddingPercentEncodingWithAllowedCharacters:)]) {
urlEncodedString = [string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
}
else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
urlEncodedString = (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)string,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8);
#pragma clang diagnostic pop
}

return urlEncodedString;
}

+ (void)sendAsynchronousRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURLResponse *response, NSData *data, NSError *error))completionHandler
{
// Introduced in iOS 7, NSURLSession replaces NSURLConnection (deprecated in iOS 9).
if ([NSURLSession class]) {
[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (completionHandler) {
dispatch_async(dispatch_get_main_queue(), ^{
completionHandler(response, data, error);
});
}
}] resume];
}
else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:completionHandler];
#pragma clang diagnostic pop
}
}

@end

0 comments on commit 0a354fb

Please sign in to comment.