在iOS设备上找不到文件Qt Creator C ++ QStandardPaths :: standardLocations(QStandardPaths :: DownloadLo cation);

我正在使用Qt Creator C ++ QStandardPaths :: standardLocations(QStandardPaths :: DownloadLo阳离子); 到达用户的文件,但不会返回在iOS设备上find的文件。 这是我的代码到目前为止:

const QStringList mPathList = QStandardPaths::standardLocations(QStandardPaths::DownloadLo‌​cation); mPath = QString("%1").arg(mPathList.first()); dirmodel = new QFileSystemModel (this); dirmodel->setFilter(QDir::AllDirs | QDir::NoDotAndDotDot); dirmodel->setRootPath(mPath); 

在iOS上,所有应用程序都位于自己的沙箱中,并且永远不会直接访问应用程序之外的事物/文件。

为此,您需要在运行时从您的应用程序调用本机iOS API。 最好不要在下次启动应用程序时保存path来使用它们,因为iOS会为sandbox中的应用程序生成基于散列的path,并且在下次运行应用程序时它们可能会更改。

这里是如何在Qt中调用本地iOS代码的示例。

用以下内容创buildios_utils.mm并将其添加到项目的源代码目录中:

 #include "ios_utils.h" #include <QStringList> #include <UIKit/UIKit.h> #include <qpa/qplatformnativeinterface.h> QStringList getiOSHomeFolder() { QStringList retval; NSString *item; NSArray *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:NSHomeDirectory() error:nil]; for (item in contents) { retval.append(QString::fromNSString(item)); } return retval; } 

创buildios_utils.h

 #ifndef IOSUTILS_H #define IOSUTILS_H #ifdef Q_OS_IOS QStringList getiOSHomeFolder(); #endif // Q_OS_IOS #endif // IOSUTILS_H 

并在你的Qt代码中的某个地方:

 #include "ios_utils.h" qDebug() << getiOSHomeFolder();