Merge remote-tracking branch 'origin/master'

This commit is contained in:
Dorian Zedler 2020-01-19 16:28:41 +01:00
commit 5648a2ae41
7 changed files with 179 additions and 3 deletions

View File

@ -67,6 +67,14 @@ win {
ios {
QMAKE_ASSET_CATALOGS += shared/Assets.xcassets
OBJECTIVE_SOURCES += \
sources/ios/docviewcontroller.mm \
sources/ios/iosshareutils.mm
HEADERS += \
headers/ios/docviewcontroller.hpp \
headers/ios/iosshareutils.hpp
}
DISTFILES += \

View File

@ -5,7 +5,7 @@
#include <QDebug>
#if defined(Q_OS_IOS)
mPlatformShareUtils = new IosShareUtils(this);
#include "ios/iosshareutils.hpp"
#elif defined(Q_OS_ANDROID)
#include <QtAndroid>
#include <QAndroidActivityResultReceiver>
@ -32,6 +32,7 @@ public:
private:
#if defined(Q_OS_IOS)
#elif defined(Q_OS_ANDROID)
void processActivityResult(int requestCode, int resultCode);
static FileHelper* mInstance;

View File

@ -0,0 +1,21 @@
// (c) 2017 Ekkehard Gentz (ekke) @ekkescorner
// my blog about Qt for mobile: http://j.mp/qt-x
// see also /COPYRIGHT and /LICENSE
#ifndef DOCVIEWCONTROLLER_HPP
#define DOCVIEWCONTROLLER_HPP
#import <UIKit/UIKit.h>
#import "headers/ios/iosshareutils.hpp"
@interface DocViewController : UIViewController <UIDocumentInteractionControllerDelegate>
@property int requestId;
@property IosShareUtils *mIosShareUtils;
@end
#endif // DOCVIEWCONTROLLER_HPP

28
headers/ios/iosshareutils.hpp Executable file
View File

@ -0,0 +1,28 @@
// (c) 2017 Ekkehard Gentz (ekke) @ekkescorner
// my blog about Qt for mobile: http://j.mp/qt-x
// see also /COPYRIGHT and /LICENSE
#ifndef __IOSSHAREUTILS_H__
#define __IOSSHAREUTILS_H__
#include <QObject>
class IosShareUtils : public QObject
{
Q_OBJECT
public:
explicit IosShareUtils(QObject *parent = 0);
bool checkMimeTypeView(const QString &mimeType);
bool checkMimeTypeEdit(const QString &mimeType);
void sendFile(const QString &filePath, const QString &title, const QString &mimeType, const int &requestId);
void handleDocumentPreviewDone(const int &requestId);
signals:
void shareFinished(int requestCode);
void shareNoAppAvailable(int requestCode);
void shareError(int requestCode, QString message);
};
#endif

View File

@ -1,10 +1,10 @@
#include "../headers/filehelper.h"
#include "headers/filehelper.h"
#if defined(Q_OS_IOS)
#include <QUrl>
#include <QFileInfo>
#include <QDateTime>
#elif defined(Q_OS_ANDROID)
#include <QtAndroidExtras/QAndroidJniObject>
#include <jni.h>
#endif
@ -15,12 +15,15 @@ FileHelper::FileHelper(QObject *parent) : QObject(parent)
#elif defined(Q_OS_ANDROID)
mInstance = this;
#else
#endif
}
void FileHelper::viewFile(const QString &filePath, const QString &title, const QString &mimeType, const int &requestId)
{
#if defined(Q_OS_IOS)
IosShareUtils iosShareUtils;
iosShareUtils.sendFile(filePath, title, mimeType, requestId);
#elif defined(Q_OS_ANDROID)
QAndroidJniObject jsPath = QAndroidJniObject::fromString(filePath);
QAndroidJniObject jsTitle = QAndroidJniObject::fromString(title);

View File

@ -0,0 +1,32 @@
// (c) 2017 Ekkehard Gentz (ekke) @ekkescorner
// my blog about Qt for mobile: http://j.mp/qt-x
// see also /COPYRIGHT and /LICENSE
#import "headers/ios/docviewcontroller.hpp"
#include <QDebug>
@interface DocViewController ()
@end
@implementation DocViewController
#pragma mark -
#pragma mark View Life Cycle
- (void)viewDidLoad {
[super viewDidLoad];
}
#pragma mark -
#pragma mark Document Interaction Controller Delegate Methods
- (UIViewController *) documentInteractionControllerViewControllerForPreview: (UIDocumentInteractionController *) controller {
#pragma unused (controller)
return self;
}
- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller
{
#pragma unused (controller)
qDebug() << "end preview";
self.mIosShareUtils->handleDocumentPreviewDone(self.requestId);
[self removeFromParentViewController];
}
@end

83
sources/ios/iosshareutils.mm Executable file
View File

@ -0,0 +1,83 @@
// (c) 2017 Ekkehard Gentz (ekke) @ekkescorner
// my blog about Qt for mobile: http://j.mp/qt-x
// see also /COPYRIGHT and /LICENSE
#import "headers/ios/iosshareutils.hpp"
#import <UIKit/UIKit.h>
#import <QGuiApplication>
#import <QQuickWindow>
#import <QDesktopServices>
#import <QUrl>
#import <QFileInfo>
#import <UIKit/UIDocumentInteractionController.h>
#import "headers/ios/docviewcontroller.hpp"
IosShareUtils::IosShareUtils(QObject *parent) : QObject(parent)
{
// Sharing Files from other iOS Apps I got the ideas and some code contribution from:
// Thomas K. Fischer (@taskfabric) - http://taskfabric.com - thx
QDesktopServices::setUrlHandler("file", this, "handleFileUrlReceived");
}
bool IosShareUtils::checkMimeTypeView(const QString &mimeType) {
#pragma unused (mimeType)
// dummi implementation on iOS
// MimeType not used yet
return true;
}
bool IosShareUtils::checkMimeTypeEdit(const QString &mimeType) {
#pragma unused (mimeType)
// dummi implementation on iOS
// MimeType not used yet
return true;
}
// altImpl not used yet on iOS, on Android twi ways to use JNI
void IosShareUtils::sendFile(const QString &filePath, const QString &title, const QString &mimeType, const int &requestId) {
#pragma unused (title, mimeType)
NSString* nsFilePath = filePath.toNSString();
NSURL *nsFileUrl = [NSURL fileURLWithPath:nsFilePath];
static DocViewController* docViewController = nil;
if(docViewController!=nil)
{
[docViewController removeFromParentViewController];
[docViewController release];
}
UIDocumentInteractionController* documentInteractionController = nil;
documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:nsFileUrl];
UIViewController* qtUIViewController = [[[[UIApplication sharedApplication]windows] firstObject]rootViewController];
if(qtUIViewController!=nil)
{
docViewController = [[DocViewController alloc] init];
docViewController.requestId = requestId;
// we need this to be able to execute handleDocumentPreviewDone() method,
// when preview was finished
docViewController.mIosShareUtils = this;
[qtUIViewController addChildViewController:docViewController];
documentInteractionController.delegate = docViewController;
// [documentInteractionController presentPreviewAnimated:YES];
if(![documentInteractionController presentPreviewAnimated:YES])
{
emit shareError(0, tr("No App found to open: %1").arg(filePath));
}
}
}
void IosShareUtils::handleDocumentPreviewDone(const int &requestId)
{
// documentInteractionControllerDidEndPreview
qDebug() << "handleShareDone: " << requestId;
//emit shareFinished(requestId);
}