Lui*_*ado 9 ios facebook-javascript-sdk swift
我们正在开发iOS 8.0及更高版本的应用程序.此应用程序需要身份验证(或用户注册).在网站上,我们使用html表单注册和登录用户.这些形式是敏感的.用户还可以单击"使用Facebook登录"按钮以使用FB凭据登录.这个按钮是用FB的SDK for Javascript(v2.4)实现的
因此,我们不希望在我们的iOS应用程序上实现注册和日志的本机屏幕,但我们宁愿实现一个带有WKWebView元素的视图控制器来处理这种浏览体验.
但是,我们注意到,当用户点击"使用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
}
Run Code Online (Sandbox Code Playgroud)
非常感谢有关如何使FB登录按钮在WKWebView(或者,如果可行的话,在a内UIWebView)工作的任何指针.
小智 15
回答有点晚,但我最近遇到了同样的问题,找不到任何令人满意的答案.
我有一个解决方案,但通过实现两种方法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;
}
Run Code Online (Sandbox Code Playgroud)
现在,完成此操作后,新请求将自动加载到WKWebView刚刚创建的"弹出窗口" 中.下一步是确保在流程完成后关闭它.由于我们注册self为popup.UIDelegate,我们可以在外部认证流程关闭时删除视图:
func webViewDidClose(webView: WKWebView) {
// Popup window is closed, we remove it
webView.removeFromSuperview()
}
Run Code Online (Sandbox Code Playgroud)
我希望这有帮助.
小智 6
这是由 WKWebView 使用 target=_blank 阻塞链接引起的。解决方案是捕获这种情况,而是直接在视图中加载链接,如下所示>
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
if navigationAction.targetFrame?.isMainFrame == nil {
webView.load(navigationAction.request)
}
return nil
}
Run Code Online (Sandbox Code Playgroud)
来自此处的解决方案为什么 WKWebView 没有打开 target="_blank" 的链接
来自bernyfox的精彩回答。经过几天的寻找解决方案后,它对我有用。
这是objective-c版本:
- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures
{
if (navigationAction.targetFrame == nil) {
WKWebView *popup = [[WKWebView alloc]initWithFrame:self.view.frame configuration:configuration];
popup.UIDelegate = self;
[self.view addSubview:popup];
return popup;
}
}
-(void) webViewDidClose:(WKWebView *)webView{
// Popup window is closed, we remove it
[webView removeFromSuperview];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4652 次 |
| 最近记录: |