added getting the path to the file for iOS
This commit is contained in:
parent
fdff57da7c
commit
1c7868312d
4 changed files with 45 additions and 11 deletions
|
|
@ -4,6 +4,7 @@ void MobileUtils::shareText(const QStringList &)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void MobileUtils::openFile()
|
QString MobileUtils::openFile()
|
||||||
{
|
{
|
||||||
|
return QString();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,12 +8,15 @@ class MobileUtils : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
//public:
|
||||||
MobileUtils() = delete;
|
// MobileUtils() = delete;
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
static void shareText(const QStringList &filesToSend);
|
static void shareText(const QStringList &filesToSend);
|
||||||
static void openFile();
|
QString openFile();
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void finished();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // MOBILEUTILS_H
|
#endif // MOBILEUTILS_H
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
#include <Security/Security.h>
|
#include <Security/Security.h>
|
||||||
|
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
#include <QEventLoop>
|
||||||
|
|
||||||
static UIViewController* getViewController() {
|
static UIViewController* getViewController() {
|
||||||
NSArray *windows = [[UIApplication sharedApplication]windows];
|
NSArray *windows = [[UIApplication sharedApplication]windows];
|
||||||
|
|
@ -35,24 +36,33 @@ void MobileUtils::shareText(const QStringList& filesToSend) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
typedef void (^FileSelectionCallback)(NSString *fileContent);
|
||||||
|
|
||||||
@interface MyFilePickerDelegate : NSObject <UIDocumentPickerDelegate>
|
@interface MyFilePickerDelegate : NSObject <UIDocumentPickerDelegate>
|
||||||
|
|
||||||
|
@property (nonatomic, copy) FileSelectionCallback fileSelectionCallback;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@implementation MyFilePickerDelegate
|
@implementation MyFilePickerDelegate
|
||||||
|
|
||||||
- (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) {
|
||||||
NSString *filePath = [url path];
|
if (self.fileSelectionCallback) {
|
||||||
|
self.fileSelectionCallback([url path]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
|
- (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller {
|
||||||
NSString *fileContent = [[NSString alloc] initWithData:fileData encoding:NSUTF8StringEncoding];
|
if (self.fileSelectionCallback) {
|
||||||
NSLog(@"Содержимое файла: %@", fileContent);
|
self.fileSelectionCallback(nil);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
void 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];
|
MyFilePickerDelegate *filePickerDelegate = [[MyFilePickerDelegate alloc] init];
|
||||||
|
|
@ -62,4 +72,21 @@ void MobileUtils::openFile() {
|
||||||
if (!qtController) return;
|
if (!qtController) return;
|
||||||
|
|
||||||
[qtController presentViewController:documentPicker animated:YES completion:nil];
|
[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;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@
|
||||||
#include <QQuickItem>
|
#include <QQuickItem>
|
||||||
#include <QStandardPaths>
|
#include <QStandardPaths>
|
||||||
#include <QUrl>
|
#include <QUrl>
|
||||||
|
#include <QtConcurrent>
|
||||||
|
|
||||||
#ifdef Q_OS_ANDROID
|
#ifdef Q_OS_ANDROID
|
||||||
#include "platforms/android/android_controller.h"
|
#include "platforms/android/android_controller.h"
|
||||||
|
|
@ -58,7 +59,9 @@ QString SystemController::getFileName(const QString &acceptLabel, const QString
|
||||||
{
|
{
|
||||||
QString fileName;
|
QString fileName;
|
||||||
#ifdef Q_OS_IOS
|
#ifdef Q_OS_IOS
|
||||||
MobileUtils::openFile();
|
|
||||||
|
MobileUtils mobileUtils;
|
||||||
|
fileName = mobileUtils.openFile();
|
||||||
|
|
||||||
CFURLRef url = CFURLCreateWithFileSystemPath(
|
CFURLRef url = CFURLCreateWithFileSystemPath(
|
||||||
kCFAllocatorDefault,
|
kCFAllocatorDefault,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue