iOS Facebook SDK connect fails on iDevice - facebook

I'm using facebook connect in an iOS app (ARC, running on iOS 5). it was working fine on both simulator and device but since this morning it has stopped working on the device, it does still work on the simulator. On the device i get the fbDinNotLogin method called.
Now, I know the differece is simulator uses mobile facebook and my app uses native iOS facebook app, and I also know that you can tweak the facebook.m file to force the app to do the login elsewhere but I don't want to do that.
loading my previous builds from yesterday (where everything was working fine) also gives me the same results. so same code that ran yesterday doesn't run today. any idea why this happens? below is my delegate .m file:
[EIDT] I get various different behaiviours from the native facebook app. sometimes it opens facebook app, and stays there not coming back to my app. also sometimes it just goes back to my app quickly without allowing facebook app to ask me for my permission. I'm very confused. [/EDIT]
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions {
user *userClass= [[user alloc] init];
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"HasLaunchedOnce"])
{
// app already launched for the first time
}
else
{
// This is the first launch ever
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"HasLaunchedOnce"];
[[NSUserDefaults standardUserDefaults] synchronize];
[userClass CopyDbToDocumentsFolder];
}
facebook = [[Facebook alloc] initWithAppId:#"XXXXXXXXXX" 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];
}
return YES;
}
-(void)fbDidLogout
{
NSLog(#"Facebook loggedOut");
}
- (void)fbDidNotLogin:(BOOL)cancelled
{
NSLog(#"Facebook Connect Failed");
}
- (void)fbDidLogin {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
}
- (void)fbSessionInvalidated
{
NSLog(#"Facebook Session Invalidated");
}
- (void)fbDidExtendToken:(NSString*)accessToken
expiresAt:(NSDate*)expiresAt
{
NSLog(#"Facebook Toekn was extended");
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [self.facebook handleOpenURL:url];
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication: (NSString *)sourceApplication annotation:(id)annotation {
return [self.facebook handleOpenURL:url];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Although the SDK attempts to refresh its access tokens when it makes API calls,
// it's a good practice to refresh the access token also when the app becomes active.
// This gives apps that seldom make api calls a higher chance of having a non expired
// access token.
[[self facebook] extendAccessTokenIfNeeded];
}
#end

Turned out the problem was indeed with facebook and not my app! my developer account had been failing to save changes etc, but it's back to a working now.

Related

Not able to handle the session properly

I'm using Xcode 4.3.1
I use the below code to get the Facebook access token.
In Appdelegate:
facebook=[[Facebook alloc]initWithAppId:appid andDelegate:first];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"]
&& [defaults objectForKey:#"FBExpirationDateKey"])
{
facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
accessToken=facebook.accessToken;
date=facebook.expirationDate;
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [ self.facebook handleOpenURL:url];
}
Facebook SSO (login)
In viewController:
-(IBAction)LoginClick:(id)sender
{
accesstoken=xapp.accessToken;
date=xapp.date;
[self fbDidExtendToken:xapp.accessToken expiresAt:xapp.date]; //For extending tokens
NSLog(#"accesstoken:%#",accesstoken);
if (![xapp.facebook isSessionValid])
{
[xapp.facebook authorize:permissions];
}
else
{
[xapp.facebook authorize: nil];
}
}
Here I can't able to handle the sessions properly.My requirement is for the first time user must login and he should authorize the app.Once he done the app should not ask for authorization.Bt now its asking authorization for each and ever time I'm running the app.How can I solve this.....
Do u save the token to user defaults after login ?
// this is a delegate method of FBSessionDelegate, called after user successfully login
- (void)fbDidLogin
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[xapp.facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[xapp.facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
}
Use the Latest FBGraph API.... Check this link How to share or post by mail, twitter and facebook from the current application? for further queries. This post includes all the latest techniques for FB, Twitter and EMail.

Facebook always reauthorizing on each app launch?

I have recently added Facebook integration to my app, and everything works fine except when the user relaunches the app. Each time the app is restarted, Facebook has to go back through its authorization process. This involves switching out of the app to Safari/Facebook, then back to my app. How can I make Facebook save the info, or be able to get blanket permissions for my app so that I doesn't constantly reauthorize?
Here is my code from the applicationDidFinishLaunchingWithOptions: method:
facebook = [[Facebook alloc] initWithAppId:#"203604286395694" 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];
}
Relevant Facebook delegate methods:
- (void)fbDidLogin {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
}
- (void) requestDialogButtonClicked {
NSMutableDictionary* params =
[NSMutableDictionary dictionaryWithObjectsAndKeys:
#"invites you to check out cool stuff", #"message",
#"Check this out", #"notification_text",
nil];
[facebook dialog:#"apprequests"
andParams:params
andDelegate:self];
}
- (void)dialogDidComplete:(FBDialog *)dialog {
NSLog(#"dialog completed successfully");
}
Are you calling Facebook authorize: somewhere? That's how the fbDidLogin will be triggered. Assuming you are, the behavior indicates that either the token or the date (or both) is nil, or that the date is past expiration (unlikely).
Log the values upon writing and reading the user defaults. My guess is that the authorize either wasn't called, or failed (due to app key or some other problem not shown in the code), leaving the fbDidLogin method uncalled or saving nil values.
That's because you are not asking for permissions when you authorize your app .. "offline_access" provide a long term access token so you will not need to get a new access token every time the user wants to use the Facebook .. so what you need to authorize your app like this
if (![facebook isSessionValid]) {
NSArray *permissions = [[NSArray alloc] initWithObjects:
#"user_likes",
#"read_stream",
#"offline_access"
nil];
[facebook authorize:permissions];
[permissions release];
}
Edit :
Please check the following in your implementation :
1.you need to add the following functions in your application delegate(the Facebook object here is your instance value of FaceBook Class).
// Pre 4.2 support
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
return [facebook handleOpenURL:url];
}
// For 4.2+ support
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [facebook handleOpenURL:url];
}
2.In your info.plist add to URL types > URL Schemes > your Facebook app ID with fb prefix (finally your value will be like this for ex. fb313714***).

Facebook SSO Not Logging In (isSessionValid returns NO all the time)

I am using Facebook Connect with my app. Everything is set up, all the code WAS working. Around yesterday, I could login with my app, then post stuff and other functions. I don't recall making any real changes. Now, as usual, whenever I press login and it goes to a Safari page where I have to confirm that I want to use this app and it says who I am logging in as. If I press ok it goes back to the app. During this whole process, in the app, Facebook never logs in. REMEMBER: JUST YESTERDAY THIS WORKS, I DONT RECALL MAKING ANY CHANGES! Here is how the connection starts:
-(IBAction)connectToFacebook {
[loginButton setImage:[UIImage imageNamed:#"LoginWithFacebookPressed#2x.png"] forState:UIControlStateNormal];
facebook = [[Facebook alloc] initWithAppId:#"387500177929927" 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];
}
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(makeVisibleButtons) userInfo:nil repeats:NO];
[timer setAccessibilityHint:#"timer"];
}
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// JUST SO THAT HANDLE URL CAN GET CALLED
return YES;
}
So I press a button to call this IBAction. The NSTimer is not related, its just for the appearance of some buttons. Now, these two methods never get called (when I log it):
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
ConnectToFB *connect = [[ConnectToFB alloc] init];
return [connect.facebook handleOpenURL:url];
NSLog(#"url");
[connect release];
}
- (void)fbDidLogin {
NSLog(#"log in");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
}
The app id is up to date and my url schemes in info plist is also up to date. What is the problem here?
See this [enter link description here][1]
[1]: Story link flow using SSO facebook - openURL not being called .
You propably don't have facebook instance in AppDelegate

Facebook Connect Logging In Issues

I am using Facebook Connect with my app. Everything is set up, all the code WAS working. Around yesterday, I could login with my app, then post stuff and other functions. I don't recall making any real changes. Now, as usual, whenever I press login and it goes to a Safari page where I have to confirm that I want to use this app and it says who I am logging in as. If I press ok it goes back to the app. During this whole process, in the app, Facebook never logs in. REMEMBER: JUST YESTERDAY THIS WORKS, I DONT RECALL MAKING ANY CHANGES! Here is how the connection starts:
-(IBAction)connectToFacebook {
[loginButton setImage:[UIImage imageNamed:#"LoginWithFacebookPressed#2x.png"] forState:UIControlStateNormal];
facebook = [[Facebook alloc] initWithAppId:#"387500177929927" 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]) {
NSArray *permissions = [[NSArray alloc] initWithObjects:
#"user_likes",
#"read_stream",
nil];
[facebook authorize:permissions];
[permissions release];
}
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(makeVisibleButtons) userInfo:nil repeats:NO];
}
So I press a button to call this IBAction. The NSTimer is not related, its just for the appearance of some buttons. Now, these two methods never get called (when I log it):
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [self.facebook handleOpenURL:url];
NSLog(#"handleOpenURL");
}
- (void)fbDidLogin {
NSLog(#"log in");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
}
I don't understand why. Especially if it was working before. Do you have any ideas?
Facebook API having SSO(Single Sign On). So It will ask the user to login for only one time. This login is valid till that login tokens are expired. Check your code itself that you are storing your token and expiration time in NSUserDefaults. This will help the API to login again. If you want to make sure your app login functionality is working or not, then delete your app from simulator and again run it. It will ask your login credentials for the first time only. Hope you undertand.

Facebook SDK method 'fbDidLogin' never gets called after user authentication (iOS)

I'm having a lot of trouble posting comments to Facebook. Here is how I have got the code setup; in my viewController class I have set up a button that when pressed checks if the user is connected to Facebook. If they're not connected I attempts to make a connection. Here is the code the .h and .m files:
.h file
#import <UIKit/UIKit.h>
#import "FBConnect.h"
#interface Info_Main_ViewController : UIViewController <FBSessionDelegate>
{
Facebook *facebook;
}
#property(nonatomic, retain)Facebook *facebook;
- (IBAction)shareButtonPressed:(id)sender;
- (void)setUpFacebookConnection;
.m file
- (IBAction)shareButtonPressed:(id)sender
{
if (facebook == nil)
{
// Setup Facebook connection
[self setUpFacebookConnection];
}
}
- (void)setUpFacebookConnection
{
facebook = [[Facebook alloc] initWithAppId:#"xxxxxx" 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])
{
NSArray *permissions = [[NSArray alloc] initWithObjects:
#"publish_actions",
nil];
[facebook authorize:permissions];
}
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [facebook handleOpenURL:url];
}
- (void)fbDidLogin
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
}
When I run this code and press the button it opens up safari with a page allowing the user to choose whether to allow the app access to Facebook. Once you accept this it switched back to my app, however the method fbDidLogin never gets called so I can't ever post any messages.
Please can someone help me out, I've spent ages trawling the internet for advice but all of the relevant articles are either outdated or they have the connection to Facebook setup in the app delegate class. Whereas in my situation I only want to user to connect when they choose (i.e. when they press the button).
This method:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [facebook handleOpenURL:url];
}
must be placed in your application delegate. You will have to change the reference to the facebook instance so that it's valid in the app delegate, of course.