I want to create a login button. When I click it the facebook launches, I accept my application, the Facebook application closes and my application reloads. My problem is that the second time my application starts, after accepting on Facebook, I cannot find where to disable the login button.
I wrote this code:
-(IBAction) login:(id)sender;
{
AutoAppDelegate *delegate = (AutoAppDelegate *) [[UIApplication sharedApplication] delegate];
// Check and retrieve authorization information
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"]
&& [defaults objectForKey:@"FBExpirationDateKey"]) {
[delegate facebook].accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
[delegate facebook].expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
}
if (![[delegate facebook] isSessionValid]) {
[delegate facebook].sessionDelegate = self;
NSArray *permissions = [[NSArray alloc] initWithObjects:
@"user_likes",
@"read_stream",
nil];
[[delegate facebook] authorize:permissions];
} else {
logIn.hidden=YES;
lbl.text=@"";
lbl.text=@"Connected";
[self showLoggedIn];
}
}
- (void) showLoggedIn {
NSLog(@"11 Logged In ");
lbl.text=@"";
lbl.text=@"Connected";
}
- (void) showLoggedOut {
NSLog(@"11 Not Logged In ");
lbl.text=@"";
lbl.text=@"Not Connected";
}
- (void) viewWillAppear:(BOOL)animated
{
//[self.navigationController setNavigationBarHidden:YES animated:animated];
[super viewWillAppear:animated];
AutoAppDelegate *delegate = (AutoAppDelegate *) [[UIApplication sharedApplication] delegate];
// Check and retrieve authorization information
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"]
&& [defaults objectForKey:@"FBExpirationDateKey"]) {
[delegate facebook].accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
[delegate facebook].expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
}
if (![[delegate facebook] isSessionValid]) {
// [self showLoggedOut];
NSLog(@"Not Connected");
} else {
// [self showLoggedIn];
NSLog(@"Connected");
}
}
The viewWillAppear works the first time, but when I give permissions in the Facebook application, my application restarts but not viewWillAppear method!
You need to put the code in your application delegate (
applicationWillEnterForeground), as it’s the only thing to get notified automatically when the app is re-opened (your view is not necessarily re-drawn).