UBports Robot Logo UBports Forum
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    SOLVED: How do I get my app to launch the web browser?

    Scheduled Pinned Locked Moved App Development
    21 Posts 6 Posters 4.0k Views 3 Watching
    Loading More Posts
    • 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.
      • CraigC Offline
        Craig @lduboeuf
        last edited by

        @lduboeuf thank you. I downloaded the url-dispatcher package and read many of the files. I imagine I would need to run make on some of these files, put an #include in my existing main.cpp or main.h and then I can call a function that will launch the web browser? Are there any instructions for doing this? Details wont bore me, I need them.

        Thank you.

        1 Reply Last reply Reply Quote 0
        • dobeyD Offline
          dobey
          last edited by dobey

          First off, you should not use system(). If you must exec an external binary, you should use exec() family of calls instead.

          Are you using Qt at all? Or glib/gio?

          If you are using either of these, there are APIs to open URLs with the registered URL handler.

          CraigC 1 Reply Last reply Reply Quote 0
          • CraigC Offline
            Craig @dobey
            last edited by

            @dobey Thank you for your reply. Yes, with app confinement, I realized system() was not going to work.

            I'm using pure C++ and SDL in my app. That's it.

            I downloaded the url-dispatcher that @lduboeuf recommended.
            https://github.com/ubports/url-dispatcher

            However, it didn't come with any instructions.

            lduboeufL dobeyD 2 Replies Last reply Reply Quote 0
            • lduboeufL Offline
              lduboeuf @Craig
              last edited by

              @Craig Really not sure of the url-dispatcher thing. Hope i don't gave you wrong instruction πŸ™‚

              1 Reply Last reply Reply Quote 0
              • dobeyD Offline
                dobey @Craig
                last edited by

                @Craig url-dispatcher is the service which manages URL handlers on UT. It's not something you'd use directly.

                Try doing something like:

                #include <unistd.h>
                
                ...
                    execlp("xdg-open", MY_URL, NULL);
                ...
                

                If your code is actually C++, I'd suggest using nullptr instead of NULL here, but this should do what you want. And MY_URL would be the URL string you want to open. Also, if using C++11 or newer, you probably would want to do the execlp() call inside a std::thread as well.

                CraigC 1 Reply Last reply Reply Quote 0
                • CraigC Offline
                  Craig @dobey
                  last edited by

                  @dobey Thank you for your reply! I like your idea. It sounds simple. However, I tried it and it didn't work.

                  This works from adb shell:
                  morph-browser --desktop_file_hint=/usr/share/applications/morph-browser.desktop https://forums.ubports.com/

                  However, from C++, this did not work:
                  #include <unistd.h>
                  ...
                  execlp("morph-browser", "--desktop_file_hint=/usr/share/applications/morph-browser.desktop", "https://forums.ubports.com/", nullptr );

                  I also did "int error = execlp()..." and it always returned -1 (error).

                  1 Reply Last reply Reply Quote 0
                  • arubislanderA Offline
                    arubislander
                    last edited by arubislander

                    @Craig said in How do I get my app to launch the web browser?:

                    execlp("morph-browser", "--desktop_file_hint=/usr/share/applications/morph-browser.desktop", "https://forums.ubports.com/", nullptr );

                    Have you tried:

                    execlp("xdg-open","https://forums.ubports.com/", nullptr );
                    

                    as per @dobey 's instructions?

                    πŸ‡¦πŸ‡Ό πŸ‡³πŸ‡± πŸ‡ΊπŸ‡Έ πŸ‡ͺπŸ‡Έ
                    Happily running Ubuntu Touch
                    Google Pixel 3a (20.04 DEV)
                    JingPad (24.04 preview)
                    Meizu Pro 5 (16.04 DEV)

                    CraigC 1 Reply Last reply Reply Quote 0
                    • CraigC Offline
                      Craig @arubislander
                      last edited by

                      @arubislander Yes, I did try that first. It appears xdg-open isn't installed on Ubuntu Touch. Have you tried it from a C++ app? If you try it from adb shell it says, "bash: xdg-open: command not found".

                      1 Reply Last reply Reply Quote 0
                      • arubislanderA Offline
                        arubislander
                        last edited by

                        I must admit I haven't. Just checked, and indeed xdg-open is not installed. So the question becomes: What are the options for launching the default url handler?

                        πŸ‡¦πŸ‡Ό πŸ‡³πŸ‡± πŸ‡ΊπŸ‡Έ πŸ‡ͺπŸ‡Έ
                        Happily running Ubuntu Touch
                        Google Pixel 3a (20.04 DEV)
                        JingPad (24.04 preview)
                        Meizu Pro 5 (16.04 DEV)

                        1 Reply Last reply Reply Quote 0
                        • arubislanderA Offline
                          arubislander
                          last edited by

                          There is a thread somewhere on this forum concerning launching the browser from QML. I do not have the opportunity to search for it a.t.m. Might do so when I get home, if no one beats me to it. I am thinking something similar must be posible from C++ πŸ€”

                          πŸ‡¦πŸ‡Ό πŸ‡³πŸ‡± πŸ‡ΊπŸ‡Έ πŸ‡ͺπŸ‡Έ
                          Happily running Ubuntu Touch
                          Google Pixel 3a (20.04 DEV)
                          JingPad (24.04 preview)
                          Meizu Pro 5 (16.04 DEV)

                          1 Reply Last reply Reply Quote 0
                          • AppLeeA Offline
                            AppLee
                            last edited by

                            From QML, I have this working :

                             Action {
                                    iconName: "info"
                                    onTriggered: Qt.openUrlExternally("https://www.domoticz.com/")
                                  }
                            

                            I don't remember what to include to have Qt.openUrlExternally() in your C++ code.
                            Might help...

                            dobeyD 1 Reply Last reply Reply Quote 0
                            • dobeyD Offline
                              dobey @AppLee
                              last edited by

                              @AppLee One would need to use https://doc.qt.io/qt-5.9/qdesktopservices.html#openUrl

                              CraigC 1 Reply Last reply Reply Quote 1
                              • CraigC Offline
                                Craig @dobey
                                last edited by

                                @dobey You are right. I was testing QDesktopServices::openUrl when you posted this.

                                My C++/SDL app launches the morph web browser. Here's how to make a C++/SDL app launch the web browser in Ubuntu Touch:

                                Step 1:
                                You need to add these to your CMakeLists.txt:
                                (You may have more than one CMakeList.txt file and some of the code lines below will go in different CMakeList.txt files.)

                                (Add these lines below after "project(...)" and "cmake_minimum_required(..)")

                                find_package(Qt5Core)
                                find_package(Qt5Qml)
                                find_package(Qt5Quick)
                                

                                (Add this line below after "execute_process(... OUTPUT_VARIABLE ARCH_TRIPLET ...)" )

                                set(QT_IMPORTS_DIR "lib/${ARCH_TRIPLET}")
                                

                                (Add this line below after "add_executable(... main.cpp ...)" )

                                qt5_use_modules(YOURPROJECTNAMEHERE Gui Qml Quick QuickControls2)
                                

                                Replace YOURPROJECTNAMEHERE with your project name.

                                Step 2:
                                Put these headers near the top of main.h/main.cpp:

                                #include <QGuiApplication>
                                #include <QDesktopServices>
                                #include <QUrl>
                                

                                Put this at the top of main():

                                QGuiApplication *app = new QGuiApplication(argc, (char**)argv);
                                

                                Near the end of main(), replace "return 0;" with this:

                                return app->exec();
                                

                                Step 3:
                                And now for the magic... Somewhere in your C++ code, make your link:

                                if ( event.type == SDL_FINGERDOWN )
                                {
                                    QDesktopServices::openUrl(QUrl("https://forums.ubports.com"));
                                }
                                

                                If you have any comments, suggestions or ways to improve this code, let me know. Thank you all for your help.

                                dobeyD 1 Reply Last reply Reply Quote 3
                                • dobeyD Offline
                                  dobey @Craig
                                  last edited by

                                  @Craig said in How do I get my app to launch the web browser?:

                                  find_package(Qt5Core)
                                  find_package(Qt5Qml)
                                  find_package(Qt5Quick)

                                  You don't need Quick or Qml here. Though I think you will need Gui. Also it'd be better to do:

                                  find_package(Qt5 COMPONENTS Core Gui)
                                  

                                  set(QT_IMPORTS_DIR "lib/${ARCH_TRIPLET}")

                                  You don't need this, as you aren't using any QML parts.

                                  qt5_use_modules(YOURPROJECTNAMEHERE Gui Qml Quick QuickControls2)

                                  For this, you should instead just add

                                  Qt5::Core
                                  Qt5::Gui
                                  

                                  to the list of libraries you pass to the target_link_libraries() call.

                                  I'm also not sure if you need a QApplication thread running at all for this. If possible, it's probably better to avoid having to integrate the Qt main loop and SDL. If it is needed though, it would probably be better to use QCoreApplication or QApplication instead, if possible.

                                  CraigC 1 Reply Last reply Reply Quote 1
                                  • CraigC Offline
                                    Craig @dobey
                                    last edited by Craig

                                    @dobey I made the simplifications to the CMakeLists.txt file that you suggested. It's working, thank you!

                                    In the last part of your previous message, you wrote:

                                    I'm also not sure if you need a QApplication thread running at all for this. If possible, it's probably better to avoid having to integrate the Qt main loop and SDL. If it is needed though, it would probably be better to use QCoreApplication or QApplication instead, if possible.

                                    I think you meant something other than QApplication the second time you mentioned it. Which library instead of QApplication?

                                    I tried it with these in main():

                                    QCoreApplication a(argc, args);
                                    return a.exec();
                                    

                                    Although clickable compiled and it ran, QDesktopServices::openUrl() did not work at all. The browser would not launch.

                                    dobeyD 1 Reply Last reply Reply Quote 0
                                    • dobeyD Offline
                                      dobey @Craig
                                      last edited by

                                      @Craig said in How do I get my app to launch the web browser?:

                                      Although clickable compiled and it ran, QDesktopServices::openUrl() did not work at all. The browser would not launch.

                                      OK. I suppose you do need to use QGuiApplication then.

                                      1 Reply Last reply Reply Quote 0
                                      • CraigC Offline
                                        Craig
                                        last edited by

                                        So, just to recap, here's how to make a C++ app launch the web browser in Ubuntu Touch:

                                        Step 1:
                                        You need to add/change these in your CMakeLists.txt:

                                        find_package(Qt5 COMPONENTS Core Gui)
                                        target_link_libraries(YOURPROJECTNAMEHERE ... Qt5::Core Qt5::Gui)
                                        

                                        Step 2:
                                        Put these headers near the top of main.h/main.cpp:

                                        #include <QGuiApplication>
                                        #include <QDesktopServices>
                                        #include <QUrl>
                                        

                                        Put this at the top of main():

                                        QGuiApplication *app = new QGuiApplication(argc, (char**)argv);
                                        

                                        Near the end of main(), replace "return 0;" with this:

                                        return app->exec();
                                        

                                        Step 3:
                                        Somewhere in your C++ code, when the app user clicks on your button, call this:

                                        QDesktopServices::openUrl(QUrl("https://forums.ubports.com"));
                                        
                                        lduboeufL 1 Reply Last reply Reply Quote 3
                                        • lduboeufL Offline
                                          lduboeuf @Craig
                                          last edited by

                                          @Craig thanks for sharing πŸ™‚

                                          1 Reply Last reply Reply Quote 0
                                          • L Offline
                                            L-00997
                                            last edited by

                                            This post is deleted!
                                            1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post