Flutter cannot retrieve iPhone file (sometimes) - ios

I have an app that lets the user pick local files and send them to the backend to add to their vault. To pick the file, I use FilePicker.platform.pickFiles. It seems to work fine, the file is picked and when it's an image we show a preview to the user (and it always works).
Once the user has picked all their files, we create a list of Future to try and send the files to the backend, then we await the list to resolve. If we deem a picture is too big, we reduce the size using FlutterImageCompress.compressAndGetFile.
This usually works, but on iPhone, there is a seemingly random behavior where the file isn't found and an error is thrown : Cannot retrieve length of file, path = '/private/var/mobile/Containers/Data/Application/3600F812-FD42-4441-A654-766991E45E04/tmp/com.feykro.myApp.int-Inbox/myFile.pdf' (OS Error: No such file or directory, errno = 2)
The file in the example works just fine in other instances, the problem seems random and not directly tied to the file I chose. The problem is not exclusive to pdf files either, as it also occurs on images even though they're picked correctly and displayed in the app.
Any idea what could be causing this ?
Thanks in advance !

Related

UIDocument + NSFileWrapper + iCloud = Strange Behaviour

I’m trying to write a fairly simple UIDocument based app and I want the data for each document to be a parent directory containing some image files.
To get started I chose to use Apple’s ShapeEdit sample as that seems to handle lots of the complexity of having files in iCloud. It does only use a single plist file for it’s document data though.
As a first step I got the ShapeEdit app up and running on my iPad and everything works as expected. I have the app and iCloud Drive app running in split view on my iPad so I can see the files ShapeEdit is creating. As expected each time I add a new document a new file appears in iCloud Drive.
To migrate the ShapeEdit app to use more files I moved over to using NSFileWrapper. When ShapeEdit creates a new document it copies a template file. I changed this to just create a new folder using
try fileManager.createDirectoryAtURL(writeIntent.URL, withIntermediateDirectories: false, attributes: nil)
I then modified the UIDocument code to have it read from a NSFileWrapper like this
override func loadFromContents(contents: AnyObject, ofType typeName: String?) throws {
guard let data = contents as? NSFileWrapper else {
fatalError("Cannot handle contents of type \(contents.dynamicType).")
}
fileWrapper = data
I also hobbled some of the other functions in the document to return a simple image for thumbnails, just to get it running and set LSTypeIsPackage to true. Finally I left the shape selection UI there to trigger a document update with
updateChangeCount(.Done)
Again this all works fine. Creating a new document creates a new Directory named Untitled.shapeFile and it shows up in iCloud. Now for odd thing number one. If I open that document and tap on the shape selection UI to trigger a change and wait a few seconds another Untitled.shapeFile directory shows in iCloud. In fact every time I do this I get another one. These seem to only be local to the iPad though as iCloud on my mac only has one copy. Deleting any of these directories within the iCloud Drive app causes all of them to be deleted.
For the next step I added little bit of code to the UIDocument loadFromContents() function
fileWrapper?.addRegularFileWithContents(UIImagePNGRepresentation(image!)!, preferredFilename: "MYImage.png")
so every time the document is loaded it adds another image file to the parent directory.
To begin with this does exactly as I hoped. Open the doc, tap the shape select UI see the MyImage.png file turn up in iCloud Drive inside Untitled.shapeFile. Keep closing and opening and I get more MyImage.png files with some junk on the front which, I assume, is iCloud’s way of preventing files with the same name so all is well there. Except for strange thing number two. Shortly after the MYImage.png file shows in iCloud Drive another one pops up called MyImage 2.png. If I keep closing and opening all the versions of MyImage also get a copy with a 2. My edited version of ShapeEdit never adds any image files with this name format. These extra images do show up on the mac as well so they aren't the same strange phantom copies as the main document directory.
So two things
1) Why do I get multiple copies of the Untitled.shapeFile doc which seem to actually be the same thing?
2) Why do I get extra copies of my image file with a 2 added that my app never had anything to do with?
Any suggestions welcome before this drives me crackers.

resolveLocalFileSystemURL on iOS using different App UUID each time

I am using Cordova fileSystem to download some mp3 Audio files thru my App. I am using cordova.file.cacheDirectory and creating a subfolder within that to store the downloaded files. However, the problem is that each time I make some changes and run the App on iPad I get a different path.
The usual pattern is
file:///var/mobile/Containers/Data/Application/13E9EA83-94A2-4950-811E-E491AC176‌​A78/Library/Caches/MyFolder/MyFiles.mp3
However the long number (App UUID) is not consistent. It changes each time I run the App on iPad through Xcode. Because of this I am unable to access, play, delete any files downloaded during the previous session because I am saving the file names with absolute path in a separate file for quick access in the future.
After some R&D I have realized that even though UUID changes each time, it actually refers to the same location. Hence the solution is not to save absolute path and instead, save only the Path beyond "/MyFolder/" in the above example. Each time when the App is run, the Base Path, which is actually file.cordova.cacheDirectory, should be appended to each entry to obtain the absolute Path to the file.

iOS file upload - original filename

I have a simple HTML file upload snippet that works under iOS as well. However my problem is that the filename of the uploaded file will always be 'image.jpeg'. Is there a way to get the original filename - i.e. 'IMG_0001.jpg' instead? The major issue is that if I have 2 files selected they both have the name of 'image.jpeg' as opposed to their unique names.
Safari on iOS will always make the name of the uploaded file image.jpeg, presumably for security/privacy purposes. You need to generate your own name for the files, which is a good idea in general for uploaded files: you never want to trust the client too much.
If you are targeting more than just Safari on iOS, you will still need to handle this case because it is reasonable that people might upload multiple files with the same name, but originally located in different directories.

Saving xml file on iOS (pugiXml and Cocos2d-x)

Recently I've ran into a big problem with pugiXml (used within cocos2d-x engine).
Shortly, I've made a quiz game (in mentioned Cocos2d-x). I keep my questions (and some other data) in an Xml file. On new game they get parsed and inserted into dictionaries. If a question is answered, a short string indicating the answer (whether it was good or not - Y/N) is inserted into that Xml file (below that particular question). Later, I use this data to show statistics (I count the percentage of questions with good answers - which is counting the amount of Y's divided by the amount of questions, multiplied by 100).
I use:
CCFileUtils::sharedFileUtils()->fullPathForFilename(o_QA);
to get the file and later
pBuffer = CCFileUtils::sharedFileUtils()->getFileData(fullPath.c_str(), "rb", &bufferSize);
to put file into buffer and
pugi::xml_parse_result result = doc.load_buffer(pBuffer,bufferSize);
to parse data and start working on it.
Finally, I save the file with:
doc.save_file(fullPath.c_str());
Android:
It's working very well, although I had to copy the file with questions to /data/data/app/Files/ . Still, It's keeping the changes on many devices.
iOS:
Unfortunately, on iOS it's not working. Data get loaded and parsed (which means, that I can actually play the game), but they are not saved. I've tried moving the files to other folders (started from Resources/Documents, then main Resources folder, Resources/Library/Application Support). It's still not saving the data and I don't know what to do. The result is my statistics not being counted well (it doesn't matter how you answer the questions - all of them are false, because the Xml file is not being updated).
Did anyone run into similar problem?
Can you, please, help me?
I am doing something similar in my own app, using both pugixml and cocos2d-x. So I can confirm this combination works well.
Rather, because on iOS you cannot both read and write the data from and to the app bundle (which is read only), you will need to implement a simple check in the writable document directory - If you a saved file there, load it, if not, load from the app bundle.
So in essence for loading, if your saved filename is "my_save.xml", here is an example flow:
1) Construct a path for your save file in the writable folder by concatenating the writable folder path + your filename. CCFileUtils should have something like getWritablePath() for that.
2) if the file exists in the folder, load it. Otherwise, go to 3).
3) Construct a path to your original data file from the app bundle, using CCFileUtils::sharedFileUtils()->fullPathForFilename(). Load the file from there.
For saving, simply do step 1 and save the file there.

Installing applications OTA

I have a system set up to download jad files on users' Blackberries, but it only works intermittently, and seemingly randomly. If the user clicks on the link within their BlackBerry browser, 95% of the time on the first try an error message will pop up saying there was an HTTP 500 error (which our server never returns).
Viewing the details of this message within the blackberry browser, it says nothing but java.lang.nullpointerexception which, again, could not have come from our server (running apache/php).
However, if the user clicks on the link a few more times, or navigates away and goes back to that page, it suddenly works. No change on the server, it just shows the application install screen. Unfortunately, this doesn't always work; sometimes the error 500 just keeps showing up.
The link is rather long (containing an sha hash as a token as part of the URL), but I would think that a long URL would either always be broken or always work, not work intermittently.
The link uses a php script to download the jad and cod files. Linking to the files directly rather than using the script seems to work more often (I haven't determined if that also ever has an error 500 or not), but I can't find any issues with the headers. The content type is set correctly and, like I said, if the headers were an issue, I'd think it would either always work or always break.
Any clues?
You may be able to shed some light on the problem by looking at the event logs, which you can get using JavaLoader:
javaloader -u eventlog > event.log
Search for NullPointerException within those logs and you'll be able to see what's causing it.
I can't explain the intermittent behaviour, but I had a similar situation where I was getting the java.lang.NullPointerException in the browser details. Unfortunately, the Event Log (as dumped by javaloader.exe) or by viewing on-device using Alt-L-G-L-G didn't show the exception.
(I'm using bb-ant-tools and JDE 4.6.1.) When the signature tool ran, I noticed that there were two .cod files being signed and I can see both of them inside the .jar file the compiler creates. But the output written was a single .cod file of size 92306 bytes that was not a .zip of smaller .cod files. The compiler somehow was not able to create a .cod that contained siblings. For comparison, compiling the project with JDE 5.0.0 created sibling .cod files that were able to be loaded over OTA.
My project included an .mp3 file of 53542 bytes which I happened to not require. After removing it, the 4.6.1 compiler outputted a single .cod and I was able to successfully download it via OTA.

Resources