added a wait for the file dialog to close when sharing ios files

This commit is contained in:
vladimir.kuznetsov 2023-09-09 15:00:34 +05:00
parent 1c7868312d
commit 0a5657738e
3 changed files with 47 additions and 26 deletions

View file

@ -7,12 +7,12 @@
class MobileUtils : public QObject class MobileUtils : public QObject
{ {
Q_OBJECT Q_OBJECT
//public: public:
// MobileUtils() = delete; explicit MobileUtils(QObject *parent = nullptr);
public slots: public slots:
static void shareText(const QStringList &filesToSend); bool shareText(const QStringList &filesToSend);
QString openFile(); QString openFile();
signals: signals:

View file

@ -3,7 +3,6 @@
#include <UIKit/UIKit.h> #include <UIKit/UIKit.h>
#include <Security/Security.h> #include <Security/Security.h>
#include <QDebug>
#include <QEventLoop> #include <QEventLoop>
static UIViewController* getViewController() { static UIViewController* getViewController() {
@ -16,7 +15,11 @@ static UIViewController* getViewController() {
return nil; return nil;
} }
void MobileUtils::shareText(const QStringList& filesToSend) { MobileUtils::MobileUtils(QObject *parent) : QObject(parent) {
}
bool MobileUtils::shareText(const QStringList& filesToSend) {
NSMutableArray *sharingItems = [NSMutableArray new]; NSMutableArray *sharingItems = [NSMutableArray new];
for (int i = 0; i < filesToSend.size(); i++) { for (int i = 0; i < filesToSend.size(); i++) {
@ -28,35 +31,48 @@ void MobileUtils::shareText(const QStringList& filesToSend) {
if (!qtController) return; if (!qtController) return;
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:sharingItems applicationActivities:nil]; UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:sharingItems applicationActivities:nil];
__block bool isAccepted = false;
[activityController setCompletionWithItemsHandler:^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
isAccepted = completed;
emit finished();
}];
[qtController presentViewController:activityController animated:YES completion:nil]; [qtController presentViewController:activityController animated:YES completion:nil];
UIPopoverPresentationController *popController = activityController.popoverPresentationController; UIPopoverPresentationController *popController = activityController.popoverPresentationController;
if (popController) { if (popController) {
popController.sourceView = qtController.view; popController.sourceView = qtController.view;
} }
QEventLoop wait;
QObject::connect(this, &MobileUtils::finished, &wait, &QEventLoop::quit);
wait.exec();
return isAccepted;
} }
typedef void (^FileSelectionCallback)(NSString *fileContent); typedef void (^DocumentPickerClosedCallback)(NSString *path);
@interface MyFilePickerDelegate : NSObject <UIDocumentPickerDelegate> @interface DocumentPickerDelegate : NSObject <UIDocumentPickerDelegate>
@property (nonatomic, copy) FileSelectionCallback fileSelectionCallback; @property (nonatomic, copy) DocumentPickerClosedCallback documentPickerClosedCallback;
@end @end
@implementation MyFilePickerDelegate @implementation DocumentPickerDelegate
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls { - (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls {
for (NSURL *url in urls) { for (NSURL *url in urls) {
if (self.fileSelectionCallback) { if (self.documentPickerClosedCallback) {
self.fileSelectionCallback([url path]); self.documentPickerClosedCallback([url path]);
} }
} }
} }
- (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller { - (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller {
if (self.fileSelectionCallback) { if (self.documentPickerClosedCallback) {
self.fileSelectionCallback(nil); self.documentPickerClosedCallback(nil);
} }
} }
@ -65,28 +81,28 @@ typedef void (^FileSelectionCallback)(NSString *fileContent);
QString MobileUtils::openFile() { QString MobileUtils::openFile() {
UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:@[@"public.item"] inMode:UIDocumentPickerModeOpen]; UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:@[@"public.item"] inMode:UIDocumentPickerModeOpen];
MyFilePickerDelegate *filePickerDelegate = [[MyFilePickerDelegate alloc] init]; DocumentPickerDelegate *documentPickerDelegate = [[DocumentPickerDelegate alloc] init];
documentPicker.delegate = filePickerDelegate; documentPicker.delegate = documentPickerDelegate;
UIViewController *qtController = getViewController(); UIViewController *qtController = getViewController();
if (!qtController) return; if (!qtController) return;
[qtController presentViewController:documentPicker animated:YES completion:nil]; [qtController presentViewController:documentPicker animated:YES completion:nil];
__block QString path1; __block QString filePath;
filePickerDelegate.fileSelectionCallback = ^(NSString *filePath) { documentPickerDelegate.documentPickerClosedCallback = ^(NSString *path) {
if (filePath) { if (path) {
path1 = QString::fromUtf8(filePath.UTF8String); filePath = QString::fromUtf8(path.UTF8String);
} else { } else {
path1 = QString(""); filePath = QString();
} }
emit finished(); emit finished();
}; };
QEventLoop wait1; QEventLoop wait;
QObject::connect(this, &MobileUtils::finished, &wait1, &QEventLoop::quit); QObject::connect(this, &MobileUtils::finished, &wait, &QEventLoop::quit);
wait1.exec(); wait.exec();
return path1; return filePath;
} }

View file

@ -46,7 +46,9 @@ void SystemController::saveFile(QString fileName, const QString &data)
#ifdef Q_OS_IOS #ifdef Q_OS_IOS
QStringList filesToSend; QStringList filesToSend;
filesToSend.append(fileUrl.toString()); filesToSend.append(fileUrl.toString());
MobileUtils::shareText(filesToSend); MobileUtils mobileUtils;
// todo check if save successful
mobileUtils.shareText(filesToSend);
return; return;
#else #else
QFileInfo fi(fileName); QFileInfo fi(fileName);
@ -62,6 +64,9 @@ QString SystemController::getFileName(const QString &acceptLabel, const QString
MobileUtils mobileUtils; MobileUtils mobileUtils;
fileName = mobileUtils.openFile(); fileName = mobileUtils.openFile();
if (fileName.isEmpty()) {
return fileName;
}
CFURLRef url = CFURLCreateWithFileSystemPath( CFURLRef url = CFURLCreateWithFileSystemPath(
kCFAllocatorDefault, kCFAllocatorDefault,