Ive been searching for solution to what seems to be a simple problem for a while now but have had no luck. The login code is as follows.
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [facebook handleOpenURL:url];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *accessToken = [prefs stringForKey:@"facebook-accessToken"];
NSDate *expirationDate = [prefs objectForKey:@"facebook-expirationDate"];
facebook.accessToken = accessToken;
facebook.expirationDate = expirationDate;
if ([facebook isSessionValid]) {
NSLog(@"valid session");
}
else {
NSLog(@"NOT VALID");
[facebook authorize:nil delegate:self];
}
}
- (void)fbDidLogin {
NSLog(@"delegate method");
NSString *accessToken = facebook.accessToken;
NSDate *expirationDate = facebook.expirationDate;
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:accessToken forKey:@"facebook-accessToken"];
[prefs setObject:expirationDate forKey:@"facebook-expirationDate"];
[prefs synchronize];
}
I have a property of Type Facebook in my rootViewController and a button to logout. I pass the facebook instance to the rootViewController and then try to log out as follows.
-(IBAction) logout {
[facebook logout:self];
}
- (void)fbDidLogout {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:nil forKey:@"facebook-accessToken"];
[prefs setObject:nil forKey:@"facebook-expirationDate"];
[prefs synchronize];
}
It kind of works..... Eg. When I do not log out I can restart the app even after closing it down properly (holding icon then pressing x) and the user will be logged in without having to do anything. But when I log out, the next time the app is started the user doesnt get a chance to re-enter the login details, instead I just get a facebook page saying "you have already authorised this app to use facebook"??
Does anyone know if I am going about this the wrong way as its really annoying me now?
Many thanks
Jules