xcode 本机 iOS Facebook SSO 不会返回到应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10625210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:21:14  来源:igfitidea点击:

Native iOS Facebook SSO won't return to app

objective-ciosxcodefacebook

提问by valheru

Ok this may seem like a duplicate but I've been through all of the questions that center around the issue and I still can't seem to get this to work for my app.

好的,这似乎是重复的,但我已经解决了围绕该问题的所有问题,但我似乎仍然无法让它适用于我的应用程序。

I have followed the iOS tutorial on the Facebook Developer site and I have done the following:

我遵循了 Facebook 开发者网站上的 iOS 教程,并完成了以下操作:

  1. Created a Facebook App to get the Facebook App ID.
  2. I've assigned this App ID to a constant and I use it to initialize the Facebook object within my app.
  3. I've made sure my app is flagged for multitasking
  4. I've setup the fbAPP_ID in the plist under the URLs drop down
  5. I've made my AppDelegate a FBSessionDelegate and implemented very simple fbDidLogin and fbDidNotLogin delegate methods.
  1. 创建了一个 Facebook 应用程序以获取 Facebook 应用程序 ID。
  2. 我已将此 A​​pp ID 分配给一个常量,并使用它来初始化我的应用程序中的 Facebook 对象。
  3. 我已经确保我的应用程序被标记为多任务处理
  4. 我已经在 URL 下拉菜单下的 plist 中设置了 fbAPP_ID
  5. 我已经让我的 AppDelegate 成为一个 FBSessionDelegate 并实现了非常简单的 fbDidLogin 和 fbDidNotLogin 委托方法。

Versions: XCode 4.3.2 on Lion, iOS 5.1, Facebook iOS 4.1.1

版本:Lion 上的 XCode 4.3.2、iOS 5.1、Facebook iOS 4.1.1

All of this and I still can't seem to get Facebook to return control back to my app when it authorizes. It just stays in Facebook instead.

所有这一切,我似乎仍然无法让 Facebook 在授权时将控制权交还给我的应用程序。它只是留在 Facebook 中。

Code: (Note FB_APP_ID changed to basic number for viewing here)

代码:(注意FB_APP_ID改成基本号码查看这里)

AppDelegate.h:

AppDelegate.h:

#import <UIKit/UIKit.h>
#import "FBConnect.h"

#define FB_APP_ID @"1234567890"

@interface AppDelegate : UIResponder <UIApplicationDelegate, FBSessionDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, strong) Facebook* facebook;

@end

AppDelegate.m:

AppDelegate.m:

#import "AppDelegate.h"

@implementation AppDelegate
@synthesize window = _window;
@synthesize facebook = _facebook;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    _facebook = [[Facebook alloc] initWithAppId:FB_APP_ID andDelegate:self];
    if (![_facebook isSessionValid])
    {
        [_facebook authorize:nil];
    }
}

- (BOOL)application:(UIApplication*)application handleOpenURL:(NSURL *)url
{
    return [_facebook handleOpenURL:url];
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    return [_facebook handleOpenURL:url];
}

- (void)fbDidLogin
{
    NSLog(@"App logged in");
}

- (void)fbDidNotLogin:(BOOL)cancelled
{
    NSLog(@"App did not login");
}

@end

Relevant Info.plist porton:

相关 Info.plist 端口:

enter image description here

在此处输入图片说明

Facebook App screen (mostly pinked out):

Facebook 应用程序屏幕(大部分为粉红色):

Facebook App screen

脸书应用画面

Note: I tried it with only basic settings and also with the iOS Native app settings with a fake iTunes store ID but the same bundle ID from my provisioning profile.

注意:我仅使用基本设置进行了尝试,还使用了带有假 iTunes 商店 ID 但与我的配置文件中的捆绑 ID 相同的 iOS 本机应用程序设置。

No matter what I do it always loads up Facebook and asks to authorize my app and then it simply closes the auth screen window and sits there on my Facebook screen instead of returning to my app.

无论我做什么,它总是加载 Facebook 并要求授权我的应用程序,然后它只是关闭身份验证屏幕窗口并坐在我的 Facebook 屏幕上,而不是返回到我的应用程序。

Also note: I know it will always ask because I'm not checking for the auth token and expiration date.....that's the next step....for now I want to at least get it to return to my app before I worry about that.

另请注意:我知道它总是会询问,因为我没有检查身份验证令牌和到期日期......这是下一步......现在我想至少让它在之前返回到我的应用程序我担心那个。

Have I missed something here? Anything else I can try to get Facebook to return to my app?

我在这里错过了什么吗?我还可以尝试让 Facebook 返回我的应用程序吗?

EDIT: Also when debugging, I can't seem to find any spot where either openURL is called for some reason.

编辑:同样在调试时,我似乎找不到任何由于某种原因调用 openURL 的地方。

EDIT #2: When running it on the simulator, it loads up the FB Dialog via Safari (as expected) and then when I authorize, safari throws an error as if the URL they are trying to use to get back to my app is invalid:

编辑 #2:在模拟器上运行它时,它通过 Safari 加载 FB 对话框(如预期),然后当我授权时,safari 抛出一个错误,好像他们试图用来返回我的应用程序的 URL 无效:

simulator safari error

模拟器 safari 错误

This still doesn't tell me much since my fb1234567890 is setup right as far as I know in the Info.plist.

这仍然没有告诉我太多,因为据我所知,我的 fb1234567890 是在 Info.plist 中设置的。

回答by SRandazzo

Your info.plist seems to be formatted incorrectly

您的 info.plist 格式似乎不正确

Ensure that the url scheme is nested under an item (an array to be exact) called "URL Schemes"

确保 url 方案嵌套在名为“URL Schemes”的项目(确切地说是一个数组)下

This array should contain all possible URL Schemes that can launch your app

此数组应包含可以启动您的应用程序的所有可能的 URL Schemes

See the attached image for a better explanation of URL Schemes

有关 URL Schemes 的更好解释,请参阅附图

url scheme

网址方案

Also, the "URL identifier" should be your bundle identifier: ex. com.mycompany.myapp This should match the identifier you provide on your provisioning profiles

此外,“URL 标识符”应该是您的包标识符:例如。com.mycompany.myapp 这应该与您在配置文件中提供的标识符相匹配

hope this helps!

希望这可以帮助!

回答by Vaibhav Saran

All you have to follow these steps

所有你必须按照这些步骤

STEP 1:

第1步:

Facebook developer side stuff:

Facebook开发者方面的东西:

If your app is already submitted then fill iphone/ipad appstore id otherwise leave it empty. facebook developer account app id settings

如果您的应用程序已经提交,则填写 iphone/ipad 应用商店 ID,否则将其留空。 facebook 开发者帐户应用程序 ID 设置

STEP 2:

第2步:

Next in your XCode:

接下来在您的 XCode 中:

Goto Target > Info > URL Types> click plus icon and add the items like this- URL Scheme settings

转到 Target > Info > URL Types>单击加号图标并添加这样的项目- URL Scheme 设置

In my application, there were both options to login via facebook and google so I created two URL Types. You can see second one are Facebook URL Type settings. URL Scheme will be your facebook app id from facebook Developer account with fbat starts. So if your facebook developer app id is 9876543267575645then URL Scheme will become fb9876543267575645.

在我的应用程序中,有两个选项可以通过 facebook 和 google 登录,所以我创建了两个 URL 类型。你可以看到第二个是 Facebook URL Type 设置。URL Scheme 将是来自 facebook 开发者帐户的你的 facebook 应用程序 id fb。因此,如果您的 facebook 开发者应用程序 ID 是9876543267575645那么 URL Scheme 将变为fb9876543267575645.

STEP 3:

第 3 步:

Next, you have to add a new key in info.plist. So remain in same screen and add FacebookAppIDlike this screen: info.plist settings

接下来,您必须在 info.plist 中添加一个新密钥。所以保持在同一个屏幕上并FacebookAppID像这个屏幕一样添加: info.plist 设置

All this stuff will bring your app back after successful facebook oauth.

在成功通过 facebook oauth 后,所有这些东西都会使您的应用程序恢复原状。

STEP 4:

第四步:

Rest you will do in your AppDelegateclass. Simply add this method in AppDelegate.m

休息你会在你的AppDelegate课上做。只需将此方法添加到AppDelegate.m

NSString *retURL = [NSString stringWithFormat:@"%@", url];    
if ([retURL rangeOfString:facebookid].location != NSNotFound)
{
    // if your application is coming back from facebook native app.
    return [_facebook handleOpenURL:url];
}
else
{
    // if your app is coming back from google plus oauth.
    return [GPPURLHandler handleURL:url sourceApplication:sourceApplication annotation:annotation];
}

during all this, must intialise and synthesize facebookinstance in AppDelegate.m

在这一切期间,必须初始化和合成facebook实例AppDelegate.m

@synthesize facebook = _facebook;

and didFinishLaunchingWithOptions

didFinishLaunchingWithOptions

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    _facebook = [[Facebook alloc] initWithAppId:facebookid];
    // rest of your code is already here......
}

One more important thing. During I was implementing these steps, I also did too much research to achieve this and finally the working stuff is here. I made these settings in two of my apps.

更重要的一件事。在我实施这些步骤的过程中,我也做了太多的研究来实现这一点,最后工作的东西就在这里。我在我的两个应用程序中进行了这些设置。

But while I was creating third app, I started from beginning and did not follow STEP 1and STEP 3and voila.... redirection works! So i don't think facebook developer account Native iOS Appsettings are necessary while you are making Facebook login in your app..

但是当我创建第三个应用程序时,我从头开始并且没有遵循第1步和第3步,瞧……重定向有效!所以我认为在您的应用程序中登录 Facebook 时,不需要facebook 开发者帐户本地 iOS 应用程序设置..

回答by Robin

I ran into another issue. I edited the raw Info.plist file and added:

我遇到了另一个问题。我编辑了原始 Info.plist 文件并添加了:

<key>URL Types</key>
<array>
    <dict>
        <key>URL Schemes</key>
        <array>
             <string>fbxxxxxxxxx</string>
        </array>
    </dict>
</array>

That didn't work because the keys are not 'URL Types', 'URL Schemes' and 'URL Identifier', it's 'CFBundleURLTypes' , 'CFBundleURLSchemes' and 'CFBundleURLIdentifier' instead. Xcode will rename that in the editor to 'URL Types', 'URL Schemes' and 'URL Identifier'.

这不起作用,因为键不是“URL 类型”、“URL 方案”和“URL 标识符”,而是“CFBundleURLTypes”、“CFBundleURLSchemes”和“CFBundleURLIdentifier”。Xcode 将在编辑器中将其重命名为“URL 类型”、“URL 方案”和“URL 标识符”。

To be sure select the Info.plist in xcode, right click->show raw keys/values.

为了确保在 xcode 中选择 Info.plist,右键单击 -> 显示原始键/值。

enter image description here

在此处输入图片说明

回答by Amir Latif

None of the above, or other answers on SO, worked for me, though I seemed to have the exact same issue.

尽管我似乎遇到了完全相同的问题,但上述所有答案或关于 SO 的其他答案都不适合我。

However, when I enabled "Deep Linking", it worked perfectly, so it may have been a combination of all of the above AND enabling that option too.

但是,当我启用“深度链接”时,它运行良好,因此它可能是上述所有内容的组合,并且也启用了该选项。