I have a facebook variable in my ViewController that I want to refer to in my AppDelegate method. How do I do this?
In my AppDelegate I want to put this, since it uses UIApplication and all other refs are in this class:
// For iOS 4.2+ support
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [facebook handleOpenURL:url];
}
The facebook variable is in my ViewController .h file:
#import <UIKit/UIKit.h>
#import "FBConnect.h"
@interface ImportPicViewController : UIViewController <FBSessionDelegate, FBDialogDelegate>{
Facebook *facebook;
}
@property (nonatomic, retain) Facebook *facebook;
@end
and synthesized in the .m file:
@implementation ImportPicViewController
@synthesize facebook;
But it is alloc init in the ViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
facebook = [[Facebook alloc] initWithAppId:@"224207854355440" andDelegate:self];
Any idea?
That doesn’t sound like a good plan — it’ll mean that your app delegate is dependent on this view controller.
A better design might have the app delegate create the instance of Facebook and keep a reference to it in its own ivar. Then, when it creates the view controller, it can pass in a reference to the instance of Facebook. This way, both the app delegate and the view controller are using the same Facebook object, but they’re not trying to access each other’s instance variables.