我正在尝试读取通过电子邮件发送然后下载到 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 设备上的文本文件路径的任何帮助,我们将不胜感激。
谢谢
最佳答案
我终于明白了。
我必须将以下键值对添加到我的 .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 中,它就无法工作。
将在"file"图标\“在我的 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');
});
关于android - ionic 4 - 使用 @ionic-native/file/ngx/cordova-plugin-file 访问 iOS 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59825500/