As a programmer, I know that the hardest part of helping someone out is understanding their problem. I'm in a tough spot because I'm not sure what the "right" question is... I'm relatively new to iOS development, so I'm very much still making my way up the learning curve.
That being said, I'm trying to integrate Facebook's Graph API (via the facebook-ios-sdk libraries) into a Tab Bar Application that I've been developing, and I've hit a snag that has stopped me cold. In all of the examples and tutorials on Facebook's Developer site (and outside tutorial sites), they make the same assumption; the View Controller being a data member of the AppDelegate. This is all well and good except that in the Tab Bar Application template, the View Controllers are all separate from the AppDelegate. I have tried to refactor how I'm communicating with FB several different ways, shifting around delegate protocols and so on, and each and every way just presents new problems.
Currently all of the helper-ish functions that FB's API leverages via the AppDelegate -- didReceiveResponse, didLoad, etc. -- are in my AppDelegate as seen below:
Code:
// XXAppDelegate.m
@implementation XXAppDelegate
@synthesize window=_window;
@synthesize tabBarController=_tabBarController;
@synthesize facebook;
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
NSString *urlStr = [url absoluteString];
NSLog(@"XXAppDelegate handleOpenURL: %@", urlStr);
return [facebook handleOpenURL:url];
}
- (void)fbDidLogin
{
NSLog(@"XXAppDelegate fbDidLogin");
[[NSUserDefaults standardUserDefaults] setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
[[NSUserDefaults standardUserDefaults] setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"XXAppDelegate didReceiveResponse: %@", response);
}
- (void)request:(FBRequest *)request didLoad:(id)result
{
NSLog(@"XXAppDelegate didLoad: %@", result);
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// Add the tab bar controller's current view as a subview of the window
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
facebook = [[Facebook alloc] initWithAppId:@"XXXXXXXXXXXXXX"];
return YES;
}
The code that handles making the authorize call and making the "requestWithGraphPath" are defined/executed in the View Controller which I ultimately want to use to display the data I get back from FB. Here's the relevant code from that class below:
Code:
// XXViewController.m
- (void)viewDidLoad
{
NSLog(@"XXViewController viewDidLoad");
[super viewDidLoad];
appDelegate = (XXAppDelegate *)[[UIApplication sharedApplication] delegate];
facebookObj = appDelegate.facebook;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"] && [defaults objectForKey:@"FBExpirationDateKey"])
{
facebookObj.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
facebookObj.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
}
if (![facebookObj isSessionValid])
{
NSLog(@"XXViewController isSessionValid = false");
NSArray *facebookPerms = [[NSArray alloc] initWithObjects:@"read_stream", @"publish_stream", @"offline_access",nil];
[facebookObj authorize:facebookPerms delegate:appDelegate];
[facebookPerms release];
} else {
NSLog(@"XXViewController isSessionValid = true");
[appDelegate fbDidLogin];
}
}
- (IBAction) fbTest
{
NSLog(@"XXViewController fbTest");
[facebookObj requestWithGraphPath:@"me" andDelegate:appDelegate];
}
If I had to summarize what I'm looking to do, I want to get the data from "didLoad" in my AppDelegate and somehow send it back to the View Controller so I can use it (i.e. populate a UITableView).
Can anyone with a bit more Obj-C experience offer me some hints or suggestions?