我无法使用cordova-plugin-file在iOS 12中写入文件。 将应用程序部署到运行iOS 11.4的iPhone 7 Plus时,该代码对我有用。我要做的就是从照片库中选择一个文件并将其保存到数据目录中。
从库中成功选择文件后,这是用于写入数据目录的代码。
writeToFile(fileName, data) {
return new Promise((resolve, reject) => {
if (this.isBrowser) {
return resolve({});
}
const self = this;
window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function (directoryEntry) {
directoryEntry.getFile(fileName, {
create: true
}, function (fileEntry) {
fileEntry.createWriter(function (fileWriter) {
console.log('THIS IS PRINTING: Inside fileEntry.createWriter promise');
fileWriter.onwriteend = function (e) {
console.log('NOT PRINTING: Inside fileWriter.onwriteend promise');
return resolve(fileName);
};
fileWriter.onerror = function (e) {
return reject(e);
// you could hook this up with our global error handler, or pass in an error callback
};
const blob = new Blob([data], {
type: 'image/png'
});
fileWriter.write(blob);
}, self.fileErrorHandler.bind(null, fileName));
}, self.fileErrorHandler.bind(null, fileName));
}, self.fileErrorHandler.bind(null, fileName));
});
}
我将文件的本地URL作为数据参数传递给函数。 我尝试将其更改为字符串,并将blob的类型更改为text / plain。但这也无济于事。
我的config.xml中的代码片段
<preference name="iosPersistentFileLocation" value="Library" />
<platform name="ios">
<edit-config file="*-Info.plist" mode="merge" target="NSCameraUsageDescription">
<string>need camera access to take pictures</string>
</edit-config>
<edit-config file="*-Info.plist" mode="merge" target="NSPhotoLibraryUsageDescription">
<string>need photo library access to get pictures from there</string>
</edit-config>
<edit-config file="*-Info.plist" mode="merge" target="NSLocationWhenInUseUsageDescription">
<string>need location access to find things nearby</string>
</edit-config>
<edit-config file="*-Info.plist" mode="merge" target="NSPhotoLibraryAddUsageDescription">
<string>need photo library access to save pictures there</string>
</edit-config>
</platform>
让我感到很有趣的是,该代码在我的iPhone 7 Plus(在iOS 11.4上)能完美运行,但是当我尝试在运行iOS 12的iPhone XR上运行时,不会触发FileWriter onwriteend。尝试捕获所有错误,并且没有出现错误。
我还在XCode控制台中看到此错误
2019-06-23 10:28:51.806034-0400 Magically[30751:2758873] [discovery]
errors encountered while discovering extensions: Error Domain=PlugInKit
Code=13 "query cancelled"
UserInfo={NSLocalizedDescription=query cancelled}
作为一种解决方法,我正在考虑从图像URL创建Blob,并将其保存到本地存储中。
可以帮忙吗?