Search code examples
iosuiviewcontrollerdelegatesfacebook-ios-sdk

How do you access another UIViewController in appDelegate when login using facebook-ios-sdk?


answer:

Finally I use Notifications that I think is a elegant way to do it.

Thanks you Alan.


I have a facebook login (through facebook-ios-sdk) in my tab based app.

I initial "Facebook" in my appDelegate.

I have a "Connect Facebook" UIButton in settingViewController Tab.

I can login facebook through clicking the "Connect Facebook" UIButton (in settingViewController Tab), and then "- (void)fbDidLogin" (in appDelegate) get called.

Everything is fine.

But my question is how to update "Connect Facebook" UIButton (in settingViewController Tab) to "Facebook connected"?

Can I do something like this in appDelegate:

- (void)fbDidLogin 
{
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:[self.facebook accessToken] forKey:@"FBAccessTokenKey"];
    [userDefaults setObject:[self.facebook expirationDate] forKey:@"FBExpirationDateKey"];
    [userDefaults synchronize];

    if (settingViewController) {
        settingViewController.facebookButton.text = @"facebook connected";
    }
}

Thanks you.


What I have tried:

I put this in settingViewController Tab, but this was not called when the app return from the facebook login screen. So I guess I may have to use delegate in "- (void)fbDidLogin" in appDelegate?

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [self checkAccStatusAndUpdateButton];
}

Solution

  • There are several ways you could deal with this. Here are a couple:

    NSUserDefaults You could store a flag in NSUserDefaults when Facebook is connected. In the viewDidLoad method of SettingViewController you could check that flag and make the appropriate change in your UIButton title.

    Notifications In the fbDidLogin method, you could fire an NSNotification; and your SettingViewController could observe that notification and change the button title. That approach assumes SettingViewController instance exists and can observe the notification.