UserGuide

Copy Files to iOS Device

From Xojo Documentation

There are two ways to copy files to your iOS device. You can use a Copy Files Build Step or you can use iTunes after you set your app up for File Sharing.

Using Copy Files Build Step

You can use Build Automation to copy files into the Resources folder of your iOS app. When the app is running on the device, you can access the files directly or copy them out of the Resources folder to another location (this is required to write to the files as the Resources folder is read-only).

fa-info-circle-32.png
The Resources folder is read-only! If you need to modify files copied in the manner, you first have to copy them out of Resources and into another location, such as Documents.

Here are the steps to set up a Copy Files Build Step:

  1. A Copy Files Build Step is added to your project by selecting Insert ↠ Build Steps ↠ Copy Files. In the center area you can drag the files (or folders) you want to copy to the device.
  2. In the Inspector, change the Destination to "Resources Folder".
  3. You can also give this Copy Files step a name. It is helpful to give it a name that describes what it is copying.
  4. Drag the Copy Files Step from its original location onto the the iOS item in the Build Settings section of the Navigator. Be sure that it is after the Build item, but before the Sign item.

Any Copy File Build Steps that you add should be before the "Sign" step so that the files you have copied into the app bundle are properly signed for submission to the App Store or deployment to a device.

When you Run or Build your project, these files are now copied to the Resources folder. Your app can access the files using Xojo.IO.SpecialFolder.GetResource, which returns a FolderItem that points to the specified file in Resources. For example, if you've copied a file called MyFile.html to Resources, you can access it like this:

Var myFile As FolderItem = SpecialFolder.GetResource("MyFile.html")

Now that you have a FolderItem, you can use the file directly or you can copy it to Documents so that it can be modified by using the CopyTo method of FolderItem:

Var myFile As FolderItem = SpecialFolder.Resource("MyFile.html")

If myFile.Exists Then
Var destFile As FolderItem = SpecialFolder.Documents.Child(myFile.Name)
myFile.CopyTo(destFile)
End If

You can also copy files to a folder within Resources by specifying a value for the Subdirectory property in the Inspector for the Build Step.

You still use the Resource function to get the folder, which you can then iterate through to access the files:

Var myFolder As FolderItem = SpecialFolder.Resource("MyFolder")

If myFolder.Exists Then
For Each c As FolderItem In myFolder.Children
// use c as necessary to access or copy the file
Next
End If

iTunes File Sharing

When you enable File Sharing for an iOS app, you can use iTunes to sync files to or from the app's Documentation folder on the device.

iTunes File Sharing Apps

In order to enable File Sharing, you'll need to add a custom plist to your project. Copy the text below into a file and name it Info.plist and then drag it into your project:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>UIFileSharingEnabled</key>
<true></true>
</dict>
</plist>
iTunes File Sharing App List

Build your app and deploy it to a device. Now any files that your app creates in the Xojo.IO.SpecialFolder.Documents folder are now accessible through iTunes when your device is connected to the computer via USB.

iTunes File Sharing App Files
  1. In iTunes, click on the device and select Apps in the Settings navigator on the left.
  2. Scroll down to the File Sharing section and you'll see a list of all the apps on your device that support file sharing. Find your app and click on it to see a list of the files in the Documents folder.
  3. You can click on individual files to save them to your computer or you can choose to add files to the list. Files you add are copied to the device when you sync it using iTunes.
<Title>