WKWebView将不会打开社交媒体登录弹出窗口 [英] WKWebView won't open social media login popups

查看:104
本文介绍了WKWebView将不会打开社交媒体登录弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一个WKWebView.除了我显示的网站上有社交媒体登录按钮之外,其他所有内容都运行顺利.问题是,它们显示(或尝试显示)一个弹出窗口,您可以在其中允许其访问您的社交媒体帐户.我对此进行了研究,并尝试了一些方法,但似乎都不合适.这是我尝试过的:

I have a WKWebView in my app. Everything is running smooth except the website I show has the social media logon buttons. The problem is, they display (or try to display) a popup where you allow it to have access to your social media account. I have researched this and tried a few things, but none seem to fit. Here is what I tried:

在viewDidLoad中,我尝试在init上启用Javascript:

in viewDidLoad, I tried to enable Javascript on the init:

WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
WKPreferences *thePreferences = [[WKPreferences alloc] init];
thePreferences.javaScriptCanOpenWindowsAutomatically = YES;
thePreferences.javaScriptEnabled = YES;
theConfiguration.preferences = thePreferences;
self.wkWeb = [[WKWebView alloc] initWithFrame:screenRect configuration:theConfiguration];

但是,这并没有帮助我.然后,我尝试与代表们一起玩,走那条路.我尝试使用createWebViewWithConfiguration方法,但是这似乎有些矫kill过正,因为我必须过滤掉它们是否位于登录URL上,然后配置一个弹出窗口并显示它.然后这仍然行不通.如果不是大型机逻辑,我也来这里,取消请求,然后在主视图中重新加载它,这是一种简单的单行修复程序,因为它膨胀为20多个行.

However, this didn't help me. Then I tried to play with the delegates and go that route. I tried playing around with the createWebViewWithConfiguration method, but that seems like overkill because I had to filter out if they are at the login URL then configure a popup and display it. And then this still wasn't working. I also come in here for the if not main frame logic, cancel the request and reload it in the main view, and that is an easy one-line fix where as this was ballooning into 20+ lines.

这似乎是一个常见问题,但是我似乎找不到很多信息.有什么想法吗?

This seems like a common problem, but I can't seem to find a lot of information out there. Any ideas?

玩完Armands的答案后,我变得更近了.现在这是我的createWebViewWithConfig方法,该方法仅显示白色屏幕覆盖.就像我需要告诉新的弹出窗口来加载内容一样.另外-我假设我可以将此模式窗口放置在当前的.m文件中,并且它不需要一个全新的文件?

After playing around with Armands answer, I get closer. This is my createWebViewWithConfig method now, which just displays a white screen overlay. It's like I need something to tell the new popup to load the content. Also - I assume I can place this modal window in my current .m file and it doesn't need a completely new file?

NSURL *url = navigationAction.request.URL;
if(!navigationAction.targetFrame.isMainFrame && [url.absoluteString rangeOfString:@"initiate_"].location != NSNotFound) {
    //open new modal webview
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
    config.processPool = [appDelegate getSharedPool];

    self.popupLogin = [[WKWebView alloc] initWithFrame:self.wkWeb.bounds configuration:config];
    self.popupLogin.frame = CGRectMake(self.wkWeb.frame.origin.x,
                                       self.wkWeb.frame.origin.y,
                                       self.wkWeb.frame.size.width,
                                       self.wkWeb.frame.size.height);
    self.popupLogin.navigationDelegate = self;
    self.popupLogin.UIDelegate = self;
    [webView addSubview:self.popupLogin];
    [self.popupLogin loadRequest:navigationAction.request];
    return nil;
}

推荐答案

这些登录按钮尝试打开一个新的选项卡,WKWebView不支持该选项卡.据我所知,只有一种方法可以做到这一点.

Those login buttons tries to open a new tab, which is not supported by WKWebView. As far as I know, there's only one way to do this.

首先添加WKUIDelegate方法:

-(WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures
{
    NSURL *url = navigationAction.request.URL;
    if (navigationAction.targetFrame == nil && [url.absoluteString rangeOfString:@"facebook.com/dialog"].location != NSNotFound) {
        //Open new modal WKWebView
    }
    return nil;
}

在模式WKWebView中添加以下内容:

In the modal WKWebView add this:

-(void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler
{
    NSURL *url = navigationResponse.response.URL;
    if ([url.absoluteString rangeOfString:@"close_popup.php"].location != NSNotFound) {
        //Close viewController
    }
    decisionHandler(WKNavigationResponsePolicyAllow);
}

close_popup.php用于Facebook.如果您需要支持多个社交网络,请在其网址中找到一些独特的内容.

close_popup.php is for Facebook. If you need to support multiple social networks, find something unique in their URLs.

要使其正常工作,您将必须在WKWebViews之间共享Cookie.为此,您必须使用共享的WKProcessPool来初始化它们. 我更喜欢将其存储在AppDelegate中.

In order for this to work, you will have to share Cookies between WKWebViews. To do this you have to init them with shared WKProcessPool. I prefer to store it in AppDelegate.

可以这样创建WKWebView:

WKWebView can be created like this:

- (void)createWebView
{
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
    config.processPool = delegate.webViewProcessPool;

    self.webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:config];
    self.webView.UIDelegate = self;
    self.webView.navigationDelegate = self;
    [self.view addSubview:self.webView];
}

在AppDelegate中只是延迟加载它:

In AppDelegate just lazy load it:

-(id)webViewProcessPool
{
    if (!_webViewProcessPool) {
        _webViewProcessPool = [[WKProcessPool alloc] init];
    }
    return _webViewProcessPool;
}

这篇关于WKWebView将不会打开社交媒体登录弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆