I am trying to save the pdf in iOS using the Ionic Filesystem. I am using the below functions for writing the files.
Filesystem.writeFile({
path: 'test',
data: EStatementConstant.BinData,
directory: Directory.Documents,
encoding: Encoding.UTF8,
});
Where EStatementConstant.BinData is my base64 data [validated] which I am getting directly from the API service.
The problem is that I am unable to open the file on a Mac. I didn't find any function to decode.
I am using capacitor v-3.
https://capacitorjs.com/docs/apis/filesystem?_gl=11i199tw_gaMTYzMjY4MzAzMi4xNjM0ODQxNDI4_ga_REH9TJF6KF*MTY0ODgwMzI4NS40OS4wLjE2NDg4MDMyODUuMA..#type-401
Note:- I am unable to save the pdf data without encoding in iOS. Also, I can not use the Cordova plugin. I am struggling with this for the last few days. Please help.
Related
I have the following code (adapted from this tutorial) that I use for saving files from backend:
import { FilesystemDirectory, Plugins } from '#capacitor/core';
const { Filesystem } = Plugins;
await Filesystem.writeFile({
path: "filename.txt",
data: "base64 data",
directory: FilesystemDirectory.Documents,
});
This codes works fine on Android, even creates the Documents directory in the root of internal storage. Unfortunately, on iOS no file is created, no mater what directory I use (I've tested it with Documents, Data and ExternalStorage).
When I put this code block inside try..catch, nothing is thrown, so the operation is supposedly completed successfully, just no file is saved. Tested on one Android device and two iOS.
iOS doesn't allow accessing file storage directly. I made it work with Ionic-native's FileOpener like this:
import { FileOpener } from '#ionic-native/file-opener/ngx';
import { FilesystemDirectory, Plugins } from '#capacitor/core';
const { Filesystem } = Plugins;
const result = await Filesystem.writeFile({
path: "filename.txt",
data: "base64 data",
directory: FilesystemDirectory.Documents,
});
this.fileOpener.showOpenWithDialog(result.uri, 'application/json')
It opens a dialog where you can select where to save your file. It's a bit confusing as the function's description is "Opens with system modal to open file with an already installed app" but saving works with it as well.
You need to also install cordova-plugin-file-opener2 and put ionic FileOpener to providers in app.module.ts. If you are not using Cordova otherwise, you don't need to do Cordova setups, it works alongside Capacitor as a separate plugin. As far as I'm aware, Capacitor doesn't provide a solution for downloading in iOS.
I've encountered the same problem (while having capacitor 4.2.0 only) and solved it by adding the following permissions in my Info.plist:
<key>UIFileSharingEnabled</key>
<string>YES</string>
<key>LSSupportsOpeningDocumentsInPlace</key>
<string>YES</string>
After this, using directory Directory.Documents has made me visible a Documents directory with the name of the app under the Files App.
Hope it helps.
1) Storing images locally to be displayed later via the html, most all of the docs say to store them like in ~/images/image1.png. Typescript/Nativescript says to store these by doing this:
fileSystemModule.knownFolders.currentApp(), then join the "/images/image1.png and write the file. This works great on IOS and Android in debug mode and the simulators, but it doesn't work in a Release version of IOS. IOS gives an error saying you can't access this folder. The nativescript documentation clearly says for currentApp that " iOS - this folder is read-only and contains the app and all its resources.".
Thus, if it's IOS, I do what the doc for the Filesystem/IOS module says:
var dirPath;
if (isIOS) {
dirPath = fileSystemModule.knownFolders.ios.library();
dirPath = fileSystemModule.path.join(fileSystemModule.knownFolders.ios.library().path, '/Application Support/myapp/images/image1.png');
I'm able to write and read files from here from TS code fine. However,
2) For displaying these in my page, for an image in my html, I have src='~./Library/Application Support/myapp/images/image1.png' and it's not able to find them. Any suggestions appreciated.
There are a lot of docs saying how to write files locally using currentApp(), but none talk about how this doesn't work in the Release version of IOS.
I have followed this docs in implementing in taking a photo and picking a photo in Xamarin. I was able to implement and worked in picking a photo but I have error in using the function TakePhotoAsync() which is used to open the camera to take picture. I have googling for possible solutions but nothing works for me. I did not modify any permission or add from the current which work on Picking a photo.
It crashes in this line:
var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
{
PhotoSize = Plugin.Media.Abstractions.PhotoSize.Medium
});
I am testing in the emulator: AVD_for_Galaxy_Nexus(Android 6.0 - API 23)
Exception:
System.ArgumentException: Unable to get file location. This most likely means that the file provider information is not set in your Android Manifest file. Please check documentation on how to set this up in your project.
By the way, I did solve the error by downgrading the version of the plugin to Xam.Plugin.Media 3.1.2 from Xam.Plugin.Media 3.1.3. I didn't change anything in my codes.
I'm having a problem when deploying my Ionic2 app on iOS real device. Testing it in web and Android real device works fine but when running my app on a iPhone I have a problem when parsing data retrieved from reading a local file (using #ionic-native/file).
After checking that the file exists, I read it like this:
this.file.readAsText(this.file.cacheDirectory + 'epg/', fileName)
.then( (data) => {
console.log('read file - then 1');
console.log(data);
this.channels = JSON.parse(data);
console.log('read file - then 2');
Debugging it in Xcode console I see this:
"read file - then 1"
"(empty string)"
"TIC Read Status [2:0x0]: 1:57" (4 times)
It never gets to the second log, but it neither has an error in the logs....
When I run it in android device, I get the first log, then all the data read from the file and then the second log and everything works fine but in iOS it stops in the 'JSON.parse' line without error but the device continue in a loading state and it never stops. Seems like there were an error but no error detected...
Any help? Thanks!!!
Check out this page: https://ionicframework.com/docs/wkwebview/#rewriting-file
You should try using normalizeURL(this.file.cacheDirectory + 'epg/')
Solved it removing the platform (ionic cordova platform rm ios) and installing it again!
I use the forge.file.saveURL to save a JSON object to the local file system, then I use forge.file.String to read the contents and display them on the device.
This works just fine on IOS and Android but it seem on Android after the device has closed the app or force closed it after being open for a while when I reopen the app and try and access the file I get the error message in the console
Native call file.string with task.params:
{"uri":"file:///storage/sdcard0/Android/data/com.phonegap.conbop/files/Download/594f9cdc-a791-40ef-a40b-75d16948a9ca"}
[DEBUG] Returned: {"content":{"message":"Error reading
file","type":"UNEXPECTED_FAILURE","subtype":null},"callid":"508FE1AD-EB74-4377-8FDA-AA7318457B5C","status":"error"}
I have tested the IOS and this does not seem to happen there, its almost like the file is getting corrupt or something but what is strange is a also use the file.saveURL to save images etc and those work just fine so I'm not sure why this one is having issues.