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

随声附和 提交于 2019-12-23 04:16:14

问题


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.


回答1:


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>


来源:https://stackoverflow.com/questions/28822086/best-way-to-get-source-url-of-custom-ios-scheme-in-qml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!