Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. [SOLVED] QtWebView - iOS - Capture Links
QtWS25 Last Chance

[SOLVED] QtWebView - iOS - Capture Links

Scheduled Pinned Locked Moved Mobile and Embedded
qtwebviewiosqml
5 Posts 3 Posters 2.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    johnc
    wrote on last edited by johnc
    #1

    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.

    p3c0P 1 Reply Last reply
    0
    • J johnc

      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.

      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      @johnc Did you try onUrlChanged ?

      157

      J 1 Reply Last reply
      0
      • p3c0P p3c0

        @johnc Did you try onUrlChanged ?

        J Offline
        J Offline
        johnc
        wrote on last edited by johnc
        #3

        @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.

        1 Reply Last reply
        0
        • J Offline
          J Offline
          johnc
          wrote on last edited by johnc
          #4

          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.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

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

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved