【问题标题】:ionic 4 - iOS file access with @ionic-native/file/ngx/cordova-plugin-fileionic 4 - 使用 @ionic-native/file/ngx/cordova-plugin-file 访问 iOS 文件
【发布时间】:2020-05-06 14:14:18
【问题描述】:

我正在尝试读取通过电子邮件发送然后下载到 iOS 设备的文本文件。具体来说,文本文件保存在 - “文件图标 - 在我的手机上”。

我正在使用以下插件 @ionic-native/file/ngx / cordova-plugin-file。我可以让它在 Android 上运行,但我不确定文件的位置或如何找到它?

我在以下 iOS 目录中使用了插件 checkDir() 来验证它们是否存在并且我可以使用已安装的插件:

cordova.file.tempDirectory - Temp directory that the OS can clear at will. Do not rely on the OS to clear this directory; your app should always remove files as applicable. ( iOS , OSX , windows )

cordova.file.syncedDataDirectory - Holds app-specific files that should be synced (e.g. to iCloud). ( iOS , windows )

cordova.file.documentsDirectory - Files private to the app, but that are meaningful to other application (e.g. Office files). Note that for OSX this is the user’s ~/Documents directory. ( iOS , OSX )

我想用下面的方法来访问和读入文本文件:

this.file.readAsText()

但如果我尝试访问每个目录中的文件 - 例如通过以下方式:

this.file.readAsText(this.file.tempDirectory, 'MyData.txt').then((data) => {})

由于无法找到文件而出错。

底线是我没有存储在我的 iOS 设备上的文本文件的正确路径。

任何有关如何确定保存在 iOS 设备上的文本文件路径的帮助将不胜感激。

谢谢

【问题讨论】:

  • 检查此链接,它应该可以解决您的问题,它会向您显示图像的正常部分:
  • 感谢您的回复。在您提供的链接中,他们似乎指定了文件的下载位置 - cordova.file.dataDirectory 。就我而言,我从 iPhone 上的 gmail 帐户下载了一个文本文件。查看它存储在的 iPhone - 文件图标 - “在我的 iPhone 上”,我不知道该位置的路径?正如我在下面提到的,我尝试在以下位置读取文件,documentsDirectory、dataDirectory、syncedDataDirectory、cacheDirectory、tempDirectory per github.com/apache/cordova-plugin-file#file-system-layouts,但找不到该文件。
  • 你可以手动访问文件吗?
  • 在 iPhone 上,我可以转到“文件”图标,然后在“我的 iPhone”下看到文本文件。我可以双击它并打开它。例如,当我尝试使用 this.file.readAsText(this.file.documentsDirectory, 'MyData.txt').then((data) => {})。找不到。我不确定如何找出文件的路径?

标签: android ios angular cordova ionic-framework


【解决方案1】:

我终于明白了。

我必须在我的 .info.plist 中添加 BOTH 以下键值对:

<key>UIFileSharingEnabled</key>
<true/>
<key>LSSupportsOpeningDocumentsInPlace</key>
<true/>

在 Xcode 中,这将显示为:

Application supports iTunes file sharing | Boolean | YES
Supports opening documents in place | Boolean | YES

我看过其他一些帖子,其中提到了每个键值对,但从不需要两者。我单独尝试了多次,如果没有将两个键值对都添加到应用程序的 info.plist 中,它将无法工作。

将在“文件”图标\“在我的 iPhone”下创建一个新文件夹。文件夹名称将与您的应用名称相对应。这个新的应用程序文件夹下有一个子文件夹 - NoCloud。

我能够将我的文本文件放在这个位置(“在我的 iPhone”\MyAppName\NoCloud),并使用以下代码从我的应用程序中读取文本文件:

const path = this.file.documentsDirectory + '/NoCloud';
this.file.readAsText(path, 'MyFile.txt').then((data) => {
    console.log(data);
}).catch((err) => {
    console.log('File does not exist');
}); 

【讨论】:

    【解决方案2】:

    我会参考插件的 github 文件位置参考:https://github.com/apache/cordova-plugin-file#file-system-layouts

    然后检查从电子邮件中保存此类 .txt 后实际存储的位置(我认为这也可能取决于使用的电子邮件客户端以及您打算支持的 iOS 版本)

    更新: 对于IOS,应该通过config.xml添加文件共享权限:

    <key>UIFileSharingEnabled</key>
    <true/>
    

    【讨论】:

    • 感谢谢尔盖的回复。我相信我已经尝试过你的建议。我从 gmail 帐户下载了一个文本文件到我的 iPhone。它存储在文件图标 - “在我的 iPhone 上”。然后我继续尝试从各个位置(documentsDirectory、dataDirectory、syncedDataDirectory、cacheDirectory、tempDirectory)读取文件,如下所示 - this.file.readAsText(this.file.documentsDirectory, 'MyTextFile.txt').then(( data) => { console.log(data); }).catch((err) => { console.log('documentsDirectory 文件不存在' + err); });
    • 是的,很有趣,我读过这篇文章:stackoverflow.com/questions/55372276/… 所以很可能访问由其他应用程序(gmail?)创建的文件受权限等限制,因此cordova插件不能开箱即用当您的应用程序创建和读取自己的文件时,这很有效
    • 感谢您的链接。看起来你是对的。链接中的一个建议是将以下内容添加到 info.plist - UIFileSharingEnabled 我会试试这个。感谢您找到链接。
    猜你喜欢
    • 1970-01-01
    • 2019-12-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-16
    • 2019-01-05
    • 2018-08-25
    • 1970-01-01
    • 2021-01-10
    相关资源
    最近更新 更多