[SOLVED] QtWebView - iOS - Capture Links



  • I have a QtWebView in my app to display some embedded pages.
    For links that go to external sites, I need to force these out to the default browser.

    I tried doing something like

                    onLoadProgressChanged: {
                                        console.log(result);
                                        if( url.toString().indexOf("https://yahoo.com") === 0 )
                                        {
                                            webPageItem.stop();
                                            Qt.openUrlExternally(url);
                                        }
                    }
    

    The first problem is that on Qt 5.5 it says onLoadingChanged is not available in Qt 5.5, even though it was in 5.4. So I am using onLoadProgressChanged.

    The next problem is that the URL shows the current page. Not the page that is loading. So the URL is always one page click behind.

    Does anyone know a way I can trap any page being loaded for specific flags and pass them out to an external browser.


  • Moderators

    @johnc Did you try onUrlChanged ?



  • @p3c0

    Yeah I tried that also. The issue with this one is that it only fires after the page has finished loading.

    Just tried with a timer also. I set it to output the current URL every 1ms.
    The URL property only changes when the page has finished loading.



  • I have worked out how to get it going but it requires a modification to the QtWebView source.

    In qwebview_ios.mm add the following function

    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
    {
      if (navigationType == UIWebViewNavigationTypeLinkClicked) {
    	Q_EMIT qIosWebViewPrivate->urlChanged(QUrl::fromNSURL(request.mainDocumentURL));
        }
    
        return YES;
    }
    

    Then in the QML

    WebView {
        onUrlChanged: {
                    if( url.toString().indexOf("some url") !== 0 &&
                        url.toString().indexOf("about:blank") !== 0)
                    {
                        webPageItem.stop();
                        Qt.openUrlExternally(url);
                    }
                }
    }
    

    This then allows you to display all the embedded pages in your app, and automatically break out to Safari if the user selects an external link.


  • Lifetime Qt Champion

    Hi,

    That might be something interesting to contribute back to Qt :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.