Facebook SDK 登录永远不会在 iOS 9 上回调我的应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32299271/
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
Facebook SDK login never calls back my application on iOS 9
提问by Hesham
I've followed thisguide to update my application to use Facebook SDK 4.6 to work properly when built with the iOS 9 SDK.
我已按照本指南更新我的应用程序,以便在使用 iOS 9 SDK 构建时使用 Facebook SDK 4.6 正常工作。
When I tap the login button now, a Safari view controller gets presented (shouldn't it redirect to the Facebook app?), but after accepting permission the Safari view controller is never dismissed. It loads a new blank page and sits there doing nothing. If I tap the Done button, the returned FBSDKLoginManagerLoginResult
's isCancelled
is true.
当我现在点击登录按钮时,会显示一个 Safari 视图控制器(它不应该重定向到 Facebook 应用程序吗?),但是在接受许可后,Safari 视图控制器永远不会被解除。它加载一个新的空白页面,然后坐在那里什么也不做。如果我点击 Done 按钮,返回的FBSDKLoginManagerLoginResult
'sisCancelled
为 true。
Is it normal that the SDK is choosing the Safari view controller over the Facebook app? And why am I not getting callbacks after login is complete?
SDK 选择 Safari 视图控制器而不是 Facebook 应用程序是否正常?为什么我在登录完成后没有收到回调?
回答by Hesham
Turns out that on iOS 9 when UIApplicationDelegate
's application:openURL:options:
is implemented, application:openURL:sourceApplication:annotation:
will not get called.
事实证明,在 iOS 9上实现UIApplicationDelegate
's时application:openURL:options:
,application:openURL:sourceApplication:annotation:
不会被调用。
So what I had to do is call FBSDKApplicationDelegate
's application:openURL:sourceApplication:annotation:
from UIApplicationDelegate
's application:openURL:options:
所以我必须做的是调用FBSDKApplicationDelegate
's application:openURL:sourceApplication:annotation:
from UIApplicationDelegate
'sapplication:openURL:options:
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options {
return [[FBSDKApplicationDelegate sharedInstance] application:app
openURL:url
sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
annotation:options[UIApplicationOpenURLOptionsAnnotationKey]];
}
回答by David
For Swift this was working for me (add it in AppDelegate.swift):
对于 Swift,这对我有用(在 AppDelegate.swift 中添加):
@available(iOS 9.0, *)
func application(application: UIApplication,openURL url: NSURL, options: [String: AnyObject]) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application,
openURL: url,
sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as! String,
annotation: options [UIApplicationOpenURLOptionsAnnotationKey])
}
and
和
@available(iOS, introduced=8.0, deprecated=9.0)
func application(application: UIApplication,openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application,
openURL: url,
sourceApplication: sourceApplication!,
annotation: annotation)
}
In each case remember to add import FBSDKCoreKit
with the other import
statements.
在每种情况下,请记住添加import FBSDKCoreKit
其他import
语句。
Its basically what Google SignIn uses. If its still not working you need to set the delegates and your info.plist like it is specified in the FaceBook Docs. I hope this helps!
它基本上是 Google SignIn 使用的内容。如果它仍然无法正常工作,您需要像在 FaceBook Docs 中指定的那样设置委托和您的 info.plist。我希望这有帮助!
回答by Johnny Tsoi
For Swift 3 & Facebook SDK 4.16.0:
对于 Swift 3 和 Facebook SDK 4.16.0:
Add the following code to AppDelegate.swift
将以下代码添加到 AppDelegate.swift
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, open: url, sourceApplication: sourceApplication, annotation: annotation)
}
回答by CommittedEel
For those of you experiencing this same issue with iOS10 I added this:
对于那些在 iOS10 中遇到同样问题的人,我添加了以下内容:
@available(iOS 9.0, *)
func application(_ application: UIApplication, openURL url: URL, sourceApplication: String?, annotation: AnyObject) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, open: url, sourceApplication: sourceApplication, annotation: annotation)
}
This should work but as for now its just a workaround
这应该可行,但就目前而言,它只是一种解决方法
回答by RichAppz
I have just come across this issue, thanks @Hesham for the fix.
我刚刚遇到这个问题,感谢@Hesham 的修复。
Here is the Swift3 fix:
这是 Swift3 修复:
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(
app,
open: url,
sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String,
annotation: options[UIApplicationOpenURLOptionsKey.annotation])
}
回答by Abdullah Tahan
for xamarin users :
对于 xamarin 用户:
public override bool OpenUrl(UIApplication application, NSUrl url, string sourceApplication, NSObject annotation)
{
OnUrlProtocolDetected(url.ToString());
if (url.AbsoluteString.StartsWith("fb"))
{
return ApplicationDelegate.SharedInstance.OpenUrl(application, url, sourceApplication, annotation);
}
}
回答by victor
For total swift newbies like myself, the problem was an alternate implementation of the application() method:
对于像我这样的 swift 新手来说,问题是 application() 方法的替代实现:
Working version:
工作版本:
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(
application,
open: url,
sourceApplication: sourceApplication,
annotation: annotation)
}
Non working version (Copied from somewhere )
非工作版本(从某处复制)
func application(application: UIApplication, openURL url: URL, sourceApplication: String?, annotation: Any) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(
application,
open: url,
sourceApplication: sourceApplication,
annotation: annotation)
}
回答by Saheb Roy
Did you follow these steps?
你有没有按照这些步骤操作?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
return [[FBSDKApplicationDelegate sharedInstance] application:application
didFinishLaunchingWithOptions:launchOptions];
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [[FBSDKApplicationDelegate sharedInstance] application:application
openURL:url
sourceApplication:sourceApplication
annotation:annotation
];
}
I think you are missing the point where you got to call the applications delegate in App delegate. The second method is vital coz it gives the callback to your application about the login did finish in safari by the user
我认为您错过了在 App 委托中调用应用程序委托的要点。第二种方法很重要,因为它为您的应用程序提供了关于用户在 safari 中完成登录的回调
回答by Jakub Truhlá?
I was using the old 3.24version and on iOS 9
I was facing a similar problem.
我使用的是旧的 3.24版本,并且iOS 9
遇到了类似的问题。
Found that in appDelegate
method - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
发现在appDelegate
方法- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
was
曾是
return [FBSession.activeSession handleOpenURL:url];
return [FBSession.activeSession handleOpenURL:url];
instead of
代替
return [FBAppCall handleOpenURL:url sourceApplication:sourceApplication];
return [FBAppCall handleOpenURL:url sourceApplication:sourceApplication];
回答by Pearson Basham
For anyone looking for Swift assistance on this very issue the following worked for me.
对于在这个问题上寻求 Swift 帮助的任何人,以下内容对我有用。
func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(app, openURL: url, sourceApplication: UIApplicationOpenURLOptionsSourceApplicationKey, annotation: UIApplicationOpenURLOptionsAnnotationKey)
}