UWP项目接入Google登录网页版,遇到一个问题:点击登录打开的网页不在当前的WebView中加载而是打开了浏览器。
????如何解决呢?UWP WebView如何阻止自动打开浏览器?一文给出了答案。
不过参考资料是C#版的,而我们的项目使用的C++,因此需要调整下,CX版阻止WebView自动打开浏览器的代码如下:
void TKWinrtWeb::addLoginWeb(Platform::String^ text)
{
auto panel = cocos2d::GLViewImpl::sharedOpenGLView()->getPanel();
auto dispatcher = cocos2d::GLViewImpl::sharedOpenGLView()->getDispatcher();
cocos2d::Size visibleSize = cocos2d::Director::getInstance()->getOpenGLView()->getFrameSize();
if (dispatcher && panel)
{
// run on main UI thread
dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, ref new Windows::UI::Core::DispatchedHandler([this, visibleSize, text, panel]()
{
if (m_webView == nullptr)
{
m_webView = ref new Windows::UI::Xaml::Controls::WebView();
//m_webView->NavigateToString(text);
m_webView->Navigate(ref new Uri(text));
m_webView->NavigationStarting += ref new TypedEventHandler<WebView^, WebViewNavigationStartingEventArgs^>(this, &TKWinrtWeb::onStart);
m_webView->NavigationCompleted += ref new TypedEventHandler<WebView^, WebViewNavigationCompletedEventArgs^>(this, &TKWinrtWeb::onCompleted);
m_webView->LongRunningScriptDetected += ref new TypedEventHandler<WebView^, WebViewLongRunningScriptDetectedEventArgs^>(this, &TKWinrtWeb::onLongRunningScriptDetected);
m_webView->UnsafeContentWarningDisplaying += ref new TypedEventHandler<WebView^, Object^>(this, &TKWinrtWeb::onUnsafeContentWarning);
m_webView->NewWindowRequested += ref new TypedEventHandler<WebView^, WebViewNewWindowRequestedEventArgs^>(this, &TKWinrtWeb::onNewWindowRequested);
m_webView->ScriptNotify += ref new NotifyEventHandler(this, &TKWinrtWeb::onScriptNotify);
m_webView->Width = visibleSize.width;
m_webView->Height = visibleSize.height;
panel->Children->Append(m_webView);
int btn_w = 50;
int btn_h = btn_w;
Windows::UI::Xaml::Thickness ness = Windows::UI::Xaml::Thickness();
ness.Left = visibleSize.width - btn_w*1.5;
ness.Bottom = visibleSize.height - btn_h*1.5;
m_backBtn = ref new Windows::UI::Xaml::Controls::Button();
m_backBtn->Width = btn_w;
m_backBtn->Height = btn_h;
m_backBtn->Margin = ness;
m_backBtn->Content = "X";
m_backBtn->FontSize = 35;
m_backBtn->Click += ref new RoutedEventHandler(this, &TKWinrtWeb::onBtnClicked);
panel->Children->Append(m_backBtn);
}
}));
}
}
void TKWinrtWeb::onStart(WebView^ web, WebViewNavigationStartingEventArgs^ e)
{
}
void TKWinrtWeb::onCompleted(WebView^, WebViewNavigationCompletedEventArgs^ e)
{
}
void TKWinrtWeb::onLongRunningScriptDetected(WebView^, WebViewLongRunningScriptDetectedEventArgs^ e)
{
}
void TKWinrtWeb::onUnsafeContentWarning(WebView^, Platform::Object^ e)
{
}
//阻止打开浏览器的核心代码
using namespace Windows::Web::Http;
void TKWinrtWeb::onNewWindowRequested(WebView^ web, WebViewNewWindowRequestedEventArgs^ args)
{
args->Handled = true;
HttpMethod ^method = ref new HttpMethod("GET");
HttpRequestMessage^ req = ref new HttpRequestMessage(method, args->Uri);
web->NavigateWithHttpRequestMessage(req);
auto panel = cocos2d::GLViewImpl::sharedOpenGLView()->getPanel();
auto dispatcher = cocos2d::GLViewImpl::sharedOpenGLView()->getDispatcher();
cocos2d::Size visibleSize = cocos2d::Director::getInstance()->getOpenGLView()->getFrameSize();
if (dispatcher && panel)
{
// run on main UI thread
dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, ref new Windows::UI::Core::DispatchedHandler([this, visibleSize, web,args, panel]()
{
}));
}
}
//JS交互
void TKWinrtWeb::onScriptNotify(Object^ sender, NotifyEventArgs^ e)
{
if (e)
{
Platform::String ^content = e->Value;
TKSdkUwp::getInstance()->tellLogin(PlatformStrToStdStr(content));
removeWeb();
}
}
参考资料:
https://jingyan.baidu.com/article/90808022043919fd91c80fba.html
|