added getting the path to the file for iOS

This commit is contained in:
vladimir.kuznetsov 2023-09-09 01:29:28 +05:00
parent fdff57da7c
commit 1c7868312d
4 changed files with 45 additions and 11 deletions

View file

@ -4,6 +4,7 @@ void MobileUtils::shareText(const QStringList &)
{
}
void MobileUtils::openFile()
QString MobileUtils::openFile()
{
return QString();
}

View file

@ -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

View file

@ -4,6 +4,7 @@
#include <Security/Security.h>
#include <QDebug>
#include <QEventLoop>
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 <UIDocumentPickerDelegate>
@property (nonatomic, copy) FileSelectionCallback fileSelectionCallback;
@end
@implementation MyFilePickerDelegate
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray<NSURL *> *)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;
}

View file

@ -8,6 +8,7 @@
#include <QQuickItem>
#include <QStandardPaths>
#include <QUrl>
#include <QtConcurrent>
#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<const UniChar *>(fileName.unicode()), fileName.length()),