Today’s post is an update for an old article about posting high scores to Facebook Wall. Some users have reported some issues about this class. There were some problems with the state control when authorization was needed.
Today I will like to post a new version of the class that solves these issues. The project is now called FacebookScorer and you can find it on GitHub.
With FacebookScorer you can post scores for your game on Facebook Wall on user’s behalf in a single line of code.
Features
FacebookScorer offers the following features:
- Publish scores to Facebook Wall with the Facebook Dialog
- Handles automatically login and authorize on Facebook
Deployment
1 – Add the FacebookScorer folder to your Xcode project.
2 – Add the following methods to your app Delegate: (remember to include FacebookScorer.h
)
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { return [[[FacebookScorer sharedInstance] facebook] handleOpenURL:url]; } // For 4.2+ support - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { return [[[FacebookScorer sharedInstance] facebook] handleOpenURL:url]; }
3 – Add “URL Types” array as shown on the image below to your Info.plist file. You will need to introduce your Facebook’s app ID in the “URL identifier” and “URL Schemes” fields. Notice the “fb” prefix in “URL Schemes”.
Usage
There is a simple Demo app within the project.
On top of the FacebookScorer.m
file you will find the constants to be customized, including your Facebook app ID:
static NSString* kAppId = @"000000000000000"; // Your Facebook app ID here #define kAppName @"Your App's name" #define kCustomMessage @"I just got a score of %d in %@, an iPhone/iPod Touch game by me!" #define kServerLink @"http://indiedevstories.com" #define kImageSrc @"http://refractedpixel.com/indiedevstories/wp-content/uploads/2011/08/newsokoban_icon.png"
Then, simply add this line of code where you want the Facebook dialog to appear:
[[FacebookScorer sharedInstance] postToWallWithDialogNewHighscore:123];
Done! As usual, very easy to use 😉
HTH!
Thank’s for sharing!!.
Very useful code.
Thank you, it works great!
You are welcome! :^]
Pingback: List of Open Source Cocos2d Projects, Extensions and Code Snippets | iUridium
I got the FB app popup but then it logs me out from FB app. Also if I want the dialog stay in my game, what should I do?
Update: I had a very old version of FB app on my iPhone. After I updated it, my game launches FB app (w/o logging me out from FB app), but it did not bring the post status dialog :'(
Did you add the “handleOpenURL” methods to your app’s Delegate?
Actually I forgot to changed fb app ID from the .plist file. It works great now. Thank you very much!.
Perfect 🙂
Hope you enjoy it 😀
Hi Toni and thanks for sharing!
I have tried to use your demo app but after it launches FB add user accept the app it does not post the message and return to the app. instead it stays in FB. In fact I do not get any callback as a result of this: [_facebook requestWithGraphPath:@”me” andDelegate:self];
Any ideas what I’m doing wrong?
Where should I add the FB app secret? Currently I have only added FB app id in the FacebookScorer.m and in the plist file URS Scheme. What should I add in the URL identifier?
Hi Tommy,
Did you add the “handleOpenURL” methods in your app’s Delegate?
Toni: Yes, I’m using your demo app so the handleOpenURL is there but I never end up in that method. :/
@Tommy,
I have just re-read your previous comment and saw that you asked: ” What should I add in the URL identifier?”
Do you mean in the plist file? If so, it is again the app ID. You need to insert the app ID twice in the plist file. In the URL Identifier field without the “fb” prefix.
HTH
That did all the difference. I added the app id and now it works. Many thanks for all help.
You’re welcome :^]
Awesome, this is exatcly what I needed for my apps. Works great!
Thank you for this, everything works great, it was very easy to modify for my personal needs and is a lot less convoluted than a lot of other solutions out there.
Thank you, it was a success, just one more thing, there is a way to the users log out of the facebook and uses another account to post on his wall ???
Thank you!!
Hi @Jonhy,
You could use the “logout” method of the Facebook class like so:
[[[FacebookScorer sharedInstance] facebook] logout];
HTH :^]
I have a new problem Toni, when i run the app on the simulator on the mac, it works good, but when i run on the device, it does not work, the facebook opens my facebook`s timeline and not the “window” to post high score.
This looks like a specific problem with your configuration. Send me an e-mail describing exactly the steps you and how the device responds, please.
I understand the problem and resolve it, Thank you Toni.
Was it a problem with FacebookScorer that I should address?
Hi Tony. Good Job!! I have a problem. I do:
[[[FacebookScorer sharedInstance] facebook] logout];
But when I wan’t to share with other user I share with the same user that I used before.
I don’t know why.
Sorry for my english!
Hi Toni,
Thxs again for this code really useful, I would like to do “logout” function, I use as you said “[[[FacebookScorer sharedInstance] facebook] logout];” , it logout properly but after I am not able to find back the user access window of Facebook with userName, password, any idea ?, thxs a lot I will appreciate
Ok I understand better my issue , actually when I launch FaceBook logout instructions, after I need to go to safari settings and press “Clear cookies and token” in order to find back the identification window of Facebook, do I need to add something else to clear cookies/datas in your code, thxs a lot ?!
Great tutorial and library! I managed to get the demo working and post to my Facebook page. When I try to implement the code in my own iPhone app I get a whole stack of errors about “‘release’ is unavailable in automatic reference counting mode”, “ARC forbids explicit message send of ‘release'”, etc. I am using the latest iPhone 5.1 SDK with XCode 4.3.1. Any suggestions?
Hello Damian,
FacebookScorer does not support ARC. You should disable ARC on your project settings or exclude the FacebookScorer and Facebook API files from ARC.
HTH
Awesome, that worked! Thanks for the help!
Wow thanks! Really works out of the box.
I just made a minor mistake by entering the fb000 into the field where it shouldn’t have a prefix.
Thanks a lot!
If you don’t mind, I am putting your name on my game credits. It’s rolling out soon.
Of course it is much appreciated! 🙂 Please, tell me about your game when it is out!
Thanks!! “:^]
Thanks for posting this Toni, very much appreciated!
No problem!! “:^]
Thanks for the code, so simple to use. But I have a problem. Everytime I tap the button in my app to post to the wall, it will launch the facebook app (or Safari in the simulator) and displays the line “An error occurred. Please try again later”. When I tap the “okay” button, it will load facebook but without the wall posting. Any thoughts?
Ensure you have added your facebook app ID both in “FacebookScorer.m” and “Info.plist”.
Also ensure that you have added these methods to your app delegate:
– (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [[[FacebookScorer sharedInstance] facebook] handleOpenURL:url];
}
// For 4.2+ support
– (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [[[FacebookScorer sharedInstance] facebook] handleOpenURL:url];
}
HTH
Thanks that helped, I also realised I made a mistake by adding the app name instead of the app ID, so that’s settled. But what about posting a picture? I passed the name of a picture (e.g. @”Image1.png”) to FacebookScorer but it’s not appearing on the wall. Are images meant to be grabbed from a server?
This version of FacebookScorer is designed only to post messages to the wall with a pre-defined image in the message that must be on a server.
If you want to upload images to a user Facebook album you will need to try a different solution. FacebookScorer won’t help.
HTH
Thanks for the great way of integrating facebook
Hello:
I have been using your FBScorer and really like it. But I do have a slight challenge. The first time that a user launches the app and it asks to authorize it when you press ok to authorize in the simulator it says that Safari can’t open the page. On the device Facebook crashes. Then once you reload the app then it is fine. Please advise.
Make sure you have added your facebook app ID both in “FacebookScorer.m” and “Info.plist”.
Also ensure that you have added these methods to your app delegate:
– (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [[[FacebookScorer sharedInstance] facebook] handleOpenURL:url];
}
// For 4.2+ support
– (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [[[FacebookScorer sharedInstance] facebook] handleOpenURL:url];
}
HTH
That worked. I had the info.plist file set up incorrectly. Thanks for the quick response! Do you plan on modifying the class so that you can have upload a local image rather than a server based image?
Unfortunately it is out of the scope of this simple class. FBScorer was designed to be extremely simple and easy to use.
Allow for image upload would break this so simple design and would make no sense.
However, the code to upload an image with the Facebook API is not hard. But it is difficult to encapsulate in a neat way in a Singleton.
Sorry.
Hi.. where would I find instructions on how to obtain a “facebook app ID”? Does it have to be an actual app? (as in, does it have to be any code or functionality on facebook’s side?) thanks.
Yes. You need to create a Facebook app to obtain your app ID. You can create one here: https://developers.facebook.com/apps
It takes a few minutes 🙂
HTH “:^]
Hi there 🙂
Thanks for this amazing piece of code.
I’m using it in my app but when I tap the button in my app to post to the wall, it will launch the facebook app then switch to my app and do nothing except the method fbDidNotLogin.
Although I have set the App ID in the FBScorer.m and info.plist (twice, one with the prefix).
I’m wondering where i messed up. please a little hint 🙂
Hello,
Make sure that the App ID on the plist has no blank spaces. Sometimes, when copy-pasting the prefixed one, blank spaces are left.
HTH
It’s look like the problem was the bundle ID 🙂
Anyway thanks for the code.
If you don’t mind, I am putting your name on the game credits.
Hugely appreciated! 😀
Can I see your game progress somewhere?
Good luck! “:^]
unfortunately the game progress is not online yet but it’s coming out soon 🙂
Hi Fabrice Wu,
Hi Toni,
I’m getting the same error you got, what exactly did you do to fix it? thanks
You should describe your specific problem. Otherwise, it is difficult for me to figure out what’s wrong with your code.
hi Toni, thanks for the useful facebookscorer class. I’ll include your name in my game too. BTW is there a way to just post to the wall without the dialog box? like an auto wall post, i just don’t want to break the flow of the game but want to post game progress to facebook wall. thanks again.
It has been a long time since the last time I red the iOS Facebook API, but have a look at the method called “requestForPostStatusUpdate:” from the FBRequest class.
I think it is just what are you looking for.
HTH!
Hi Toni,
Nice one there! do you have something similar for Twitter to post scores?
Thanks!
Not exactly. But you could try with this general purpose resource:
http://indiedevstories.com/2011/12/21/a-twitter-reusable-class-ios-5-0-ready-with-ios-4-x-retrocompatibility/
HTH “:^]
The FBConnect is deprecated in the official Facebook SDK.
God Bless You Toni.
Thanks a ton.
Hey is it possible to post on users wall without the popup? so that my application doesn’t break in the middle of the game?
Yes, but you will probably need to update to the new Facebook SDK version.
Hi, i just pasted the code in and pasted in the FB App ID’s to the URL like you show in your example. However when i run in the simulator it builds, but then when i click on the button to post to FB the simulator quits and the error log states MISSING AP ID!!! any idea why this is happening? thanks.
-Brett