Facebook delegates are not calling after authentication: cocos2d - ios

He guys, I am doing a photo share in my cocos2d application with Facebook SDK. I've a problem that: the login view with status Not Logged In is remaining even after authentication. I'm attaching the images:
Problem: After clicking the Skip or OK button of the Authentication page, the status has to be changed as Login. But it isn't. No Facebook delegates are getting called after Auth(When I enter the page first time, loginViewShowingLoggedOutUser delegate was get called. After auth, nothing is get called.).
What I've done already:
Downloaded the Facebook SDK, attached to the project.
Added FacebookAppID,FacebookDisplayName and URL Schemes in info.plist.
Tried the integration with my cocos2d app.
Added <FBLoginViewDelegate> in .h.
Added loginview.delegate = self in .m.
I'm also uploading the screenshot of what I've done in my info.plist:
I am creating the login view as:
// Create Login View so that the app will be granted "status_update" permission.
FBLoginView *loginview = [[FBLoginView alloc] init];
loginview.frame = CGRectOffset(loginview.frame, 5, 5);
#ifdef __IPHONE_7_0
#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_7_0
if ([self respondsToSelector:#selector(setEdgesForExtendedLayout:)]) {
loginview.frame = CGRectOffset(loginview.frame, 5, 25);
}
#endif
#endif
#endif
loginview.delegate = self;
[[[CCDirector sharedDirector]view]addSubview:loginview];
[loginview sizeToFit];
What I found:
The following method in appdelegate is not get called:
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
// attempt to extract a token from the url
return [FBAppCall handleOpenURL:url
sourceApplication:sourceApplication
fallbackHandler:^(FBAppCall *call) {
NSLog(#"In fallback handler");
}];
}
Further info:
Xcode Version 5.0.1
iOS Version 7.0
facebook-ios-sdk-3.9
Any help ASAP is appreciable.
Thanks.

Sorry guys.... #elio.d: and thank you very much for your valuable suggestion . That was my mistake with coding. I have pasted the following method, just above #implementation AppController. What an idiot I am. This just wasted one of my whole day... :
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
}
This just saved me. Hope this might be helpful for some others like me. :)

You have to add an url scheme in your app to make the facebook app / website call your app back after login or authorization link to the documentation, this is the reason why your app is not called

Related

"You have already authorized yourApp" when logging in using FBSDK 4.7.1

I was following Facebook's own tutorial for FB login in iOS (Objective-C) but every time I log in - after the initial permission authorization screen - I'm getting the infamous "You have already authorized this app" webview.
I've read a ton of posts but I haven't been able to sort it out, hence the (re)post. I find this rather odd because the app has absolutely nothing except the boilerplate login code. This behaviour happens in both Simulator and real devices.
This app is for iOS 9.0 and I am using FBSDK 4.7.1 (installed via CocoaPods):
pod 'FBSDKLoginKit', '~> 4.6'
The code itself is pretty boilerplate stuff, here's my AppDelegate.m:
#import "AppDelegate.h"
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#interface AppDelegate ()
#end
#implementation AppDelegate
- (void)applicationDidBecomeActive:(UIApplication *)application {
[FBSDKAppEvents activateApp];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[FBSDKApplicationDelegate sharedInstance] application:application
didFinishLaunchingWithOptions:launchOptions];
return YES;
}
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
return [[FBSDKApplicationDelegate sharedInstance] application:application
openURL:url
sourceApplication:sourceApplication
annotation:annotation];
}
#end
and my ViewController.m:
#import "ViewController.h"
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
FBSDKLoginButton *loginButton = [[FBSDKLoginButton alloc] init];
loginButton.readPermissions = #[#"public_profile", #"email", #"user_friends"];
[loginButton setLoginBehavior:FBSDKLoginBehaviorSystemAccount]; // No difference
loginButton.center = self.view.center;
[self.view addSubview:loginButton];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
And a screenshot of the Info.plist:
Finally, I'm also getting these two errors in the console:
2015-11-24 11:50:42.855 lixo[26941:389756] -canOpenURL: failed for URL: "fbauth2:/" - error: "(null)"
2015-11-24 11:50:42.860 lixo[26941:389756] -canOpenURL: failed for URL: "fbauth2:/" - error: "(null)"
Although from what I've read this can be safely ignored if LSApplicationQueriesSchemes is set in the Info.plist.
Any ideas how to avoid this?
Instead of triggering a login on every launch, you can check the status of the access token. If it's not expired, you skip login, and just start using the API. The following method can be used to check if the token is expired:
+(BOOL) accessTokenIsLocalyValid
{
return [FBSDKAccessToken currentAccessToken] && [[NSDate date] compare:[FBSDKAccessToken currentAccessToken].expirationDate] == NSOrderedAscending;
}
If you have a valid token, you can refresh it using [FBSDKAccessToken refreshCurrentAccessToken:].
Check if there is a current session before logging in with FBSDKLoginManager.
if ([FBSDKAccessToken currentAccessToken])
{
NSLog(#"Token is available : %#",[[FBSDKAccessToken currentAccessToken]tokenString]);
// Now get details using graphpath.
} else {
// login with permissions using FBSDKLoginManager and get details using graphpath in completion
}
This is the default behaviour!
Facebook SDK allows you to login to facebook from Facebook App (if it is installed in your phone), OR else it will manage the login from Safari.
And when Facebook login is used by your application from Safari, this is how you have to accept permission everytime.
Another behaviour is that If your iphone doesnt have Facebook App, and you use Facebook SDK login process from your application, it will use Safari; you wont be able to logout even, as Safari saves your credentials in cookies and there is no way by which you can clear safari cookies from your application.
If your phone has Facebook App, it will redirect to your application without any interruptions as such.
EDIT -
This behaviour is also found in iOS 10.
Thanks for the edit - Tiois

Facebook apps using react native. Can't find ViewController.m

I'm creating an app using react native and xcode. I've been following the set up instructions on facebook because I'm going to implement their login. However it says to add some code into ViewController.m in your xcode project, but when I initialize a new react project in terminal, it doesn't create one. I also can't find the ViewDidLoad method. Any suggestions?
In one of your app's ViewController.m files, and also in the AppDelegate.m file add:
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>
In the ViewController.m file, add the following code to the viewDidLoad method:
FBSDKLoginButton *loginButton = [[FBSDKLoginButton alloc] init];
loginButton.center = self.view.center;
[self.view addSubview:loginButton];
Add this method in your app's AppDelegate.m file:
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
return [[FBSDKApplicationDelegate sharedInstance] application:application
openURL:url
sourceApplication:sourceApplication
annotation:annotation];
}
Solved. React Native doesn't create ViewController files. If you are using React Native and the facebook app guide, disregard the 'Let's test out your integration' steps.

ios Facebook Login button - doesn't change to Log out

I'm using Xcode 6 & Facebook iOS SDK 4.3,
I embedded the code, according to Facebook's guide and the login works, BUT
from some reason -
after I login, the button doesn't change to "Log out", and stays "Log in with Facebook".
did anyone encounter this problem?
so I started all over, and followed the instructions from the guide:
1.in the viewController.h file:
#import < FBSDKCoreKit/FBSDKCoreKit.h> //<-delete the space
#import < FBSDKLoginKit/FBSDKLoginKit.h>//<-delete the space
#interface ViewController : UIViewController<FBSDKLoginButtonDelegate>
#property (weak, nonatomic) IBOutlet FBSDKLoginButton *loginButton;
2.in the viewController.m file:
-(void) viewDidLoad{
if ([FBSDKAccessToken currentAccessToken]) {
// User is logged in, do work such as go to next view controller.
}
self.loginButton.readPermissions = #[#"public_profile", #"email", #"user_friends"];
FBSDKLoginButton *loginButton = [[FBSDKLoginButton alloc] init];
[self.view addSubview:loginButton];
}
and after that didn't work, I've found this thread that mentioned I also need to add few things to the AppDelegate didFinishLaunchingWithOptions :
return [[FBSDKApplicationDelegate sharedInstance] application:application
didFinishLaunchingWithOptions:launchOptions];
and
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [[FBSDKApplicationDelegate sharedInstance] application:application
openURL:url
sourceApplication:sourceApplication
annotation:annotation];
}
and then it worked!
finally I had the Log in button changed to Log out!
I was experiencing the same issue, and couldn't find the answer anywhere.
Turns out it was a Simulator problem; once I ran the same code on the device (iOS 9.3.1) everything worked out fine.
Remember to enable keychain sharing capability

iOS - Facebook single sign on - from my view controller

In the documentation from facebook to integrate their sdk in iOS app, they mentioned everything in app delegate. But in my application, i've settings view which has option for "Facebook". When user clicks that then only i want to enable single sign on from my application. This can be done by creating facebook object etc in my settings view controller. But in app delegate, they are asking to ad the following code:
// 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];
}
And both the above methods has to be implemented in app delegate class with "facebook" as object. But I'm creating my facebook object in my settings view controller. How should I handle this situation?
Add this to your AppDelegate.m (and add it to the .h header file)
+ (AppDelegate*) sharedAppDelegate {
return (AppDelegate*) [UIApplication sharedApplication].delegate;
}
Include the header file and do this to access your facebook object from any view controller:
[[AppDelegate sharedAppDelegate] facebook]
as mentioned in the docs, I'm creating the facebook object and calling the methods in app delegate from the settings view

Implementing SSO return a Blank view

I'm trying to implement SSO following the Tutorial:
https://developers.facebook.com/docs/mobile/ios/build/
At the end of Step 3, I'm supposed to Test my App running it on Simulator. The Run succeeded but all I see is a Blank View (screen). I'm not sure if I need to create the view called "authorization dialog" including the FB logo, buttons, etc or if it's automatically created by code I've implemented using Facebook SDK.
Also, I'm using a storyboard and wonder if that's the problem.
I think that it's because you have once signed in and now you haven't logged out so it is already authorized and hence you cannot see anything :)
Is your initial ViewController blankView ? Because the SSO only validates/autorize the session, you need to implement the UI in your ViewController.
If you implement it in Storyboard I suggest you to do most of the code in your controller file, since I was stucked there for quite long time. But make sure to put this code in the app delegate file.
#pragma mark Facebook
- (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];
}
Also in the delegate file you should instantiate the Facebook object before you use the in the above methods.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
viewController *controller = [[viewController alloc] init];
// Initialize Facebook
facebook = [[Facebook alloc] initWithAppId:#"Your app ID in string" andDelegate:controller];
// Override point for customization after application launch.
return YES;
}

Resources