facebook的影响大家都知道吧,最近在程序中也集成了facebook的功能。到现在为止,facebook SDK for iphone有两个版本,第一个版本是Oauth1.0的认证, 第二个版本是Oauth2的认证过程。前几天,用旧版本的时候遇到点小问题,所有的app_key与secret_key只能登录一次,然后就再也不能登录了,初步推断是facebook做了点修改, 第二天的时候又完全可以了。 所以我还是建议大家用新版本的SDK.
旧版本的SDK如何用,有程序达人在自己的blog中介绍了详细的步骤。我还是转过来, 有些地方该网站被屏蔽了。
------------------------以下为转载内容------------------------------
Integrating Facebook into your app is a great way to let users share content from your app with their friends. For example, if you have an app where users can record their grades, why not add a button to let the users share their final grades with their friends at the end of the semester? It could be fun for the users, and could also let a lot more people know about your app.
The Facebook developers have made it quite easy to integrate Facebook in your iPhone app with a great SDK. This article will show you how to quickly and integrate Facebook into your iPhone app – and it comes with a simpleexample projectto follow along with!
Update:Since writing this article, anew Facebook SDK(using the Graph API) has come out. This article does not cover that – it covers theold Facebook SDK(using the REST API), which is still available and operational at the time of this writing.
There have been several changes to the Facebook SDK since I wrote this article (early this year). I may update this article with the changes at some point, but until then no guarantees are made! :]
The first thing you have to do is to register your app with Facebook. It’s an extremely simple process:
Next, go to theFacebook iPhone SDK project pageand click the “Download Source” button to pull down the latest copy of the SDK. Feel free to look through the project. In particular, the “Connect” sample project is a great demo of some of the most important functionality.
But for the purposes of this article, we’re going to dive straight into things! Adding the SDK into your project is easy:
To log the user into Facebook, you need to set up a session for the user. Facebook sessions are managed by the FBSession object – and there can only be one session at a time. Therefore, you probably want to declare your FBSession somewhere that is easily accessible by any place in your code that may need to use it. In thesample project, I just have a single view controller, so I put it there, but for larger projects you should use a more central location.
To initialize your session, you need to give it some API keys so Facebook can distinguish between your application and someone else. This is often done in the viewDidLoad method where you want to start using Facebook, and there are two methods of doing this:
Here’s what it looks like with the sessionForApplication:secret:delegate method:
Next, it’s time to log the user in. The Facebook SDK makes it very easy to display a login dialog:
If you’d like to know if and when the user successfully logs in, all you have to do is implement the session:didLogin method in your FBSessionDelegate.
One of the nice things about sessions is they persist to disk automatically. After you set the API keys for your session, you can restore an old session by calling “[_session resume]“. This method will return YES or NO based on whether the session is valid or not, and it will also call session:didLogin on your delegate if the session is valid
You can post to a user’s wall using the FBSteamDialog class, and a bit of markup. First, here’s an example:
The above will result in a post that looks something like this
For simple posts, you should be able to tweak this sample pretty easily. If you want to do something more complex, refer to this greatFacebook post attachment reference guidefor all of the details.
If you integrate Facebook into your app, you’re probably going to want to get the user’s Facebook name, so that you can display who they are currently logged in as and let them log out, if nothing else.
The easiest way to do this is to use the same method used in the example provided by the Facebook SDK team – run a query against the Facebook database after a successful login.
The results will come back in the request:didLoad method in your FBRequestDelegate:
That’s all there is to it! So if you haven’t already, take a look at theexample iPhone project showing how to make a Facebook postlike the above. Feel free to play around (and even use the included API/Secret key) to test things out.
If you are writing a new app that you want to integrate with Facebook and want to be on the cutting edge, check out the follow-up tutorial series onHow To USe Facebook’s New Graph API from your iPhone App. The new API is kind of cool and lets you do some things that you just can’t do with the old API in Facebook Connect.
How have you been using (or planning to use) Facebook in your apps? How has it been working out for you?
如果你不能下载源码,这儿可以下载。
------------------------转完---------------------------------
现在官网大力支持新版sdk所以,你可以去下载, 源码里包括了一个demo,一看即明。