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 methodin yourfbDidLoginmethod too. Dont forget to change yourApp idin code and also.plistFile.