├── README.md ├── qml.qrc ├── .gitignore ├── deployment.pri ├── ShareUtils-QML.pro ├── ios ├── iosshareutils.h └── iosshareutils.mm ├── android ├── androidshareutils.h └── androidshareutils.cpp ├── main.cpp ├── main.qml ├── shareutils.cpp ├── shareutils.h └── android_data └── src └── com └── lasconic └── QShareUtils.java /README.md: -------------------------------------------------------------------------------- 1 | See http://blog.lasconic.com/share-on-ios-and-android-using-qml -------------------------------------------------------------------------------- /qml.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | main.qml 4 | 5 | 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | #Exclude android directories 2 | android/assets 3 | android/bin 4 | android/gen 5 | android/libs 6 | android/obj 7 | 8 | # Exclude Qt creator user files 9 | CMakeLists.txt.user 10 | *.pro.user 11 | 12 | # Exclude temp nibs and swap files 13 | *~.nib 14 | *.swp 15 | 16 | # Exclude OS X folder attributes 17 | .DS_Store 18 | 19 | # Exclude user-specific XCode 3 and 4 files 20 | *.mode1 21 | *.mode1v3 22 | *.mode2v3 23 | *.perspective 24 | *.perspectivev3 25 | *.pbxuser 26 | *.xcworkspace 27 | xcuserdata 28 | 29 | -------------------------------------------------------------------------------- /deployment.pri: -------------------------------------------------------------------------------- 1 | android-no-sdk { 2 | target.path = /data/user/qt 3 | export(target.path) 4 | INSTALLS += target 5 | } else:android { 6 | x86 { 7 | target.path = /libs/x86 8 | } else: armeabi-v7a { 9 | target.path = /libs/armeabi-v7a 10 | } else { 11 | target.path = /libs/armeabi 12 | } 13 | export(target.path) 14 | INSTALLS += target 15 | } else:unix { 16 | isEmpty(target.path) { 17 | qnx { 18 | target.path = /tmp/$${TARGET}/bin 19 | } else { 20 | target.path = /opt/$${TARGET}/bin 21 | } 22 | export(target.path) 23 | } 24 | INSTALLS += target 25 | } 26 | 27 | export(INSTALLS) 28 | -------------------------------------------------------------------------------- /ShareUtils-QML.pro: -------------------------------------------------------------------------------- 1 | TEMPLATE = app 2 | 3 | QT += qml quick widgets 4 | 5 | SOURCES += main.cpp 6 | 7 | RESOURCES += qml.qrc 8 | 9 | # Additional import path used to resolve QML modules in Qt Creator's code model 10 | QML_IMPORT_PATH = 11 | 12 | SOURCES += shareutils.cpp 13 | HEADERS += shareutils.h 14 | 15 | ios { 16 | OBJECTIVE_SOURCES += ios/iosshareutils.mm 17 | HEADERS += ios/iosshareutils.h 18 | 19 | Q_ENABLE_BITCODE.name = ENABLE_BITCODE 20 | Q_ENABLE_BITCODE.value = NO 21 | QMAKE_MAC_XCODE_SETTINGS += Q_ENABLE_BITCODE 22 | } 23 | 24 | android { 25 | QT += androidextras 26 | OTHER_FILES += android_data/src/com/lasconic/QShareUtils.java 27 | ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android_data 28 | SOURCES += android/androidshareutils.cpp 29 | HEADERS += android/androidshareutils.h 30 | } 31 | 32 | # Default rules for deployment. 33 | include(deployment.pri) 34 | -------------------------------------------------------------------------------- /ios/iosshareutils.h: -------------------------------------------------------------------------------- 1 | //============================================================================= 2 | // Copyright (c) 2014 Nicolas Froment 3 | 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | // copies of the Software, and to permit persons to whom the Software is 9 | // furnished to do so, subject to the following conditions: 10 | 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | // THE SOFTWARE. 21 | //============================================================================= 22 | 23 | #ifndef __IOSSHAREUTILS_H__ 24 | #define __IOSSHAREUTILS_H__ 25 | 26 | #include "shareutils.h" 27 | 28 | class IosShareUtils : public PlatformShareUtils 29 | { 30 | Q_OBJECT 31 | 32 | public: 33 | explicit IosShareUtils(QQuickItem *parent = 0); 34 | Q_INVOKABLE void share(const QString &text, const QUrl &url); 35 | }; 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /android/androidshareutils.h: -------------------------------------------------------------------------------- 1 | //============================================================================= 2 | // Copyright (c) 2014 Nicolas Froment 3 | 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | // copies of the Software, and to permit persons to whom the Software is 9 | // furnished to do so, subject to the following conditions: 10 | 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | // THE SOFTWARE. 21 | //============================================================================= 22 | 23 | #ifndef ANDROIDSHAREUTILS_H 24 | #define ANDROIDSHAREUTILS_H 25 | 26 | #include "shareutils.h" 27 | 28 | class AndroidShareUtils : public PlatformShareUtils 29 | { 30 | public: 31 | AndroidShareUtils(QQuickItem* parent = 0); 32 | void share(const QString &text, const QUrl &url) override; 33 | }; 34 | 35 | #endif // ANDROIDSHAREUTILS_H 36 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | //============================================================================= 2 | // Copyright (c) 2014 Nicolas Froment 3 | 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | // copies of the Software, and to permit persons to whom the Software is 9 | // furnished to do so, subject to the following conditions: 10 | 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | // THE SOFTWARE. 21 | //============================================================================= 22 | 23 | #include 24 | #include 25 | 26 | #include "shareutils.h" 27 | 28 | int main(int argc, char *argv[]) 29 | { 30 | QApplication app(argc, argv); 31 | 32 | qmlRegisterType ("com.lasconic", 1, 0, "ShareUtils"); 33 | 34 | QQmlApplicationEngine engine; 35 | engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); 36 | 37 | return app.exec(); 38 | } 39 | -------------------------------------------------------------------------------- /main.qml: -------------------------------------------------------------------------------- 1 | //============================================================================= 2 | // Copyright (c) 2014 Nicolas Froment 3 | 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | // copies of the Software, and to permit persons to whom the Software is 9 | // furnished to do so, subject to the following conditions: 10 | 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | // THE SOFTWARE. 21 | //============================================================================= 22 | 23 | import QtQuick 2.3 24 | import QtQuick.Controls 1.2 25 | import com.lasconic 1.0 26 | 27 | ApplicationWindow { 28 | visible: true 29 | width: 640 30 | height: 480 31 | title: qsTr("Share Utils") 32 | 33 | ShareUtils { 34 | id: shareUtils 35 | } 36 | 37 | Button { 38 | id: share 39 | text: "Share lasconic's blog" 40 | anchors.centerIn: parent 41 | onClicked: { 42 | shareUtils.share("Awesome blog", "http://blog.lasconic.com") 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /shareutils.cpp: -------------------------------------------------------------------------------- 1 | //============================================================================= 2 | // Copyright (c) 2014 Nicolas Froment 3 | 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | // copies of the Software, and to permit persons to whom the Software is 9 | // furnished to do so, subject to the following conditions: 10 | 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | // THE SOFTWARE. 21 | //============================================================================= 22 | 23 | #include "shareutils.h" 24 | 25 | #ifdef Q_OS_IOS 26 | #include "ios/iosshareutils.h" 27 | #endif 28 | 29 | #ifdef Q_OS_ANDROID 30 | #include "android/androidshareutils.h" 31 | #endif 32 | 33 | ShareUtils::ShareUtils(QQuickItem *parent) 34 | : QQuickItem(parent) 35 | { 36 | #if defined(Q_OS_IOS) 37 | _pShareUtils = new IosShareUtils(this); 38 | #elif defined(Q_OS_ANDROID) 39 | _pShareUtils = new AndroidShareUtils(this); 40 | #else 41 | _pShareUtils = new PlatformShareUtils(this); 42 | #endif 43 | } 44 | 45 | void ShareUtils::share(const QString &text, const QUrl &url) 46 | { 47 | _pShareUtils->share(text, url); 48 | } 49 | -------------------------------------------------------------------------------- /shareutils.h: -------------------------------------------------------------------------------- 1 | //============================================================================= 2 | // Copyright (c) 2014 Nicolas Froment 3 | 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | // copies of the Software, and to permit persons to whom the Software is 9 | // furnished to do so, subject to the following conditions: 10 | 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | // THE SOFTWARE. 21 | //============================================================================= 22 | 23 | #ifndef SHAREUTILS_H 24 | #define SHAREUTILS_H 25 | 26 | #include 27 | 28 | class PlatformShareUtils : public QQuickItem 29 | { 30 | public: 31 | PlatformShareUtils(QQuickItem *parent = 0) : QQuickItem(parent){} 32 | virtual ~PlatformShareUtils() {} 33 | virtual void share(const QString &text, const QUrl &url){ qDebug() << text << url; } 34 | }; 35 | 36 | class ShareUtils : public QQuickItem 37 | { 38 | Q_OBJECT 39 | PlatformShareUtils* _pShareUtils; 40 | public: 41 | explicit ShareUtils(QQuickItem *parent = 0); 42 | Q_INVOKABLE void share(const QString &text, const QUrl &url); 43 | }; 44 | 45 | #endif //SHAREUTILS_H 46 | -------------------------------------------------------------------------------- /android/androidshareutils.cpp: -------------------------------------------------------------------------------- 1 | //============================================================================= 2 | // Copyright (c) 2014 Nicolas Froment 3 | 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | // copies of the Software, and to permit persons to whom the Software is 9 | // furnished to do so, subject to the following conditions: 10 | 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | // THE SOFTWARE. 21 | //============================================================================= 22 | #include "androidshareutils.h" 23 | 24 | #include 25 | 26 | AndroidShareUtils::AndroidShareUtils(QQuickItem* parent) : PlatformShareUtils(parent) 27 | { 28 | 29 | } 30 | 31 | void AndroidShareUtils::share(const QString &text, const QUrl &url) 32 | { 33 | QAndroidJniObject jsText = QAndroidJniObject::fromString(text); 34 | QAndroidJniObject jsUrl = QAndroidJniObject::fromString(url.toString()); 35 | QAndroidJniObject::callStaticMethod("com/lasconic/QShareUtils", 36 | "share", 37 | "(Ljava/lang/String;Ljava/lang/String;)V", 38 | jsText.object(), jsUrl.object()); 39 | } 40 | -------------------------------------------------------------------------------- /android_data/src/com/lasconic/QShareUtils.java: -------------------------------------------------------------------------------- 1 | //============================================================================= 2 | // Copyright (c) 2014 Nicolas Froment 3 | 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | // copies of the Software, and to permit persons to whom the Software is 9 | // furnished to do so, subject to the following conditions: 10 | 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | // THE SOFTWARE. 21 | //============================================================================= 22 | 23 | package com.lasconic; 24 | 25 | import org.qtproject.qt5.android.QtNative; 26 | 27 | import java.lang.String; 28 | import android.content.Intent; 29 | import android.util.Log; 30 | 31 | public class QShareUtils 32 | { 33 | protected QShareUtils() 34 | { 35 | //Log.d("lasconic", "QShareUtils()"); 36 | } 37 | 38 | public static void share(String text, String url) { 39 | if (QtNative.activity() == null) 40 | return; 41 | Intent sendIntent = new Intent(); 42 | sendIntent.setAction(Intent.ACTION_SEND); 43 | sendIntent.putExtra(Intent.EXTRA_TEXT, text + " " + url); 44 | sendIntent.setType("text/plain"); 45 | QtNative.activity().startActivity(sendIntent); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /ios/iosshareutils.mm: -------------------------------------------------------------------------------- 1 | //============================================================================= 2 | // Copyright (c) 2014 Nicolas Froment 3 | 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | // copies of the Software, and to permit persons to whom the Software is 9 | // furnished to do so, subject to the following conditions: 10 | 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | // THE SOFTWARE. 21 | //============================================================================= 22 | 23 | #import "iosshareutils.h" 24 | #import 25 | #import 26 | #import 27 | 28 | IosShareUtils::IosShareUtils(QQuickItem *parent) : PlatformShareUtils(parent) 29 | { 30 | 31 | } 32 | 33 | void IosShareUtils::share(const QString &text, const QUrl &url) { 34 | 35 | NSMutableArray *sharingItems = [NSMutableArray new]; 36 | 37 | if (!text.isEmpty()) { 38 | [sharingItems addObject:text.toNSString()]; 39 | } 40 | if (url.isValid()) { 41 | [sharingItems addObject:url.toNSURL()]; 42 | } 43 | 44 | // get the main window rootViewController 45 | UIViewController *qtController = [[UIApplication sharedApplication].keyWindow rootViewController]; 46 | 47 | UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:sharingItems applicationActivities:nil]; 48 | if ( [activityController respondsToSelector:@selector(popoverPresentationController)] ) { // iOS8 49 | activityController.popoverPresentationController.sourceView = qtController.view; 50 | } 51 | [qtController presentViewController:activityController animated:YES completion:nil]; 52 | } 53 | 54 | 55 | --------------------------------------------------------------------------------