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

[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 24 Aug 2015, 09:51 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.

    P 1 Reply Last reply 24 Aug 2015, 10:59
    0
    • J johnc
      24 Aug 2015, 09:51

      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.

      P Offline
      P Offline
      p3c0
      Moderators
      wrote on 24 Aug 2015, 10:59 last edited by
      #2

      @johnc Did you try onUrlChanged ?

      157

      J 1 Reply Last reply 24 Aug 2015, 11:19
      0
      • P p3c0
        24 Aug 2015, 10:59

        @johnc Did you try onUrlChanged ?

        J Offline
        J Offline
        johnc
        wrote on 24 Aug 2015, 11:19 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 25 Aug 2015, 02:21 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
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 25 Aug 2015, 19:34 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

            1/5

            24 Aug 2015, 09:51

            • Login

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