Search code examples
ioscordovacordova-plugins

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.


Solution

  • 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