Skip to content

Instantly share code, notes, and snippets.

@ekoneil
Created October 18, 2012 15:01
Show Gist options
  • Save ekoneil/3912394 to your computer and use it in GitHub Desktop.
Save ekoneil/3912394 to your computer and use it in GitHub Desktop.
Creating an FBSession from handleOpenUrl. Workaround for an SDK bug.
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
// Save the incoming URL to test deep links later.
self.openedURL = url;
// Work around for app link from FB with valid info. If the session is closed, set the valid info (if any) in the cache
if (FBSession.activeSession.state == FBSessionStateCreated || FBSession.activeSession.state == FBSessionStateClosed){
[self handleOpenURLPre:url];
}
// We need to handle URLs by passing them to FBSession in order for SSO authentication to work.
return [FBSession.activeSession handleOpenURL:url];
}
- (void) handleOpenURLPre:(NSURL *) url {
// Parse the URL
NSString *query = [url fragment];
if (!query) {
query = [self.openedURL query];
}
NSDictionary *params = [self parseURLParams:query];
// Look for a valid access token
if ([params objectForKey:@"access_token"]) {
NSString *accessToken = [params objectForKey:@"access_token"];
NSString *expires_in = [params objectForKey:@"expires_in"];
// Determine the expiration date
NSDate *expirationDate = nil;
if (expires_in != nil) {
int expValue = [expires_in intValue];
if (expValue != 0) {
expirationDate = [NSDate dateWithTimeIntervalSinceNow:expValue];
}
}
if (!expirationDate) {
expirationDate = [NSDate distantFuture];
}
NSDate *nowDate = [NSDate date];
// Check expiration date is in the future
if (NSOrderedDescending == [expirationDate compare:nowDate]) {
// Cache the token
NSDictionary *tokenInfo = [NSDictionary dictionaryWithObjectsAndKeys:
accessToken, FBTokenInformationTokenKey,
expirationDate, FBTokenInformationExpirationDateKey,
nowDate, FBTokenInformationRefreshDateKey,
nil];
FBSessionTokenCachingStrategy *tokenCachingStrategy = [FBSessionTokenCachingStrategy defaultInstance];
[tokenCachingStrategy cacheTokenInformation:tokenInfo];
// Now open the session and the cached token should
// be picked up, open with nil permissions because
// what you send is checked against any cached permissions
// to determine token validity.
[FBSession openActiveSessionWithReadPermissions:nil
allowLoginUI:NO
completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
[self sessionStateChanged:session
state:state
error:error];
}];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment