iOS:无需离开应用程序即可连接到 Facebook 进行授权

发布于 2024-11-01 13:44:14 字数 438 浏览 0 评论 0原文

我知道在 Graph API 出现之前这是可能的。

我正在开发一个可能不在后台的 iPhone 应用程序(要求之一)。
此外,应用程序启动时还有一个登录屏幕。
因此,每次用户想要分享某些内容时,不适合进入后台以向 Facebook 进行身份验证,然后返回到应用程序并再次登录。

所以,我的问题是是否有一种方法可以在不离开应用程序的情况下通过 Facebook 进行身份验证。

顺便说一句,我尝试过使用旧的 API。
一开始它可以工作,但昨天它停止工作了。
我只在旧的 Facebook 登录 Web 视图中看到一个空白屏幕。
我还检查了我的一个旧应用程序,该应用程序使用旧的 Facebook Connect API,并且我在该应用程序中也得到了相同的空白登录屏幕。

任何想法将不胜感激。

先感谢您。

--
迈克尔.

I know that it was possible before the Graph API.

I work on an iPhone app that may not be in the background (one of the requirements).
In addition there is a login screen on the app launching.
Therefore it is not suitable to go to background in order to authenticate to Facebook and then return to the app and login once again each time the user wants to share something.

So, my question is if there is a way to authenticate with Facebook without leaving the app.

BTW, I have tried to use the old API.
It worked in the beginning but yesterday it stopped working.
I just see a blank screen inside the old Facebook login web view.
I have also checked one of my old apps that use that old Facebook Connect API and I get the same blank login screen in that app too.

Any idea will be appreciated.

Thank you in advance.

--
Michael.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情场扛把子 2024-11-08 13:44:14

在 Facebook.m

- (void)authorizeWithFBAppAuth:(BOOL)tryFBAppAuth
                    safariAuth:(BOOL)trySafariAuth {
  NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                 _appId, @"client_id",
                                 @"user_agent", @"type",
                                 kRedirectURL, @"redirect_uri",
                                 @"touch", @"display",
                                 kSDKVersion, @"sdk",
                                 nil];

方法中注释掉这个,

 UIDevice *device = [UIDevice currentDevice];
  if ([device respondsToSelector:@selector(isMultitaskingSupported)] && [device isMultitaskingSupported]) {
    if (tryFBAppAuth) {
      NSString *fbAppUrl = [FBRequest serializeURL:kFBAppAuthURL params:params];
      didOpenOtherApp = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:fbAppUrl]];
    }

    if (trySafariAuth && !didOpenOtherApp) {
      NSString *nextUrl = [NSString stringWithFormat:@"fb%@://authorize", _appId];
      [params setValue:nextUrl forKey:@"redirect_uri"];

      NSString *fbAppUrl = [FBRequest serializeURL:loginDialogURL params:params];
      didOpenOtherApp = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:fbAppUrl]];
    }
  }

这将阻止应用程序进入后台并显示标准的 Facebook 对话框。

In Facebook.m

- (void)authorizeWithFBAppAuth:(BOOL)tryFBAppAuth
                    safariAuth:(BOOL)trySafariAuth {
  NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                 _appId, @"client_id",
                                 @"user_agent", @"type",
                                 kRedirectURL, @"redirect_uri",
                                 @"touch", @"display",
                                 kSDKVersion, @"sdk",
                                 nil];

method comment out this

 UIDevice *device = [UIDevice currentDevice];
  if ([device respondsToSelector:@selector(isMultitaskingSupported)] && [device isMultitaskingSupported]) {
    if (tryFBAppAuth) {
      NSString *fbAppUrl = [FBRequest serializeURL:kFBAppAuthURL params:params];
      didOpenOtherApp = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:fbAppUrl]];
    }

    if (trySafariAuth && !didOpenOtherApp) {
      NSString *nextUrl = [NSString stringWithFormat:@"fb%@://authorize", _appId];
      [params setValue:nextUrl forKey:@"redirect_uri"];

      NSString *fbAppUrl = [FBRequest serializeURL:loginDialogURL params:params];
      didOpenOtherApp = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:fbAppUrl]];
    }
  }

This will prevent the app from going to background and show you the standard fb dialog.

花开半夏魅人心 2024-11-08 13:44:14

在这里您有一个替代解决方案。

如果您不喜欢更改 facebook sdk 代码并想要一个允许您在 SSO 或老式机制之间进行选择的解决方案,您可以实现如下扩展:

//Facebook_SSOExtension.h
--------------------------------------------------------

@interface Facebook(SSOExtension)
-(void) authorize:(NSArray*)permissions useSSO:(BOOL) useSSO;
@end

//Facebook_SSOExtension.m
--------------------------------------------------------

//So warnings do not appear
@interface Facebook(PrivateSSOExtension)
- (void)authorizeWithFBAppAuth:(BOOL)tryFBAppAuth
                    safariAuth:(BOOL)trySafariAuth;
-(void) setPermissions:(NSArray*) permissions;
@end 

@implementation Facebook(SSOExtension)
-(void) authorize:(NSArray*)permissions useSSO:(BOOL) useSSO
{
    [self setPermissions: permissions];
    [self authorizeWithFBAppAuth:useSSO safariAuth:useSSO];
}
@end

尽管这比注释 sso 需要更多的工作代码,您将能够更新 facebook-sdk 而不会出现重大问题(如果他们重命名authorizeWithFBAppAuth:safariAuth:您的扩展将无法工作,请使用断言快速检测此问题)。另外,如果您正在构建一个可重用的组件来与 facebook 交互,而不需要一遍又一遍地重复事情,这也将为您节省一些工作。

干杯。

Here you have an alternative solution.

If you don't like to change the facebook sdk code and want a solution that allows you to choose between SSO or the old-fashioned mechanism, you can Implement an extension like this:

//Facebook_SSOExtension.h
--------------------------------------------------------

@interface Facebook(SSOExtension)
-(void) authorize:(NSArray*)permissions useSSO:(BOOL) useSSO;
@end

//Facebook_SSOExtension.m
--------------------------------------------------------

//So warnings do not appear
@interface Facebook(PrivateSSOExtension)
- (void)authorizeWithFBAppAuth:(BOOL)tryFBAppAuth
                    safariAuth:(BOOL)trySafariAuth;
-(void) setPermissions:(NSArray*) permissions;
@end 

@implementation Facebook(SSOExtension)
-(void) authorize:(NSArray*)permissions useSSO:(BOOL) useSSO
{
    [self setPermissions: permissions];
    [self authorizeWithFBAppAuth:useSSO safariAuth:useSSO];
}
@end

Even though this requires more work than commenting-out the sso code, you will be able to update the facebook-sdk without major problems (if they rename authorizeWithFBAppAuth:safariAuth: your extension will not work, use asserts to detect this issue quickly). Also, if you are building a reusable component to interact with facebook without repeating things over and over again, this will save you some work too.

Cheers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文