简体   繁体   中英

best way to get source URL of custom iOS scheme in QML

I am new to Qt and QML and am trying to build my first App for iOS. I need to get the contents of a custom URL Scheme that I defined in info.plist (myApp://). The URL scheme works and the apps opens when I open any url myApp://xyz. Now I need to get the contents of the URL (the xyz).

Trying to do that for the past days and not succeeding and also not finding a good manual on how to do it best, I ask here. What I understood so far: Some people create their own application delegates. There are few examples on github that use older Qt versions (I use 5.4), but I did not manage to integrate them into my app and it seems rather complex for this easy thing. Also, as I see here: https://github.com/qtproject/qtbase/blob/stable/src/plugins/platforms/ios/qiosapplicationdelegate.mm the URL is somehow already handled by Qt. I am not sure if Qt Desktop Services http://doc.qt.io/qt-5/QDesktopServices.html , handle it, but (it seems) it is not available from QML.

What's the best way to do that? Thanks a lot.

I finally made it work using QDesktopServices and a signal for QML.

HandleURL.h

#include <QDesktopServices>

#ifndef HANDLEURL
#define HANDLEURL

class HandleURL : public QObject
{
    Q_OBJECT

signals:
    void incomingURL(QString path);

public slots:
     void handleURL(const QUrl &url);
};

#endif

HandleURL.cpp emit the signal

void HandleURL::handleURL(const QUrl &url)
{
   emit incomingURL(url.toString());
}

In main.cpp, setURLHandler and create QML Context Property

HandleURL *URLHandler = new HandleURL();
QDesktopServices::setUrlHandler("myscheme", URLHandler, "handleURL");
engine.rootContext()->setContextProperty("URLHandler", URLHandler);

In QML, listen to Signal

Connections{
            target: URLHandler;
            onIncomingURL: { console.log("Incoming Signal: "+path)
            }
        }

plist.info

<key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleURLName</key>
            <string>com.myorg.mobile1</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>myscheme</string>
            </array>
        </dict>
    </array>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM