How can I add files to the iOS simulator?
The easiest solution can be found here: https://apple.stackexchange.com/a/299565/140768
Short summary: You need an application which is able to handle/store "documents". As suggested in the linked post just create an empty "Document based" app and run it once.
Since none of the given solution worked for me on Simulator of iOS 13/Xcode 11, (trying to add a file, dragged from the Finder would just give an error), I found a workaround.
This takes a bit of setting up at first, but you get free access to the actual folder right from the Finder.
When I drag a file to the Simulator (providing no other App handles the file), the Files App would open, but when I save, I get "Invalid argument error".
The solution:
The folder named "On my phone" in the Simulator is called "File Provider Storage". The aim is to find it in the Finder.
The full path:
username/Library/Developer/CoreSimulator/Devices/device_hash_folder/data/Containers/Shared/AppGroup/hash_folder/File Provider Storage/
(Note:
Up until to current version (13.1), the Simulator does not work for picking files. You can see the files, but nothing happens on click.)
Here are the steps:
- Open the Files app on the Simulator and navigate to "On my phone" folder.
- (Still in the Simulator) Create a folder with a unique name.
2a. You need to drag the screen down to reveal a '...' button. Pressing that will give you an option to create a new folder.
From here there are two ways
The short:
(In the Finder)
3a. Navigate to: username/Library/Developer/Core Simulator/Devices.
3b. Search for the folder you created. It should be in "File Provider Storage". That is the "On my phone" folder.
If the search does not find for any reason:
3a. Run an App and print (from Xcode) the path to documentDirectory
(using FileManager
).
3b. Open it in Finder and navigate back to "Containers folder".
3c. Navigate to Shared/AppGroup.
3d. You're looking for Shared/AppGroup/some_hashFolder/File Provider Storage/your_folder.
3e. Either look for modified date (folder created) of search for the folder name.
You can upload files to iCloud Drive of the account you are logged in with on the simulator, and access them by navigating to Browse/iCloud Drive inside the Files app or a file upload dialog.
updated answer:
since xcode11/sdk13 dragging files over the simulator does not work for whatever reason. you can send media files (jpg, png, mov, etc) via terminal with xcrun simctl addmedia <device> <path>
.
therefore, this command will add an image right into Photos app with no questions asked:
xcrun simctl addmedia booted pic.001.jpg <== booted is the current running simulator.
in my case, i wanted to send root certificates to allow connecting to local servers, which doesnt work with addmedia
. the solution was to force the simulator to navigate to the full macOS local path to the .cer file with safari
xcrun simctl openurl booted 'file:///Users/my.user/Desktop/my.cer'
as it turns out, launching safari and typing file:///Users/my.user/Desktop/my.cer
in the address bar will also work. use this for pdfs and other types.
original answer:
On XCode 9 simulator, if it's an image/video, simply drag it over the simulator. It adds it to the Photos app. Other types of files seem to randomly open the file browser and generally fail. :P