gpt4 book ai didi

ios - Facebook 登录按钮在 WKWebView 中不起作用

转载 作者:IT王子 更新时间:2023-10-29 05:46:53 29 4
gpt4 key购买 nike

我们正在开发适用于 iOS 8.0 及更高版本的应用程序。此应用程序需要身份验证(或用户注册)。在网站上,我们使用 html 表单来注册和登录用户。这些表格是响应式的。用户还可以单击“使用 Facebook 登录按钮”以使用 FB 凭据登录。这个按钮是用 FB 的 SDK for Javascript (v2.4) 实现的

因此,我们不想在我们的 iOS 应用程序上实现 native 屏幕以进行注册和登录,但我们宁愿实现一个带有 WKWebView 元素的 View Controller 来处理这种浏览体验。

但是,我们注意到,当用户点击“使用 Facebook 登录”按钮时,没有任何反应。 FB 打开以询问用户登录和授予权限的典型弹出窗口永远不会出现。

下面是我们如何初始化 WKWebView:

class LoginViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {
var webView: WKWebView
@IBOutlet weak var cancelButton: UIButton!

override func viewDidLoad() {
super.viewDidLoad()

view.addSubview(webView)
webView.setTranslatesAutoresizingMaskIntoConstraints(false)
let height = NSLayoutConstraint(item: webView, attribute: .Height, relatedBy: .Equal, toItem: view, attribute: .Height, multiplier: 1, constant: 0)
let width = NSLayoutConstraint(item: webView, attribute: .Width, relatedBy: .Equal, toItem: view, attribute: .Width, multiplier: 1, constant: 0)
view.addConstraints([height, width])

loadDefaultUrl()
}

required init(coder aDecoder: NSCoder){
let config = WKWebViewConfiguration()
config.preferences.javaScriptCanOpenWindowsAutomatically = true
self.webView = WKWebView(frame: CGRectZero, configuration: config)
super.init(coder: aDecoder)
webView.navigationDelegate = self
webView.UIDelegate = self
}

非常感谢有关如何使 FB 登录按钮在 WKWebView 中工作的任何指示(或者,如果可行,在 UIWebView 中)。

最佳答案

回答有点晚,但我最近遇到了同样的问题,但找不到任何令人满意的答案。

我有一个解决方案,但在 iOS 9.0 中,通过实现 WKUIDelegate 的两种方法,您已经将其注册为您的 webView。 Facebook JS 库的身份验证流程将首先打开一个弹出窗口,允许用户输入其凭据、允许信息传输等,然后该窗口将关闭并返回到父窗口。

为了允许该流程,您首先需要使用以下方法拦截打开弹出窗口的调用:

func webView(webView: WKWebView, createWebViewWithConfiguration configuration: WKWebViewConfiguration, forNavigationAction navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
// A nil targetFrame means a new window (from Apple's doc)
if (navigationAction.targetFrame == nil) {
// Let's create a new webview on the fly with the provided configuration,
// set us as the UI delegate and return the handle to the parent webview
let popup = WKWebView(frame: self.view.frame, configuration: configuration)
popup.UIDelegate = self
self.view.addSubview(popup)
return popup
}
return nil;
}

现在这已经完成,新请求将自动加载到刚刚创建的“弹出”WKWebView 中。下一步是确保在流程完成后关闭它。由于我们将 self 注册为 popup.UIDelegate,我们可以在 View 将被外部身份验证流程关闭时将其删除:

func webViewDidClose(webView: WKWebView) {
// Popup window is closed, we remove it
webView.removeFromSuperview()
}

希望对您有所帮助。

关于ios - Facebook 登录按钮在 WKWebView 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31988121/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com