Skip to content

Instantly share code, notes, and snippets.

@ekoneil
Created October 18, 2012 15:02
Show Gist options
  • Save ekoneil/3912400 to your computer and use it in GitHub Desktop.
Save ekoneil/3912400 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];
}];
}
}
}
@ettore
Copy link

ettore commented Jan 14, 2013

There are a few problems with this gist:

line 27: what object is self? I assumed it's the app delegate, but clearly there's no parseURLParams: method in that class. Perhaps it's of the Facebook class...

line 69: again, not sure how am I supposed to invoke sessionStateChanged:state:error:. The only class that defines that method is FBUserSettingsViewController...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment