How to view document directory in iOS emulator? - ios

I wrote a React Native application that downloads a file and saves it to the document directory.
The application gives me a path like this:
/Users/laurent/Library/Developer/CoreSimulator/Devices/SOMEID/data/Containers/Data/Application/SOMEID/Documents
and using macOS Finder I can see that the file has indeed been created.
My problem is how to view this file from the iOS emulator? If I click on the Files app for instance, I can't see that file. Basically is this file available to the user from somewhere and, if so, where? Or maybe I need to save it to a different directory?

Your iOS app is sandboxed by default, meaning that no other app can access its resources. The simulator is very different since it runs on your mac and stores your files there so you can easily access them.
That said, it is very simple to expose your Documents folder by adding the appropriate keys to your app's info.plist:
Add the UISupportsDocumentBrowser key to grants access to all the documents in the app’s Documents directory and have them appear in the Files app (iOS 11 and above). When this is enabled, other apps can also have access to your documents when using UIDocumentBrowserViewController.
Add the UIFileSharingEnabled key to expose the app’s Documents directory to the desktop iTunes app. When combined with the LSSupportsOpeningDocumentsInPlace key, it will also enable Files app support and document browser as mentioned above.

By default any files your application downloads are only available from within that app, they will not show up in the file system of the device.
As far as I know there is no react-native library which allows you to access the iOS file system, so you will need to make use of native iOS code to solve your problem. Besides pointing you to the official documentation, there is not much more I can do to help.

Related

How to Flutter write files to iOS iPhone "Find on my phone" Folder

My goal is to allow my app user write and read files created by my app. For example, write and then allow the user to email a .CSV file of data, or a .GPX file of location data. And on the other end, import a file back into my application.
I have successfully deployed an Android version of my app (Plant List GPS) which uses Flutter plugin path_provider 1.6.10. On Android, I successfully use external directories, but this external directories solution is not available/allowed for iOS. I find on iOS I can create subfolders and write to the getApplicationDocumentsDirectory, but when I try to find/read that file from a file picker solution, it cannot be seen. I understand this is proper behavior to protect sensitive app files in this folder location.
I also understand that iOS has the concept of an app Sandbox and appreciate its objectives. I'm not trying to defeat it, but find a way to access/use it in my Flutter app. I have played around in writing test data files to various places on the iOS file system using the iPhone 11 simulator on my MacBook, but can not get any file to appear in the phone "Find on my phone" dialog.
Any help in solution approach or technique is appreciated.
I think you need to add both the keys into info.plist, and then files from your app directory will be visible on Files app!
LSSupportsOpeningDocumentsInPlace
UIFileSharingEnabled

How to Save video file to iOS Files App using UIDocumentInteractionController

I want to write one or multiple mov/mp4 files to iOS Files App but can not find how to do it with UIDocumentInteractionController. Looking for a sample code to copy files to a subfolder of my app in iOS files app using UIDocumentInteractionController. See sample screenshot to understand what I want.
You will first need to create a File Provider extension for your app if you are storing the files remotely.
If they are only being stored locally on device you can achieve this by (in the Info.plist) either setting the UISupportsDocumentBrowser key, or by setting bother the UIFileSharingEnabled and LSSupportsOpeningDocumentsInPlace keys.
As explained in this documentation:
These keys enable other apps to open and edit the contents of your Documents directory in place. Your files also appear in both the Files app and the document browser. For more information, see the UIDocumentBrowserViewController class.

how do you export a file out of iOS when using cordova file plugin?

I'm using https://github.com/apache/cordova-plugin-file on an iOS device and I'm attempting to write files. I can write files ok but according to the iOS file system layout, all of these locations where I write are private.
I'm guessing this is due to Apple's sandbox security model so that each app can only see files within its own bundle. This is different on Android where the cordova.file.externalRootDirectory location is NOT private.
My question is: How do you export this file out of an iOS device?
The file that I'm writing contain a long list of GPS locations captured by the Cordova app I'm running. I can see the file in XCode, under devices, and export the whole container in order to view the file. But there has to be a better way to transfer the file out. Is it possible to transfer a Cordova generated file out of an iOS device to the computer -- maybe through iTunes or iCloud?
I've also tried using cordova.file.syncedDataDirectory and I can write files to that location just fine. But the file doesn't appear in my iCloud account.
You have to use itunes file sharing
File-Sharing Support
File-sharing support lets apps make user data files available in
iTunes 9.1 and later. An app that declares its support for file
sharing makes the contents of its /Documents directory available to
the user. The user can then move files in and out of this directory as
needed from iTunes. This feature does not allow your app to share
files with other apps on the same device; that behavior requires the
pasteboard or a document interaction controller object.
To enable file sharing for your app, do the following:
Add the UIFileSharingEnabled key to your app’s Info.plist file, and set the value of the key to YES.
Put whatever files you want to share in your app’s Documents directory.
When the device is plugged into the user’s computer, iTunes displays a File Sharing section in the Apps tab of the selected
device.
The user can add files to this directory or move files to the desktop.
Apps that support file sharing should be able to recognize when files
have been added to the Documents directory and respond appropriately.
For example, your app might make the contents of any new files
available from its interface. You should never present the user with
the list of files in this directory and ask them to decide what to do
with those files.
So, add the UIFileSharingEnabled to your info.plist and set it to YES
https://developer.apple.com/library/ios/documentation/Miscellaneous/Conceptual/iPhoneOSTechOverview/CoreServicesLayer/CoreServicesLayer.html#//apple_ref/doc/uid/TP40007898-CH10-SW30
#jcesarmobile's answer helped me track down that I needed to enable file sharing in iOS. After confirming that I can enable file sharing by modifying the info.plist file in XCode, I found out that you could directly set UIFileSharingEnabled in the config.xml file but it's a bit more tricky because it's not a standard preference.
<gap:config-file platform="ios" parent="UIFileSharingEnabled" mode="replace">
<true/>
</gap:config-file>
After putting this in my config.xml file, it worked.
This is noted in the PhoneGap documentation, but I'll admit that it was really buried and a bit difficult to find.
http://docs.build.phonegap.com/en_US/configuring_config_file_element.md.html#Config%20File%20Elements

Having a bit of trouble with copying a local file on iOS

Having only worked with Android/web dev before I am having a hard time figuring out where in the world my file should be getting copied to in iOS. After reading the file browser documentation on the iOS developer resource page it says that each app is an "island" which contains its own folder system. If I want to have my user be able to copy a file from my app's sdk to their iOS device, where should I put it?
I downloaded a file browser app from the app store on my iOS testing device but am I right in thinking that there is no global file browsing system?
I am using a Cordova plugin called Asset2sd which works perfectly for me in Android, getting the root storage folder and downloading the file to there. It has no iOS alternative so I'm going to have to figure something else out, I just need to know where to start. Do I have to have the user access my app's internal files somehow? Can I create a folder for them to look in when they download my app? Some documentation or something would be wonderful. I am totally lost.
Thanks!
You're right. Apps don't have access to the global file system. Each app only has access to their own app directory. Here's in-depth information on Sandboxing: https://developer.apple.com/app-sandboxing/
In your app's sandbox, you basically can manage files as you want, i.e. download, copy, move, delete, etc. So you can create a folder Downloads in your apps documents directory and then display the contents of this folder for instance in a table view.
Related documentation/links:
https://developer.apple.com/library/prerelease/ios/documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/index.html
https://stackoverflow.com/a/6907432/967327
https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/iPhoneAppProgrammingGuide.pdf

Access to Application Support Directory on the device during development

One can physically access to the app's Document Directory on the device using iTunes File Sharing. What about the Application Support Directory? (The same way if the app is on the simulator)
Anyway one can do this?
Take a look at http://developer.apple.com/library/mac/#recipes/xcode_help-devices_organizer/articles/copy_app_data_from_sandbox.html
This should provide you with a package of all data within the App's sandbox

Resources