I'm having troubles developing an iOS 5 App with Facebook SDK integration. My App has many ViewsController and for each of them I want to get different data from Facebook using different requests (of course). At the moment I can get data only on the AppDelegate (following the official tutorial) but I need to get data in the view controller. I tried 2 option: 1) Instantiate different Facebook instances on each ViewController -It dosent' work, I don't know why but it seems that
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation;
is never called
2 Instantiate the Facebook instance in the AppDelegate and then use that instance in ViewControllers - It don't works because with this code (found here: How do I persist a Facebook instance in multiple IOS view classes?)
- (void)viewDidLoad
{
[super viewDidLoad];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
Facebook *facebook = [appDelegate facebook];
}
facebook is nil.
Which is the proper way to work?
(I will edit my answer if you give me after that more feedback.)
I haven't used a lot the Facebook SDK but I remember having that problem when I tried.
The - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
implementation should be in your app delegate
If you need to access your Facebook instance in just one viewController (let's say MyFacebooViewController)
You can have something like this
#import <UIKit/UIKit.h>
#import "FBConnect.h"
@interface MyFacebookViewController : UIViewController <FBSessonDelegate, FBRequestDelegate>
@property (nonatomic, strong) Facebook *facebook;
...
@end
And your .m file should contain this
#import "MyFacebookViewController.h"
#import "MyAppDelegate.h"
@implementation MyFacebookViewController
@synthesize facebook = _facebook;
-(Facebook *)facebook
{
if(!_facebook){
_facebook = [[Facebook alloc] initWithAppId:MY_APP_ID andDelegate:self];
}
return _facebook;
}
-(void)viewDidLoad
{
[super viewDidLoad];
((MyAppDelegate *)([[UIApplication sharedApplication] delegate])).facebookViewController = self;
...
}
And of course you need to implement your FBSessionDelegate and FBRequestDelegate methods
In the AppDelegate :
#import <UIKit/UIKit.h>
#import "MyFacebookViewController.h"
@interface MyAppDelegate : UIResponder <UIApplicationDelegate>
...
@property (strong, nonatomic) MyFacebookConnectViewController *facebookViewController;
@end
And it's in the implementation file that you need to put the - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
@implementation MyAppDelegate
@synthesize facebookViewController = _facebookViewController;
...
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [self.facebookViewController.facebook handleOpenURL:url];
}
If you still have problems let me know!