简体   繁体   English

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

[英]How to implement Single Sign On (SSO) using facebook ios sdk for iphone

I am using the latest facebook ios sdk in my app.我在我的应用程序中使用最新的 facebook ios sdk。 In order to implement SSO, Facebook says:为了实现 SSO,Facebook 说:

Modify your application's main AppDelegate class as follows:修改你的应用程序的主 AppDelegate class 如下:

- (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.在此方法中,调用应用程序的 Facebook 对象的 handleOpenURL 方法,确保传入 url 参数。

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.如果我在 facebook 演示应用程序中隐藏该方法,只要它总是一次又一次地将我重定向到登录页面。 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.现在这个方法是在 ApplicationDelegate.m 文件中定义的,我们必须在其中调用 facebook object。 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. 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.注意:我已经尝试过 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. 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.该方法用于确定您的应用程序是否会“处理”传递给它的 url。 For example, your application can handle a specific URL 'scheme' and then choose at runtime if it will handle the specific URL.例如,您的应用程序可以处理特定的 URL '方案',然后在运行时选择它是否将处理特定的 URL。 IF you return NO, it will move on to other application that may also handle the same URL scheme.如果您返回 NO,它将转到其他可能也处理相同 URL 方案的应用程序。 Although, in the case of the Facebook iOS SDK, your application would be the only application handling the URL scheme.尽管在 Facebook iOS SDK 的情况下,您的应用程序将是唯一处理 ZE6B3931A8D2C4D45708 方案的应用程序。

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. 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:有关 oAuth 的更多信息,请参阅:

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

Second, this is a bigger question than just the Facebook SDK.其次,这是一个比 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.您可以在应用程序委托中包含 Facebook object,然后在视图 controller 中引用它。 This is the easiest method.这是最简单的方法。 Within the view controller, you could just do the following:在 controller 视图中,您可以执行以下操作:

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). 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.还有其他用于共享类实例的选项,例如 Objection iOS 框架,但这些选项更高级一些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM