Popup Facebook feed iOS - objective-c

I am trying to implement the Facebook sdk into my app but am having issue displaying the write to wall/feed popup. I Can log in successfully into Facebook from my app. First it takes me to the native Facebook app, once I authorize it, it takes me back to my default app and nothing happens. The dialog box does not appear and all I see is my empty view I created. This is my code, if anyone knows why this is not working I would appreciate the help.
Thanks
fbViewController.h
#import <UIKit/UIKit.h>
#import "FBConnect.h"
#interface fbViewController : UIViewController <UIApplicationDelegate,FBSessionDelegate,FBDialogDelegate>
{
Facebook *facebook;
}
#property (nonatomic, retain) Facebook *facebook;
#end
fbViewController.m
#import "fbViewController.h"
#define kAppId #"xxxxxxx"
- (void)viewDidLoad
{
[super viewDidLoad];
facebook = [[Facebook alloc] initWithAppId:#"xxxxxxxxxxx" andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"]
&& [defaults objectForKey:#"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
}
if (![facebook isSessionValid]) {
[facebook authorize:nil];
}
}
- (void)fbDidLogin {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
kAppId, #"app_id",
#"http://developers.facebook.com/docs/reference/dialogs/", #"link",
#"http://fbrell.com/f8.jpg", #"picture",
#"Facebook Dialogs", #"name",
#"Reference Documentation", #"caption",
#"Using Dialogs to interact with users.", #"description",
nil];
[facebook dialog:#"feed" andParams:params andDelegate:self];
}
myAppDelegate.m
// Pre iOS 4.2 support
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [facebook handleOpenUrl:url];
}
// For iOS 4.2+ support
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [facebook handleOpenURL:url];
}

I struggled with this for a while. Here's my SO question that I posted while trying to get it sorted out. It might be worth it to implement the test that dmirkitanov suggests in his answer. It's out of the Hackbook app (the one that you get with the facebook-ios-sdk).
Long story short, what I had to do was delete my app from the Facebook developer site, delete it from the iTunes store, submit it all over again from square one on both, and use the new app ID info etc in my code. I think there was some issue with how it was registered on Facebook's end.

It seems, AppId is incorrect. Try to use another app Id and check
1) Info -> FacebookAppID is same this app id in your code

Related

Getting Full Name from Facebook from a UIViewController

Hello so I am just starting out with the Facebook iOS SDK and I can sign in to Facebook easy but I am having problems figuring out how to get the users full name and displaying it in a UILabel. Also I am doing this from a UIViewController and so far everything I have seen on the internet has taken place in the app delegate, however, I don't want it to be done from the delegate, it just is not the right place to do this i want it in a UIViewController so when a button is pressed you can sign in to Facebook. The Facebook SDK just seems complicated right now as its my first time using it.
EDIT:
okay heres some of my code.
This is in the view controller:
-(void)createfbInstance {
Facebook* fb = [[Facebook alloc] initWithAppId:FB_APP_ID andDelegate:self];
self.facebook = fb;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"]
&& [defaults objectForKey:#"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
}
if (![facebook isSessionValid]) {
NSArray* permissions = [[NSArray alloc] initWithObjects:#"user_about_me", nil];
[facebook authorize:permissions];
}
}
So I just set it up like the Facebook tutorial
Then I try to send a request with graph path:
- (void)fbDidLogin {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
FBAppDelegate* appDelegate = (FBAppDelegate*)[[UIApplication sharedApplication] delegate];
[appDelegate.facebook requestWithGraphPath:#"name" andDelegate:self];
}
Then try and receive the request:
- (void)request:(FBRequest *)request didLoad:(id)result {
}
Sorry I got frustrated and deleted the code out of that method.
and then in the delegate I have the Facebook property
#property (nonatomic, retain) Facebook* Facebook;
There is other code in the file but this seemed like the most important code.
In whatever class you need to access the Facebook API from you need to include the following files (in the header file):
#import "FBConnect.h"
#import "YOUR_APP_DELEGATE.h"
and also make your class implement the FBRequestDelegate Delegate.
Then in order to use it you simply do the following:
YOURAPPDELEGATE *appDelegate = (YOURAPPDELEGATE*)[[UIApplication sharedApplication] delegate];
With that you can now access the Facebook property you have in your delegate allowing you to access the API.
Now you need to add the method - (void)request:(FBRequest *)request didLoad:(id)result in your view controller so you can receive the response.
So now you can call
[appDelegate.facebook requestWithGraphPath:#"APICALLHERE" andDelegate:self];
and you will now receive the response in the request:didLoad: method which you can then do whatever you want with.

popup facebook feed dialog (for wall posting) in ios xcode 4.3 (storyboard)

I am making an app in which I need to integrate a facebook share (feed) dialog. I read all stuff on https://developers.facebook.com/docs/mobile/ios/build/ , and I was able to load facebook share (feed) dialog when the app finished launching. But my problem is that I am using xcode 4.3 i.e storyboard and I want to load facebook feed dialog on button action.
I made another view controller and paste all the stuff of AppDelegate's function
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
in its -(void) viewDidload and was able to load "login page" and then my "facebook app" page.
After that when I allow permission to my facebook app it does not show "feed dialog"
Here is my viewController.h file
#import "FBConnect.h"
#interface ViewController : UIViewController<FBSessionDelegate,FBDialogDelegate>
{
Facebook *facebook;
}
#property (nonatomic, retain) Facebook *facebook;
#end
-Here is my ViewController.m file
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize facebook=_facebook;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
facebook = [[Facebook alloc] initWithAppId:#"284727404938450" andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"]
&& [defaults objectForKey:#"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
}
if (![facebook isSessionValid]) {
[facebook authorize:nil];
}
}
- (void)fbDidLogin {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
[facebook dialog:#"feed" andDelegate:self];
}
but here are two methods which are used in AppDelegate file
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [ facebook] handleOpenURL:url];
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [ facebook] handleOpenURL:url];
}
I know that in xib these methods should be:
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [[_ViewController facebook] handleOpenURL:url];
}
// For iOS 4.2+ support
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [[_ViewController facebook] handleOpenURL:url];
}
These method are responsible for popping up a feed dialog for the current "facebook" instance (Facebook *facebook).
My problem is that in the storyboard we dont have a "window" and view controller intances so my guess is that these two methods are not being called for my current viewController's "facebook" instance. How can I call these two functions for my "facebook" instance, where my viewController is not an instance of AppDelegate file?
Please help me out.
You can pass a reference of your viewcontroller in the app delegate's _viewcontroller before using Facebook (or calling these delegate functions).
Your view controller's viewdidload() will look like as below:
-(void)viewDidLoad{
[[UIApplication sharedApplication] delegate]._viewComtroller = self; // pass reference to the appdalegate
// Other code (or call the facebook function here)
}
It will be working
Be careful because you are defining an ivar named "facebook" and a property named "facebook" that creates another ivar named "_facebook". Then on your code your are working with the ivar "facebook", so when you call [_ViewController facebook] you are accessing the property (ivar _facebook) and not the ivar "facebook" that you initialized.
I don't think this is the answer you were looking for but it seemed important to point out.
Good luck!
Looks like you're missing a few Facebook related delegates and that you're not putting them all in your main app delegate as the online tutorial instructs:
See https://developers.facebook.com/docs/mobile/ios/build/
Well one solution could implement the Facebook delegate methods in the AppDelegate, then in your viewController in the viewDidLoad method, or the method you prefer, use
AppDelegate *appDelegate= (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.facebook authorize:permissions];
where permissions is an array with the permissions you need. That should work and the methods
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [ facebook] handleOpenURL:url];
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [ facebook] handleOpenURL:url];
}
are performed.

Facebook login not properly being recognized once returning to app

I'm having trouble implementing the Facebook SDK in an iOS project. I am trying to call it on a specific section of my app through a UIActionSheet. I am able to login to Facebook, but once redirected back to my app, the app does not register that it is logged in and can't execute any of its sharing functions.
I have followed the Facebook iOS Tutorial at https://developers.facebook.com/docs/mobile/ios/build/ to include adding "fbMY_APP_ID" in the project info but can't figure out why it won't recognize that I am logged in.
Here is the relevant header and method I am using. For convenience, I've removed items unrelated to Facebook implementation:
//Header
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#import "MWFeedItem.h"
#import "FBConnect.h"
#interface DetailTableViewController : UITableViewController <UIAlertViewDelegate, UIActionSheetDelegate, UIApplicationDelegate, FBSessionDelegate>{
Facebook *facebook;
BOOL login;
}
#property (nonatomic, strong) Facebook *facebook;
#end
And the method:
// Method
#import "DetailTableViewController.h"
#import "NSString+HTML.h"
#import <Twitter/Twitter.h>
#implementation DetailTableViewController
#synthesize facebook;
//Navigation button on detail view does THIS:
- (void)navAction {
NSLog(#"Action sheet activated.");
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:#"Like this story?"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:#"Email link"
otherButtonTitles:#"Share on Facebook", #"Share on Twitter", #"Open in Safari", nil];
[actionSheet showInView:[self.view window]];
}
-(void)actionSheet: (UIActionSheet *) actionSheet willDismissWithButtonIndex:(NSInteger) buttonIndex{
///////THIS SECTION DEFINES THE EMAIL SHARE FUNCTION/////
if (buttonIndex == 0) {
// MFMailComposeViewController implementation
}
///////THIS SECTION DEFINES THE FACEBOOK SHARE FUNCTION/////
else {
if (buttonIndex == 1) {
if (login == NO){
NSLog(#"No login detected");
UIAlertView *infoAlert = [[UIAlertView alloc]
initWithTitle:#"Login necessary"
message:#"You must first login to your Facebook account to use this feature."
delegate:self
cancelButtonTitle:#"Login"
otherButtonTitles:#"Cancel", nil];
[infoAlert show];
}else {
if (login == YES){
NSLog(#"Login detected.");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:#"FBExpirationKey"];
[defaults synchronize];
//APPLY LOGIN ACTION HERE. Waiting until I can make the system run the NSLog above. So far, the login is not being correctly recorded.
}
}
}
///////THIS SECTION DEFINES THE TWITTER SHARE FUNCTION/////
else {
if (buttonIndex == 2) {
//Twitter implementation here
}
///////THIS SECTION DEFINES THE SAFARI FUNCTION/////
else{
if (buttonIndex == 3){
//Safari implementation here
}
}
}
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
// CALL TO FACEBOOK HERE
facebook = [[Facebook alloc] initWithAppId:#"MY_APP_ID" andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"] && [defaults objectForKey:#"FBExpirationDateKey"]){
facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
}
if (![facebook isSessionValid]){
NSLog(#"Reached isSessionValid evaluation.");
[facebook authorize:nil];
}
}
else if (buttonIndex == 1)
{
NSLog(#"Pressed cancel button");
}
}
// THIS METHOD SUPPORTS IOS PRE 4.2
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
return [facebook handleOpenURL:url];
}
// THIS METHOD SUPPORTS IOS AFTER 4.2
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
return [facebook handleOpenURL:url];
}
////////////////////////////////////////////
- (void)fbDidNotLogin:(BOOL)cancelled{
}
- (void)fbDidExtendToken:(NSString*)accessToken
expiresAt:(NSDate*)expiresAt{
}
- (void)fbDidLogout{
}
- (void)fbSessionInvalidated{
}
- (void) fbDidLogin {
NSLog(#"Logged in with Facebook credentials");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:#"FBExpirationKey"];
[defaults synchronize];
//APPLY LOGIN ACTION HERE. Waiting until I can make the system run the NSLog above. So far, the login is not being correctly recorded.
}
Additional info:
I've also tried adding the openURL portions to the main app delegate file but had the same results.
Try this code in your project. I had a same issues which you have. Now my code is working perfect. Call your sharing method in your fbDidLogin method too. Dont forget to change your App id in code and also .plist File.
-(void)buttonPressed:(id)sender{
facebook = [[Facebook alloc] initWithAppId:#"YOUR_APP_ID" andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"] && [defaults objectForKey:#"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
}
if (![facebook isSessionValid]) {
[facebook authorize:nil];
}else{
[self postWall];
}
// Pre 4.2 support
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [facebook handleOpenURL:url];
}
- (void)fbDidLogin {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
[self postWall];
}
-(void)postWall{
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"https://developers.facebook.com/docs/reference/dialogs/",#"link",
#"http://fbrell.com/f8.jpg",#"picture",
#"Facebook Dialogs",#"name",
#"Reference Documentation",#"caption",
#"Using Dialogs to interact with users.",#"description",
#"Facebook Dialogs are so easy!",#"message",
nil];
[[self facebook] dialog:#"feed" andParams:params andDelegate:self];
}
This could be related to some deprecated Facebook API methods. See this post for possible help as well: related question

Facebook SDK Singleton in xcode auto login before post?

I have had the Facebook SDK working in the APPDelegate as instructed by the Facebook tutorial, however I am trying to put in into a singleton method. Every tutorial i've found seems to be for an older version of the SDK, but I have managed success with this one http://indiedevstories.com/2011/08/30/a-facebook-reusable-class/
I'm having 2 problems, the first is posted here this is the second:
I want a button to post to Facebook, but if the user isn't logged in then they need to login first then post (without having to press a separate login button first).
I can log in fine, and I can post fine, however I can't do both together.
If not logged in, the post code shows the login screen, but doesn't goto the post screen after logging in. you have to press post again.
If you try to login but are already logged in, then nothing happens.
So on the button I use code to login then post, as the login is just skipped if already logged in.
The problem i'm having, is the post code is being run instantly after the login code, so before the user has had a chance to login. This results in 2 popups being opened (1 login and 1 post which displays login as not logged in yet).
How can I get my code to wait for the user to login before moving onto the next line in the code to post?
FacebookHelper.h
#interface FacebookHelper : NSObject <FBSessionDelegate, FBRequestDelegate, FBDialogDelegate, FBLoginDialogDelegate> {
Facebook *_facebook;
NSArray *_permissions;
}
#property (nonatomic,strong) Facebook *facebook;
+(FacebookHelper *) sharedInstance;
+(void)fbDidLogin;
#pragma mark - Public Methods
-(BOOL) isFBSessionValid;
-(void) login;
-(void) logout;
-(void) postToWallWithDialogNewHighscore:(int)highscore;
#end
FacebookHelper.m
#implementation FacebookHelper
#synthesize facebook;
#pragma mark -
#pragma mark Singleton Variables
static FacebookHelper *singletonDelegate = nil;
#pragma mark -
#pragma mark Singleton Methods
+(FacebookHelper *)sharedInstance
{
#synchronized(self) {
if (singletonDelegate == nil) {
singletonDelegate = [[self alloc] init]; // Assignment not done here
}
}
return singletonDelegate;
}
-(id)init
{
self = [super init];
if (self) {
_facebook = [[Facebook alloc] initWithAppId:kAppId andDelegate:self];
// Restore previous session
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"] && [defaults objectForKey:#"FBExpirationDateKey"]) {
_facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
_facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
}
//
}
return self;
}
+(id)allocWithZone:(NSZone *)zone
{
#synchronized(self) {
if (singletonDelegate == nil) {
singletonDelegate = [super allocWithZone:zone];
// assignment and return on first allocation
return singletonDelegate;
}
}
//on subsequent allocation attemps, return nil
return nil;
}
-(id)copyWithZone:(NSZone *)zone
{
return self;
}
#pragma mark - Facebook Delegate Methods
-(void)fbDidLogin
{
NSLog(#"fbDidLogin");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[_facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[_facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
}
-(void)fbDidLogout
{
NSLog(#"fbDidLogout");
// Remove saved authorisation information if it exists
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"]) {
[defaults removeObjectForKey:#"FBAccessTokenKey"];
[defaults removeObjectForKey:#"FBExpirationDateKey"];
[defaults synchronize];
}
}
#pragma mark - Public Methods
-(NSMutableDictionary *) buildPostParamsWithHighScore:(int)highscore
{
NSString *customMessage = [NSString stringWithFormat:kCustomMessage, highscore, kAppName];
NSString *postName = kAppName;
NSString *serverLink = [NSString stringWithFormat:kServerLink];
NSString *imageSrc = kImageScr;
//Final params build
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
//#"message", #"message",
imageSrc, #"picture",
serverLink, #"link",
postName, #"name",
#" ", #"caption",
customMessage, #"description",
nil];
return params;
}
-(BOOL) isFBSessionValid
{
// Check if there is a valid session
//_facebook = [[Facebook alloc] initWithAppId:kAppId andDelegate:self];
_facebook.accessToken = [[NSUserDefaults standardUserDefaults] objectForKey:#"FBAccessTokenKey"];
_facebook.expirationDate = [[NSUserDefaults standardUserDefaults] objectForKey:#"FBExpirationDateKey"];
NSLog(#"accessToken=%# expirationDaate=%#",_facebook.accessToken,_facebook.expirationDate);
if (![_facebook isSessionValid]) {
NSLog(#"FacebookHelper isFBSessionValid = NO");
return NO;
} else {
NSLog(#"FacebookHelper isFBSessionValid = YES");
return YES;
}
return NO;
}
-(void) login
{
NSLog(#"FacebookHelper login");
_permissions = [NSArray arrayWithObjects:#"publish_stream", nil]; //#"read_stream", #"offline_access"
[_facebook authorize:_permissions];
}
-(void) logout
{
[_facebook logout];
}
-(void) postToWallWithDialogNewHighscore:(int)highscore
{
NSMutableDictionary *params = [self buildPostParamsWithHighScore:highscore];
NSLog(#"Post Feed");
[_facebook dialog:#"feed" andParams:params andDelegate:self];
}
#end
Button Action:
- (IBAction)facebookTest:(id)sender {
[[FacebookHelper sharedInstance] login];
[[FacebookHelper sharedInstance] postToWallWithDialogNewHighscore:123];
}
I have updated the singleton class to post scores to Facebook Wall. Check out the new version: http://indiedevstories.com/2012/04/11/facebookscorer-post-highscores-to-users-facebook-wall/
This new version handles correctly the internal state when authorization and login is required.
HTH
What you need to do is have your postToWallWithDialogNewHighscore call the login, if necessary, and then wait to hear back from the FBSession delegate. Then you need to setup your facebook fbDidLogin delegate so that when it is done it sends back a message. There are a number of ways to do this (a post-login selector, NSNotificationCenter, etc). Something like this should work (note I am assuming an ivar of NSMutableDictionary *postParams for convenience):
-(void)doPost {
NSLog(#"Post Feed");
[_facebook dialog:#"feed" andParams:postParams andDelegate:self];
}
-(void)postToWallWithDialogNewHighscore:(int)highscore
{
postParams = [self buildPostParamsWithHighScore:highscore];
if (_facebook.isSessionValid) {
[self doPost];
} else {
//register an observer for FB login messages
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(postToWallObserver:) name:#"FBLoginComplete" object:nil];
[self login];
}
}
-(void)fbDidLogin {
NSLog(#"token granted until %#", [_facebook expirationDate]);
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[_facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[_facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
[[NSNotificationCenter defaultCenter] postNotificationName:#"FBLoginComplete" object:nil];
}
-(void)fbDidNotLogin:(BOOL)cancelled {
//do nothing as they didn't login
}
-(void) postToWallObserver:(NSNotification *) notification {
if ([[notification name] isEqualToString:#"FBLoginComplete"]) {
if ([_facebook isSessionValid]) {
[self doPost];
}
}
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
Note: I have not compiled this code so there may be typos, etc.

Facebook iOS Login - Delegate methods not being called

I'm trying to implement the Facebook login for one of my applications and the delegate methods - fbDidLogin: / application: handleOpenURL: / fbDidNotLogin: aren't being called. Here is the code that I'm using. Any help is much appreciated!
- (void)viewDidLoad
{
...
facebook = [[Facebook alloc] initWithAppId:kFacebookAppId];
}
-(IBAction) loginWithFacebook:(id)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"]
&& [defaults objectForKey:#"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
}
if (![facebook isSessionValid]) {
NSArray* permissions = [[NSArray arrayWithObjects:
#"email", nil] retain];
[facebook authorize:permissions delegate:self];
}
}
#pragma mark - Facebook delegate
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [facebook handleOpenURL:url];
}
- (void)fbDidLogin {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
}
-(void) fbDidNotLogin:(BOOL)cancelled {
}
-(void) fbDidLogout {
}
I also have the Facebook App ID in the info.plist file under the appropriate entry. I have break points in the loginWithFacebook: method and they hit successfully. the [facebook authorize:] method gets called as well.
Thanks,
Teja.
Oops, I've just realized the application: handleOpenURL: method is an iOS application delegate method. I had it in one of my UIViewControllers. Moved it to the right place and it works.
application: handleOpenURL: calls than also it doesn't calling delegate methods. what should be problem?