UserGuide

Copy Files to iOS Device

From Xojo Documentation

Revision as of 22:22, 2 March 2021 by Gperlman (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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 SpecialFolder.Resources, 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.Resource("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
fa-info-circle-32.png
The Framework folder is also supported. Use this folder when you need to include a framework file to which you plan to access via Delcares.

Finder File Sharing

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

In order to enable File Sharing, click on iOS under Build Settings in the Xojo IDE Navigator then in the Inspector, click the File Sharing switch.

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

  1. In the Finder, open a Finder window then click on the device and click the Files button to switch the listbox below it to show apps that save files.
  2. Find your app and click to expand it to see files that are in the app's 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 the Finder.