且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用 facebook ios sdk for iphone 实现单点登录 (SSO)

更新时间:2022-12-22 11:36:40

该方法用于确定您的应用程序是否会处理"传递给它的 url.例如,您的应用程序可以处理特定的 URL方案",然后在运行时选择它是否会处理特定的 URL.如果您返回 NO,它将转到其他可能也处理相同 URL 方案的应用程序.尽管在 Facebook iOS SDK 的情况下,您的应用程序将是唯一处理 URL 方案的应用程序.

当您尝试登录 Facebook 时,SDK 会将值传递给 Facebook 应用程序或 Facebook 网站,并且它包含一个回调 URL,以便可以完成授权过程.如果在应用程序委托中没有此方法,该回调将无法正常工作.

有关 oAuth 的更多信息,请参阅:

http://developers.facebook.com/docs/authentication/

其次,这是一个比 Facebook SDK 更大的问题.这与核心架构问题有关.您可以在应用程序委托中包含 Facebook 对象,然后在视图控制器中引用它.这是最简单的方法.在视图控制器中,您可以执行以下操作:

Facebook *facebook = [(YourAppDelegate *)[UIApplication sharedApplication] delegate] facebook];

此选项并不总是理想的,因为它使应用程序成为许多共享实例的垃圾场.

另一种选择是让应用程序委托在创建 Facebook 对象时将其传递给视图控制器(视图控制器将具有 Facebook 对象的属性 - 然后应用程序委托将在初始化后将其传递给视图控制器).

MyViewController *vc = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];vc.facebook = self.facebook;

还有其他用于共享类实例的选项,例如 Objection iOS 框架,但这些选项更高级.

I am using the latest facebook ios sdk in my app. In order to implement SSO, Facebook says:

Modify your application's main AppDelegate class as follows:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url

In this method, call your application's Facebook object's handleOpenURL method, making sure to pass in the url parameter.

My query is:

  1. What is the use of the above method? Why to call it? If I hide the method in the facebook demo app provided it always redirects me to login page again and again. If I unhide this method it redirects me to logout with other buttons option (which is right)

  2. Now as this method is defined in ApplicationDelegate.m file and we have to call facebook object in it. How can I refer to the facebook object in this method as my facebook object is defined in a view controller class which is 4 times down the hierarchy.

NOTE: I have tried the DemoApp. I do not understand how did they refer to the facebook object in the AppDelegate.m file inspite of the facebook object declared in ViewController file.

The method is used to determine if your application will 'handle' the url that is passed to it. For example, your application can handle a specific URL 'scheme' and then choose at runtime if it will handle the specific URL. IF you return NO, it will move on to other application that may also handle the same URL scheme. Although, in the case of the Facebook iOS SDK, your application would be the only application handling the URL scheme.

When you attempt to login to Facebook, the SDK passes values to either the Facebook application or to the Facebook website and it includes a callback URL so that the authorization process can be completed. Without this method in the app delegate, that callback won't work properly.

For more information on oAuth, see:

http://developers.facebook.com/docs/authentication/

Second, this is a bigger question than just the Facebook SDK. This has to do with a core architectural issue. You can include the Facebook object in your application delegate and then reference it in the view controller. This is the easiest method. Within the view controller, you could just do the following:

Facebook *facebook = [(YourAppDelegate *)[UIApplication sharedApplication] delegate] facebook];

This option isn't always ideal, because it makes the app delegate a dumping ground for a lot of shared instances.

Another option is to have the application delegate pass the Facebook object to the view controller when you create it (the view controller would have a property for the Facebook object - and then the application delegate would pass it to the view controller after init).

MyViewController *vc = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
vc.facebook = self.facebook;

There are also other options for sharing instances of classes such as the Objection iOS framework, but these are a bit more advanced.