Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-11-2011, 12:23 AM   #1 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 2
davidatwedu is on a distinguished road
Default Trouble with Facebook Graph API in Tab Bar Application

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?
davidatwedu is offline   Reply With Quote
Old 08-11-2011, 09:23 AM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

have you tried just setting the delegate to your view controller? Then implementing the didLoad/didReceiveResponse methods in that view controller?

i.e.
Code:
- (IBAction) fbTest
{
    NSLog(@"XXViewController fbTest");
    
    [facebookObj requestWithGraphPath:@"me" andDelegate:self];
}
- (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response
{
 
}

- (void)request:(FBRequest *)request didLoad:(id)result
{
  
}
smithdale87 is offline   Reply With Quote
Old 08-11-2011, 10:11 AM   #3 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 2
davidatwedu is on a distinguished road
Default

Quote:
Originally Posted by smithdale87 View Post
have you tried just setting the delegate to your view controller? Then implementing the didLoad/didReceiveResponse methods in that view controller?
I think I've already tried that approach... I had moved all of the helper functions -- didLoad, didReceiveResponse, didFailWithError, fbDidLogin -- to my View Controller's implementation and added the protocols -- <FBSessionDelegate, FBRequestDelegate> -- to its interface, and I kept getting the notorious "10000 Error." That error signifies not having an auth token which tells me that my authorize call isn't getting where it needs to.

The function that seems to be critical to everything working is "handleOpenURL" which is in the AppDelegate's implementation in the examples, but it's not clear to me how I'd change that if it's transplanted into the View Controller instead. Here's how it looks when it's in the AppDelegate:

Code:
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    return [facebook handleOpenURL:url]; 
}
davidatwedu is offline   Reply With Quote
Old 08-14-2011, 02:32 AM   #4 (permalink)
Registered Member
 
SundialSoft's Avatar
 
Join Date: Oct 2010
Location: Scotland
Posts: 176
SundialSoft is on a distinguished road
Default Facebook used from another view controller

Quote:
Originally Posted by davidatwedu View Post
I think I've already tried that approach... I had moved all of the helper functions -- didLoad, didReceiveResponse, didFailWithError, fbDidLogin -- to my View Controller's implementation and added the protocols -- <FBSessionDelegate, FBRequestDelegate> -- to its interface, and I kept getting the notorious "10000 Error." That error signifies not having an auth token which tells me that my authorize call isn't getting where it needs to.

The function that seems to be critical to everything working is "handleOpenURL" which is in the AppDelegate's implementation in the examples, but it's not clear to me how I'd change that if it's transplanted into the View Controller instead. Here's how it looks when it's in the AppDelegate:

Code:
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    return [facebook handleOpenURL:url]; 
}
Hi David, I left the url.scheme in the app delegate and then I pick up facebook in the view controller where I use it with :-

Code:
    yourappnamehereAppDelegate *appDelegate = (yourappnamehereAppDelegate *)[[UIApplication sharedApplication] delegate];

    [appDelegate.facebook requestWithGraphPath:request andDelegate:self];
Hope this helps. It works fine for me. I'm just finishing a small FB part in a bigger app with lots of view controllers. I managed to post a message, upload a photo with a caption, download a news feed, download full sized photos & obtain all the comments on a post.

Cheers

Ian
SundialSoft is offline   Reply With Quote
Reply

Bookmarks

Tags
api, facebook, graph, ios 4.3, iphone

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 395
15 members and 380 guests
7twenty7, blasterbr, chiataytuday, cristofercolmbos, dedeys78, dre, fiftysixty, gmarro, HemiMG, jimmyon122, jonathandeknudt, pungs, raymng, tymex, UMAD
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,669
Threads: 94,121
Posts: 402,903
Top Poster: BrianSlick (7,990)
Welcome to our newest member, dedeys78
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:07 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0