iOS 6 get Facebook access token with device account - facebook

The application I'm working on supports both iOS 5 and 6.
For iOS 5 I get an access token by using the FBSession object. The same thing works for iOS 6 but doesn't let me use the ACAccount that is already on the device and always asks the user to input email/password.
Since Facebook won't be updating their SDK for a couple of weeks to support this feature, and I don't have the option of waiting that long, I was wondering how do I get an access token using the accounts or social frameworks.
I've spent a few hours searching for an answer, but still no luck. So thanks to everybody in advance.
EDIT
Here is the code for getting the acces token with Facebook SDK 3.1+
[FBSession openActiveSessionWithReadPermissions:permissions
allowLoginUI:YES
completionHandler:^(FBSession *session, FBSessionState status, NSError *error){
if (session.isOpen) {
switch (status) {
case FBSessionStateOpen:
// here you get the token
NSLog(#"%#", session.accessToken);
break;
case FBSessionStateClosed:
case FBSessionStateClosedLoginFailed:
[[FBSession activeSession] closeAndClearTokenInformation];
break;
default:
break;
} // switch
}];

This issue has been fixed with Facebook SDK 3.1.

Related

facebook login works on iphone simulator but not on iphone device

In my app, I want to take photos from the facebook. so i used facebook ios sdk. i can be able to authorize from the below code:
if (appDelegate.session.isOpen) {
//[appDelegate.session closeAndClearTokenInformation];
[self retrieveAlbums];
} else {
if (appDelegate.session.state != FBSessionStateCreated) {
// Create a new, logged out session.
appDelegate.session = [[FBSession alloc] init];
}
// if the session isn't open, let's open it now and present the login UX to the user
[appDelegate.session openWithCompletionHandler:^(FBSession *session,
FBSessionState status,
NSError *error) {
// and here we make sure to update our UX according to the new session state
//NSLog(#"%#",session.accessTokenData.accessToken);
[self retrieveAlbums];
}];
It works fine on iphone simulator. But I can't able to login on my iphone 4 device.
When I tried to login it will open a safari and then shows the message Updating.
Then it will redirect without authorization. access token retrieved as nil. So I cant able to retreive images.
Please help me.
EDIT
Thanks for everyone .Finally I solved my problem. It is because i enabled sandbox mode in facebook developer account settings. When i disabled the sandbox mode, it works normally.
Try clearing the cookies and cache of your safari, going to Settings.
Also if you do not want the app to redirect to Safari, just dont provide the URL Scheme in your info.plist that is something like "fb-AppId-" and it will open a popup for login
I was having the similar problem and solved it by composing couple of solutions in this website. First of all I got rid of the url scheme at my info plist. So now my app opens up the facebook login dialog inside my registration page. Which is a good thing because it doesn't go back and forth to safari for facebook authentication and you stay inside the app. The second thing I made is, putting an error handling mechanism inside my code for facebook login. So if login with facebook fails, it uses another strategy for logging in.
Here is my code.
- (void)loginView:(FBLoginView *)loginView handleError:(NSError *)error {
NSArray *permissions = [[NSArray alloc] initWithObjects:
#"public_profile", #"user_friends", #"email",
nil];
FBSession *session = [[FBSession alloc] initWithPermissions:permissions];
[FBSession setActiveSession:session];
[self openSessionWithAllowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
if (!error && status == FBSessionStateOpen) {
NSString *token = session.accessTokenData.accessToken;
NSLog(#"Logged in! - Token -- %#", token);
} else {
NSLog(#"Something wrong happened");//you can check the error code here
}
}];
}

Facebook iOS SDK 3.1 openActiveSession.. always returns 0

I am working on an application that needs to be able to upload an image for the app for iOS versions 5.0+. For iOS 6, I have easily implemented the social feed that works perfectly.
For iOS 5, I am using the Facebook iOS SDK version 3.1. I use the methods that were in the tutorials, and so when I got to publish the image on iOS <6.0, I call the following method:
- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI
{
return [FBSession openActiveSessionWithPublishPermissions:[NSArray arrayWithObjects:#"publish_actions", nil] defaultAudience:FBSessionDefaultAudienceFriends allowLoginUI:allowLoginUI completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
[self sessionStateChanged:session
state:status
error:error];
}];
}
With [self openSessionWithAllowLoginUI:YES]; I always get back NO (0) and sessionStateChanged:state:error is never called due to it returning 0.
I have the app set up correctly, the two different parts set up in the plist, etc. etc., but for some reason this code on my iOS 5.0 device does not want to return correctly. (Just like in openSessionWithAllowLoginUI almost always returns NO)
Does anyone have any solutions on why it would always be returning no? I have the official FB app installed on the device, I am logged into my account...I am not getting any errors or anything...
Thanks in advance!

iOS 6 facebook Auth Application status

I'm doing Facebook auth with SDK 3.1 on iOS6.
I have an existing app that was using old FB SDK.
Apparently, I've implemented:
[FBSession openActiveSessionWithReadPermissions:permissions
allowLoginUI:YES
completionHandler:
^(FBSession *session,
FBSessionState state, NSError *error) {
[self sessionStateChanged:session state:state error:error];
}];
but initially I've got "error 2" with FBSessionStateClosedLoginFailed after the first logging attempt.
No auth dialog was provided. App initially became inactive in iOS-Settings -> Facebook.
I've tried several solutions, but the setting is disabled by default on any new device with configured Facebook account.
Please, help :)
Did you try with Facebook SDK 3.5.1? I have developed a plugin for game developers using the same SDK and none of my plugin user facing this problem.

Facebook IOS SDK 3.1 - reauthorizeWithPublishPermissions?

Just upgraded to the Facebook iOS SDK 3.1 and already running into problems.
My app can't find the facebook method reauthorizeWithPublishPermissions, and it also hasn't got a clue what the static FBSessionDefaultAudienceFriends definition is!
Anyone else seen these problems? Can't believe that the 3.1 SDK would have these basic bugs in it, so I'm blaming my stupidity for now!!
For completeness, here is my code:
[FBSession reauthorizeWithPublishPermissions:[NSArray arrayWithObjects:#"publish_stream", #"publish_actions",nil]
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error) {
// Do something...
}];
Thanks.
#Zotter, answer is depreciated. Now (with latest Facebook iOS SDK, I guess v.3.19) you've to use - (void)requestNewReadPermissions:(NSArray *)readPermissions completionHandler:(FBSessionRequestPermissionResultHandler)handler; method, to authorise app with publish permissions.
[FBSession.activeSession requestNewPublishPermissions:permissions
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error) {
if(!error) {
}
}];
/*! No audience needed; this value is useful for cases where data will only be read from Facebook */
FBSessionDefaultAudienceNone = 0,
/*! Indicates that only the user is able to see posts made by the application */
FBSessionDefaultAudienceOnlyMe = 10,
/*! Indicates that the user's friends are able to see posts made by the application */
FBSessionDefaultAudienceFriends = 20,
/*! Indicates that all Facebook users are able to see posts made by the application */
FBSessionDefaultAudienceEveryone = 30,
reauthorizeWithPublishPermissions: is an instance method
[FBSession.activeSession reauthorizeWithPublishPermissions:permissions defaultAudience:FBSessionDefaultAudienceFriends completionHandler:
^(FBSession *session,
NSError *error)
{
...
}];
I had this problem. I was using an old version of Cocoapods that wasn't properly upgrading the source from 3.0 to 3.1. If you're using Cocoapods, upgrade to 0.14.0 and try again. If not, double check to make sure you have the latest Facebook SDK from master - if you don't see those values in FBSession.h, you probably have an old version of the SDK.

Facebook Connect SSO not working on some iPhones

I have implemented FBConnect SSO in my iphone application according to this tutorial. I have tested it on two phones so far, one of which lets me use the function just fine, whereas the other phone simply opens up the Facebook app and closes it right away. I am able to establish a valid session, yet the dialog seems to just close on me on one of the ones.
What could be the problem? Any help is much appreciated.
I think that your problem is that you dont have the proper configuration, either in the client side (your app) or in the server side (the FB app configuration). Please, check both things as follows:
Client app:
Make sure that you have introduced the correct FBAppId into your Info.plist as a URL handler. Facebook will try to open the app by using this handler, and if it doesn't work, your app will not be opened.
(replace the XXXXXXXXXX with your FB appID)
Check whether your app (in the appDelegate) handles the custom URL added above:
- (BOOL)handleOpenURL:(NSURL*)url
{
NSString* scheme = [url scheme];
NSString* prefix = [NSString stringWithFormat:#"fb%#", facebookAppId];
if ([scheme hasPrefix:prefix]) {
return [fb handleOpenURL:url];
}
return YES;
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [self handleOpenURL:url];
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
return [self handleOpenURL:url];
}
Server side
Check that you have correctly configured your bundleId in the FB config page. You can check this in https://developers.facebook.com/apps, selecting you app, and then in the edition panel:
If you have both things correctly configured it should work in any case (with the FBapp installed or Safari based).
I have seen this with the 3.0 SDK. The reason it occurs is because of the way facebook caches access tokens. Access tokens are cached both in your app, and in the facebook app on the device (at least conceptually).
If it's the first time you started your app on the device, facebook won't have a valid access token cached in your app. However, it may still have a valid access token in the facebook app for your app. If it finds one on the facebook side, then it doesn't have to ask the user again, so the dialog appears, then disappears, and it sends back the access token. The access token is then cached in your app.
If you restart your app (without deleting it), you won't see the login dialog at all since the access token is now cached in your app. If you delete your app on the device and login again, you'll see the same behavior, as long as facebook can find a cached token on it's side.
I have found a solution to this problem. Here is some sample code:
if (![FBSession activeSession].isOpen) {
FBSession *session = [[FBSession alloc] initWithAppID:nil permissions:permissions defaultAudience:FBSessionDefaultAudienceEveryone urlSchemeSuffix:nil tokenCacheStrategy:nil];
[FBSession setActiveSession:session];
[session openWithBehavior:FBSessionLoginBehaviorUseSystemAccountIfPresent completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
// NOTE openActiveSessionWithPermissions causes the facebook app to hang with a blank dialog if my app is installed, authed, and then reinstalled. openWithBehavior
// does not. I think it has something to do with the FBSession activeSession.
// [FBSession openActiveSessionWithPermissions:self.permissions allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
switch (status) {
case FBSessionStateOpen:
[self getFacebookUser];
break;
case FBSessionStateClosed:
[self fbSessionClosed];
break;
case FBSessionStateCreated:
[self fbSessionCreated];
break;
case FBSessionStateCreatedOpening:
[self fbSessionOpening];
break;
case FBSessionStateClosedLoginFailed:
[self fbSessionClosedLoginFailed];
break;
case FBSessionStateOpenTokenExtended:
[self fbSessionOpenTokenExtended];
break;
case FBSessionStateCreatedTokenLoaded:
[self fbSessionCreatedTokenLoaded];
break;
}
}];
}
}
openActiveSessionWithPermissions causes problems, but openWithBehavior does not. openActiveSession makes the same openWithBehavior call, but I think the timing of setting the active session is different, and hence does not work. If I set the active session before calling openWithBehavior, everything seems to work fine. As far as your app and the end user are concerned, everything is identical.
You need to insert suitable logging throughout your code, post the code, and post what logs are being made in the working and the failure cases.
To make a log (see here for more advanced techniques):
NSLog(#"My log text");
At a minimum you need to add logs in all the callbacks from the Facebook API (even the ones you aren't using), and in your handleOpenURL and openUrl methods in the application delegate.
Your logs will appear in the Console in the Xcode Organizer when you connect the device you are testing with - that should show you what is happening.
FYI, the Facebook SDK code will first attempt to use the native app if it is installed, otherwise it will launch the Safari browser to ask for authorization through the web version of Facebook.
You should double check your Facebook URL scheme configuration, as described in the answer by Angel GarcĂ­a Olloqui.