From 1c7868312dad02d3e1f19248d5160b99974036bf Mon Sep 17 00:00:00 2001 From: "vladimir.kuznetsov" Date: Sat, 9 Sep 2023 01:29:28 +0500 Subject: [PATCH] added getting the path to the file for iOS --- client/platforms/ios/MobileUtils.cpp | 3 +- client/platforms/ios/MobileUtils.h | 9 +++-- client/platforms/ios/MobileUtils.mm | 39 ++++++++++++++++++---- client/ui/controllers/systemController.cpp | 5 ++- 4 files changed, 45 insertions(+), 11 deletions(-) diff --git a/client/platforms/ios/MobileUtils.cpp b/client/platforms/ios/MobileUtils.cpp index f94d56fc..fd20fcda 100644 --- a/client/platforms/ios/MobileUtils.cpp +++ b/client/platforms/ios/MobileUtils.cpp @@ -4,6 +4,7 @@ void MobileUtils::shareText(const QStringList &) { } -void MobileUtils::openFile() +QString MobileUtils::openFile() { + return QString(); } diff --git a/client/platforms/ios/MobileUtils.h b/client/platforms/ios/MobileUtils.h index cad9de9e..fe7549c5 100644 --- a/client/platforms/ios/MobileUtils.h +++ b/client/platforms/ios/MobileUtils.h @@ -8,12 +8,15 @@ class MobileUtils : public QObject { Q_OBJECT -public: - MobileUtils() = delete; +//public: +// MobileUtils() = delete; public slots: static void shareText(const QStringList &filesToSend); - static void openFile(); + QString openFile(); + +signals: + void finished(); }; #endif // MOBILEUTILS_H diff --git a/client/platforms/ios/MobileUtils.mm b/client/platforms/ios/MobileUtils.mm index 63ee0364..7f5fc1e6 100644 --- a/client/platforms/ios/MobileUtils.mm +++ b/client/platforms/ios/MobileUtils.mm @@ -4,6 +4,7 @@ #include #include +#include static UIViewController* getViewController() { NSArray *windows = [[UIApplication sharedApplication]windows]; @@ -35,24 +36,33 @@ void MobileUtils::shareText(const QStringList& filesToSend) { } } +typedef void (^FileSelectionCallback)(NSString *fileContent); + @interface MyFilePickerDelegate : NSObject + +@property (nonatomic, copy) FileSelectionCallback fileSelectionCallback; + @end @implementation MyFilePickerDelegate - (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray *)urls { for (NSURL *url in urls) { - NSString *filePath = [url path]; - - NSData *fileData = [NSData dataWithContentsOfFile:filePath]; - NSString *fileContent = [[NSString alloc] initWithData:fileData encoding:NSUTF8StringEncoding]; - NSLog(@"Содержимое файла: %@", fileContent); + if (self.fileSelectionCallback) { + self.fileSelectionCallback([url path]); + } + } +} + +- (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller { + if (self.fileSelectionCallback) { + self.fileSelectionCallback(nil); } } @end -void MobileUtils::openFile() { +QString MobileUtils::openFile() { UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:@[@"public.item"] inMode:UIDocumentPickerModeOpen]; MyFilePickerDelegate *filePickerDelegate = [[MyFilePickerDelegate alloc] init]; @@ -62,4 +72,21 @@ void MobileUtils::openFile() { if (!qtController) return; [qtController presentViewController:documentPicker animated:YES completion:nil]; + + __block QString path1; + + filePickerDelegate.fileSelectionCallback = ^(NSString *filePath) { + if (filePath) { + path1 = QString::fromUtf8(filePath.UTF8String); + } else { + path1 = QString(""); + } + emit finished(); + }; + + QEventLoop wait1; + QObject::connect(this, &MobileUtils::finished, &wait1, &QEventLoop::quit); + wait1.exec(); + + return path1; } diff --git a/client/ui/controllers/systemController.cpp b/client/ui/controllers/systemController.cpp index a6210b69..8e1fb6c8 100644 --- a/client/ui/controllers/systemController.cpp +++ b/client/ui/controllers/systemController.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #ifdef Q_OS_ANDROID #include "platforms/android/android_controller.h" @@ -58,8 +59,10 @@ QString SystemController::getFileName(const QString &acceptLabel, const QString { QString fileName; #ifdef Q_OS_IOS - MobileUtils::openFile(); + MobileUtils mobileUtils; + fileName = mobileUtils.openFile(); + CFURLRef url = CFURLCreateWithFileSystemPath( kCFAllocatorDefault, CFStringCreateWithCharacters(0, reinterpret_cast(fileName.unicode()), fileName.length()),