在我们的 iOS 应用程序中,我们有一个 UIWebView,它显示我们域中的 Web 内容,该域具有 Facebook 评论模块。评论模块要求用户使用 facebook 登录。当用户点击登录按钮时,他们将进入登录流程,但绝不会重定向回我们的页面。他们最终会进入一个 FB 拥有的页面,该页面只告诉用户“您现在已登录”。
重现步骤:
- 在 iOS 应用程序中创建一个 UIWebView,并在您拥有的某个域(例如 http://foo.com/test.htm)上托管的页面上托管 Facebook 评论模块。
- 单击评论模块上的“登录”按钮,注意您将重定向到 FB 登录。
- 使用有效的 FB 凭据登录并观察会发生什么。
登录后(第 3 步)我希望在成功验证后,您将被重定向回原始页面(例如 http://foo.com/test.htm),以便您可以继续进行交互。然而,这并没有发生。
相反,您在一个 FB 拥有的页面上,该页面只显示类似“您现在已登录”之类的内容,而您被困在那里。没有重定向发生。
这确实是一个错误还是我应该做些什么来确保重定向发生?
最佳答案
如果您只支持 iOS 8 及更高版本,您可以使用 WKWebView
它已经实现了 @kabuko 描述的功能:
// Container view including the main WKWebView
var container : UIView?
var popupWebView : WKWebView?
override func viewDidLoad() {
super.viewDidLoad()
let prefs = WKPreferences()
prefs.javaScriptEnabled = true
// allow facebook to open the login popup
prefs.javaScriptCanOpenWindowsAutomatically = true
let config = WKWebViewConfiguration()
config.preferences = prefs
webView = WKWebView(frame: container.frame, configuration: config)
webView?.UIDelegate = self
webView?.navigationDelegate = self
}
// callback if the content of the webView wants to create a new window
func webView(webView: WKWebView, createWebViewWithConfiguration configuration: WKWebViewConfiguration, forNavigationAction navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
// create new popup webview and add it to the view hierarchy
popupWebView = WKWebView(frame: container.frame, configuration: configuration)
container.addSubview(popupWebView!)
return popupWebView
}
func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
// if the main webView loads a new page (e.g. due to succesful facebook login)
// remove the popup
if (popupWebView != nil) {
popupWebView?.removeFromSuperview()
popupWebView = nil
}
}
关于ios - UIWebView 中的 Facebook 身份验证不会重定向回我网站上要求身份验证的原始页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8025082/