我尝试使用自定义登录按钮实现 Facebook 登录,我已完成所有设置步骤,当我点击该按钮时,会出现 Web View 并要求我提供权限。
但是,如果我点击“完成”(位于 Web View Controller 的左上角),则会调用完成 block 但是当我点击“取消”或“确定”时,Web View Controller 会被关闭,但完成 block 永远不会被调用?强> 这是我使用的代码:
let fbLoginManager = FBSDKLoginManager()
fbLoginManager.loginBehavior = .Native
fbLoginManager.logInWithReadPermissions(["email"], fromViewController: self) { (result, error) in
// put a breakpoint here but it won't stop here if I tap OK
if error != nil {
print(error.localizedFailureReason)
} else if result.isCancelled {
// dismiss view
} else {
let token = result.token.tokenString
// do something with the token
}
}
编辑 1
这是 AppDelegate 中的 open url 方法:
func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
return application(app, openURL: url, sourceApplication: nil, annotation: [:])
}
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
if url.scheme == Key.FACEBOOK_URL_SCHEME {
let handled = FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
return handled
}
let dynamicLink = FIRDynamicLinks.dynamicLinks()?.dynamicLinkFromCustomSchemeURL(url)
if let dynamicLink = dynamicLink {
if let url = dynamicLink.url {
if let paths = url.pathComponents{
if paths.count > 2 {
let slug = paths[2]
if let book = AppManager.instance.findBookBySlug(slug) {
// Open detail book
self.openDetailBook(book)
} else {
self.openDetailBookWithSlug(slug)
}
}
}
}
}
if url.scheme == Key.DEEP_LINK_URL_SCHEME {
if let paths = url.pathComponents, host = url.host {
if host == "truyen" {
if paths.count > 1 {
let slug = paths[1]
if let book = AppManager.instance.findBookBySlug(slug) {
// Open detail book
self.openDetailBook(book)
} else {
self.openDetailBookWithSlug(slug)
}
}
}
}
}
return true
}
最佳答案
如果您遇到这些情况,请自行调试,因为 Facebook SDK 是开源的。您看到的窗口是 SFSafariViewController。当您点击任何这些按钮时,回调将由 SFSafariViewController 处理。在 Facebook sdk 中查找 SFSafariViewController
delegate
方法。
调用登录代码后下断点,一步步往下走,就会到使用SFSafariViewController的类。
在其中一个delegate
方法中,您会发现对 open url 方法的调用。
在应用程序委托(delegate)方法中实现相同的打开 URL 方法。
在您的情况下,问题可能是使用了错误的 openURL 方法。正确的应该是
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])
}
关于ios - Facebook 登录完成 block 未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38633729/