UPDATE: I realized that the problem below only occurs if the user has already granted Facebook access to my application and then restored my application. The first time a user selects the Facebook button in my application, he goes to the Facebook application to get authentication, but since the authentication status is stored in the Facebook application, he immediately returns to my application without explanation to the user, and the postToFacebookWall method does not work.
UPDATE 2: I finished implementing the solution found here: How to authorize a user through DIALOG using the new iOS Facebook Connect API? and avoid the Facebook app. Safari is better, because at least the user gets a message that they will be logged in. However, the feed dialog still does not work the first time, as before.
I have something that should be a common problem, but I have not yet been able to find a solution. Please note that my application is limited to iOS 5 and above.
I have an application with a Facebook button for posting on a user’s wall. When the button is pressed, the application checks if the user has permission to publish the application on the wall. If they are, a pop-up window opens with a channel dialog with information about the wall. If they did not, Safari will open and the user will be allowed to log in. After logging in, they will be returned to the application and the Feed Channel dialog box will appear.
This works on the simulator and on my device if the Facebook application is not installed. If the Facebook application is installed, the user goes to the Facebook application, but then immediately returns to the original application, not allowing the user to do anything. The Feed Channel dialog box appears, but it is completely empty and is automatically canceled. Pressing the Facebook button again will display the Submission dialog box, as expected when the user gave permission to the application to publish it on the wall.
I pretty much followed the instructions that were here: http://developers.facebook.com/docs/mobile/ios/build/#implementsso
and here: http://developers.facebook.com/docs/reference/dialogs/feed/
In AppDelegate, I have the following code:
- (void)loginToFacebook { // Set up facebook self.facebook = [[Facebook alloc] initWithAppId:@"YOUR_APP_ID" andDelegate:self]; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; if ([defaults objectForKey:@"FBAccessTokenKey"] && [defaults objectForKey:@"FBExpirationDateKey"]) { self.facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"]; self.facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"]; } if (![self.facebook isSessionValid]) { [self.facebook authorize:nil]; } } // This is an FBSessionDelegate protocol method // that gets called after a successful login - (void)fbDidLogin { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults setObject:[self.facebook accessToken] forKey:@"FBAccessTokenKey"]; [defaults setObject:[self.facebook expirationDate] forKey:@"FBExpirationDateKey"]; [defaults synchronize]; [[NSNotificationCenter defaultCenter] postNotificationName:@"POST_TO_FACEBOOK_WALL" object:nil]; } // This is a UIApplicationDelegate protocol method // It is called when safari is dismissed - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { return [self.facebook handleOpenURL:url]; }
The only difference between this and the sample code is that I added NSNotification, which picked up my view controller, where I have the following code:
- (void)facebookButtonPressed:(UIControl*)sender { Facebook *facebook = [(AppDelegate*)[[UIApplication sharedApplication] delegate] facebook]; if (![facebook isSessionValid]) { // Login to facebook if not already logged in [(AppDelegate*)[[UIApplication sharedApplication] delegate] loginToFacebook]; } else { [self postToFacebookWall]; } } - (void)postToFacebookWall { Facebook *facebook = [(AppDelegate*)[[UIApplication sharedApplication] delegate] facebook]; /*Facebook Application ID*/ // TODO: Get this either from the AppDelegate facebook property or the info plist NSString *client_id = @"YOUR_APP_ID"; 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]; }
Does anyone know how to fix my problem? (Also, if this is not clear, YOUR_APP_ID is replaced by my application identifier in my code).