且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

WKWebView将不会打开社交媒体登录弹出窗口

更新时间:2023-12-03 22:44:58

这些登录按钮尝试打开一个新的选项卡,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;
}