如何使用 facebook ios sdk for iphone 实现单点登录 (SSO) [英] How to implement Single Sign On (SSO) using facebook ios sdk for iphone

查看:45
本文介绍了如何使用 facebook ios sdk for iphone 实现单点登录 (SSO)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用最新的 facebook ios sdk.为了实施 SSO,Facebook 表示:

如下修改您的应用程序的主 AppDelegate 类:

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

在此方法中,调用应用程序的 Facebook 对象的 handleOpenURL 方法,确保传入 url 参数.

我的查询是:

<块引用>

  1. 上面的方法有什么用?为什么叫它?如果我在 facebook 演示应用程序中隐藏该方法,只要它总是一次又一次地将我重定向到登录页面.如果我取消隐藏此方法,它会将我重定向到使用其他按钮选项注销(这是正确的)

  2. 现在这个方法是在 ApplicationDelegate.m 文件中定义的,我们必须在其中调用 facebook 对象.我如何在此方法中引用 facebook 对象,因为我的 facebook 对象是在视图控制器类中定义的,该类是层次结构的 4 倍.

注意:我已经尝试过 DemoApp.我不明白他们是如何引用 AppDelegate.m 文件中的 facebook 对象的,尽管在 ViewController 文件中声明了 facebook 对象.

解决方案

该方法用于确定您的应用程序是否会处理"传递给它的 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.

这篇关于如何使用 facebook ios sdk for iphone 实现单点登录 (SSO)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆