How can I add files to the iOS simulator?
Asked Answered
F

22

81

Can someone tell me how can I add files to be used as test files for uploading from the simulator?

I dragged a PDF to the simulator and this is all I get. Can't really tap the Add button.

screenshot of the "add item" dialog

Flathead answered 20/2, 2018 at 11:26 Comment(1)
Tap "On my iPhone" to add the file to Files on the simulated device. Alternately you can login with an iCloud account (I suggest using a test account), then iCloud storage is available.Sensorium
G
13

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.

Gadget answered 20/2, 2018 at 11:30 Comment(3)
So there's no way to pull local files on device?Flathead
Does iCloud Drive show files in your simulators after login? No success for me ... empty iCloud Drive (logged-in and option enabled in settings)Ariminum
not work, icloud after signin, is empty fileLudeman
R
76

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:

  1. Open the Files app on the Simulator and navigate to "On my phone" folder.
  2. (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.

Roundhouse answered 9/10, 2019 at 7:42 Comment(17)
So I did this with a csv file. The file appears but I can't click on it. When I click on it from the files app nothing happens. When I click on it from my app, using a UIDocumentPickerDelegate, nothing happens (iOS 13.1).Simplistic
@BenNoland Sadly, same here. It works on a real device, but not on the Simulator. While I found a way to add files, it seems that the simulator does not support this kind of interaction. It's really too bad.Roundhouse
This solution worked perfectly for me on a simulator on iOS 13.3. Thank you for such a detailed answer!Kevenkeverian
Brilliant idea, perfect workaround. Works like a charmPrint
Thanks! this was the solution for me!!, but the full path are hidden by default , just CMD +SHIF + " ." to see all the hidden files inside your userFolderUncle
@Uncle Or, you can open the Go menu (from Finder) and hold 'Options' key, the Library will appear. From there, you can freely navigate. I don't think it's really 'hidden'. Hidden files start with '.' as the first character. I guess hiding the Library is a Finder preferences settings.Roundhouse
@Roundhouse can you please share how did you manage to add a file? I'm trying to add a plain-text document to the simulator and I run into the same issue as you and Ben did!Houchens
@AkashAgarwal My answer focus mainly on locating the folder (in the Finder) of where the Simulator files are located. All that's left is to copy a file to that location.Roundhouse
@Roundhouse thanks for the reply. I was primarily referring to your comment in reply to Ben Noland on this answer :)Houchens
@AkashAgarwal I filed a bug report to Apple about that at the time. They fixed many issues in the Simulator regarding the Files app since though.Roundhouse
This should be the top answer. A few notes. For me CoreSimular was single word with no space. I.e /Users/xxxx/Library/Developer/CoreSimulator/Devices/2FAE1010-A0DC-42E5-98B4-899B86FD6A10/data/Containers/Shared/AppGroup/E92C52DF-233E-435F-A957-62F3B4FA2565/File Provider Storage/MyNewUploadsFolderBenzophenone
One can do folder search by name. For example I did: find /Users/xxxx/Library/Developer/CoreSimulator/Devices/ -name "MyNewUploadsFolder" to reveal the folder path.Benzophenone
@ArvindK. Thank! I did notice 'Core Simulator' is now 'CoreSimulator' (no spaces). I'll update the answer.Roundhouse
Thanks! This should be the accepted answer.Holding
To get images/videos into the Photos app, open the video in Files and click "Save Video" and it will save to PhotosVivie
If you're still not finding it, just create a Folder in iOS Simulator Files app with a random string like: "Qhdtbixpx". Then search for it in Macbook Finder, you'll easily find the folder and be able to add files there. https://mcmap.net/q/261701/-how-can-i-add-files-to-the-ios-simulatorCarlo
this helped me so much because saving files in Xcode 16 Beta 3 simulator is not working at all.Sharkskin
F
36

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

Frohne answered 14/9, 2018 at 10:44 Comment(7)
This is by far the best answer. Just worked for me with a pdf.Jujutsu
This should be the most voted answer! So simple and powerful!Pronounce
this used to work just fine, but since xcode11/ios13, it doesnt seem to work -- no errors but no files. i transferred root certificates via icloud, seemed easier than dealing with finder paths like in other answers.Frohne
On Xcode 12, iOS 14: It just successfully added an image into the photos app, xcrun simctl addmedia Booted 20200913_130141.jpg, not sure why it doesn't work for you Joe. Dragging the image also worked 😛Bicollateral
interesting 😏 just tested the drag and drop on xcode 12 ios 13.2.2 and it worked fine. i guess it's something with the simulator that came bundled in xcode 11, who knows. but the xcrun does the job anyway.Frohne
CSV file worked perfectly for me! xcrun simctl openurl booted 'file:///Users/myuser/Desktop/file.csv'Fichte
Still works!! Xcode 15Magdalenamagdalene
P
23

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.

enter image description here

Plasticize answered 4/1, 2019 at 15:9 Comment(2)
This is super! A nice workable solution for a developer :)Bonita
Great idea! I do think it's annoying that Apple doesn't provide a way to just mock some file data in the simulator, but alas it is Apple, haha.Rusell
B
17

The answer is much simpler, just zip the file in macOS and drag and drop into the simulator, it will automatically be opened in the files app and there you can decompress and use it.

Blindworm answered 12/9, 2022 at 11:16 Comment(1)
You gangster broVanderbilt
G
13

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.

Gadget answered 20/2, 2018 at 11:30 Comment(3)
So there's no way to pull local files on device?Flathead
Does iCloud Drive show files in your simulators after login? No success for me ... empty iCloud Drive (logged-in and option enabled in settings)Ariminum
not work, icloud after signin, is empty fileLudeman
D
11

Multiple steps but works

1) Create a new project Document Based app and run it on the same simulator.

enter image description here

2) Now open safari in simulator and google for pdf. Select save to files option from action sheet and then select you newly created app folder.

enter image description here

enter image description here

Disconcerted answered 9/6, 2019 at 9:12 Comment(4)
I recommend to rename "multiple" to "just 2" :) because it really took 2 minutes to do it. which is fast to be honest. also you'd increase the conversion rate of people to complete these steps :)Candicecandid
I get an error that says "The operation couldn't be completed. No such file or directory"Elide
Just found a solution, I had to go to the root folder of my computer and send files from there.Elide
This should be the accepted answer. Spot on. No frillsPollypollyanna
S
11

Dragging and dropping a file from Filer onto the simulator has an issue with macOS Catalina (I am currently on 10.15.5 and seeing it). Apparently, if the source file is in a "restricted" directory (such as Downloads), it won't transfer.

Put the file in your $HOME directory and then drag it (or use Share button to share to simulator). See thread: https://forums.developer.apple.com/thread/126307

Stratigraphy answered 12/6, 2020 at 17:35 Comment(2)
Apparently this drag and drop problem got fixed. Now on macOS Catalina 10.15.6 and XCode Version 12.0.1 (12A7300) and Simulator Version 12.0.1 (940.16).Stratigraphy
this is easiest solution that worked, Thanks.Lupien
M
11
  1. Use safari on your simulator

  2. Vist https://en.unesco.org/inclusivepolicylab/sites/default/files/dummy-pdf_2.pdf for a dummy pdf

  3. Click the middle icon as shown below enter image description here

  4. Click on save to files and you are done enter image description here

Mildew answered 27/7, 2020 at 8:47 Comment(1)
BRILLIANT!!! How noone thought of that before! Thanks!Annalee
B
11

Currently, you can click using your right mouse button on the folder / image that you wanna send and select from the list Share -> Simulator

enter image description here

then you'll see a list of options:

enter image description here

Bravar answered 18/7, 2022 at 12:9 Comment(3)
This is the easiest way to add files to any iOS simulatorAlethiaaletta
Where are the file(s) sent to? When I did this, the file does not show up in the Files folder.Rosanarosane
OMG, you are genius! The easiest way to send specific file to simulator!Asarum
I
8

I found one easy way to do it with a little help from Python3 (I suppose all Macs should have it installed by default.

Here are the steps.

  1. Open your terminal app
  2. Go to the directory which contains your file(s), e.g.

enter image description here

(I need to transfer a self-signed certificate to the simulator)

  1. Run python3 -m http.server to launch an HTTP server
  2. Open Safari in your similator, and open http://localhost:8000, you should see a list of file(s), e.g.

enter image description here

  1. Download the file(s) you need from Safari
Invulnerable answered 24/11, 2021 at 6:2 Comment(1)
Might as well drag and drop the zscaler cert into the simulator manually, seems easierGoldiegoldilocks
P
7

Just drag and drop on the simulator window. That's it.

Pock answered 3/6, 2020 at 12:24 Comment(0)
P
6

Just open Safari and download something.

Pock answered 28/12, 2019 at 12:58 Comment(2)
This is the ONLY solution that worked for me! Honestly, I love you right now :-)Rachael
didn't work for me. The 'Add' button is still disabled inside "On My iPhone" folderPollypollyanna
S
4

Simple answer:

  1. Put the simulator into home screen
  2. Drag and drop a file onto the simulator home screen
  3. If the file is associated with an app, it will open that app and you can save the file using that app. If not associated with any app, then the Files app will open and you can choose to save "On My iPhone" or elsewhere.

Side note: I was working on an app that had the file association, but did not save the file. So I deleted my app from the simulator, copied the file into the Files app, then restored my app to the simulator. Mention in case it helps others.

@Palli Kominak gave this answer but left it in a comment under one of the other answers.

Sly answered 5/1, 2021 at 20:50 Comment(0)
S
3

Maybe I am a bit late but after reading all complex answers, here's the simple and quick solution. Since you are using simulator and you want to access some of the files/images via simulator, follow these steps,

  1. Minimize your current applications and search for photos app,

enter image description here

  1. Open photos app in simulator, it will just look like real iPhone

enter image description here

  1. Now, just drag and drop an Image or File of any type. If it is an image it will appear in Photos else it will open the Files app it self and will ask you the permission to save file on device. Something like this, ]enter image description here

Simple, now use it just like it acts on real device.

Saccharine answered 17/10, 2019 at 11:24 Comment(2)
You don't even need to open Photos app. It works similar when dragging file to simulator home screen. BUT when you have registered document type with an app and you are trying to drag file of that type it will open the app right away and not Files app.Smokejumper
Instead of opening Photos just open the Files app on the iOS simulator and drag files(of any type) from Mac Finder onto that. It will accept them and allow you to place in the Files folder hierarchyShaefer
B
3

If you want to add some files to the "Files" app, first you have to find its directory in your mac, create a directory named for example, "MagicTheGreate", in the "Files" app, and then go to the command line and search the ./Library folder for the name.

find ~/Library/Developer/CoreSimulator/Devices/ -name MagicTheGreate

Now you can copy your files to that directory or the parent one, it will show up on your simulator. The bad news is, the directory changes once in a while and you have to find it again.

Barranca answered 30/6, 2022 at 13:3 Comment(0)
S
1

The easiest way is to create a Folder in iOS Simulator Files app with a random string like: "Qhdtbixpx". Then search for it in Macbook Finder in the Devies folder (Cmd + G : /Users/user/Library/Developer/CoreSimulator/Devices/) , you'll easily find the folder and be able to add files there.

Screenshot of ios Simulator with custom folder and search to find it in Finder

Sandbag answered 4/3 at 11:33 Comment(0)
H
0

You can put files in your APP's local /documents folder. To get the directory name in your APP do

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *fileNam = [NSString stringWithFormat:@"myImage.JPG"]; NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent: fileNam];

you will also need to enable file sharing by putting

UIFileSharingEnabled

in info.plist

You can copy any file you like into the simulator's local documents directory.

Hereinafter answered 10/12, 2021 at 14:6 Comment(0)
Q
0

In my case I wanted to upload the proxy certificate with shell without any confirmation dialog, so I came up with this:

SIM_UDID=$(xcrun simctl list devices | grep Booted | awk -F'[\(\)]' '{print $2}')
find ~/Library/Developer/CoreSimulator/Devices/$SIM_UDID/data/Containers/Shared/AppGroup/ -name "File\ Provider\ Storage" -exec cp path_to_some_file {} \;

First line is simply getting the UDID of a booted simulator and the second one is finding all File Provider Storage directories inside booted simulator and then copies the file to them.

Copied file should appear inside Files app.

Of course if one wants to specify the simulator or multiple simulators are booted, grep Booted can be changed to grep <name_of_the_device>


UPDATE

this lines can copy the file directly just to LocalStorage, no other directories in the contrary to the previous solution.

SIM_UDID=$(xcrun simctl list devices | grep Booted | awk -F'[\(\)]' '{print $2}')
FILES_PATH=$(xcrun simctl listapps $SIM_UDID | grep LocalStorage | awk -F'"' '{print $4}' | sed -e "s/^file:\/\///")
cp path_to_some_file $FILES_PATH/File\ Provider\ Storage

This approach makes use of xcrun simctl listapps command which return the AppGroup id of FileProvider.LocalStorage

Quinque answered 9/5, 2022 at 12:37 Comment(0)
A
0

If you want to file .app you can file follow this step by step.

  1. open Finder -> Go -> Computer -> Untitled -> Users -> (FolderNameYourMacbook)
  2. shift + command + .
  3. then file hidden is show open this path continue : /Library/Developer/Xcode/DerivedData/YourAppName-xxxxxxxxx/Build/Products
  4. try drag file .app on Simulator

nearly my result image this. enter image description here

enter image description here

Apish answered 17/3, 2023 at 15:27 Comment(0)
Z
0

create a folder, copy your file to the folder and then drag the folder and drop it in files in simulator or home screen, audio file will be copied in simulator.

Zaibatsu answered 12/6 at 10:4 Comment(0)
S
-1

You can browse to Library/Developer/CoreSimulator/Devices/Your App ID/data

This is basically your phone's storage. You can copy the files to any of the location you like and it will appear in your simulator.

Spinning answered 20/2, 2018 at 11:43 Comment(2)
I have accessed the files of the application this way, as suggested in this answer: #47950738 copying files on to that location should in turn appear in your simulator.Spinning
Correct path = ~/Library/Developer/CoreSimulator/Devices/(SIMULATORID)/data/Containers/Data/Application/(APPLICATIONID)Mitra
E
-1

You don't need any hacks for that. Steps:

  1. Open IOS Simulator on your Mac.
  2. Go to Finder
  3. Choose any file
  4. Click the right mouse button to open "perform tasks menu"
  5. Choose Simulator
  6. Then, choose the directory on the simulator to save file. You can also change the file name or then create a new folder for your files.
  7. Click "Save" on the simulator. Done!

1 screen 2 screen

I use it always, it works perfectly. You can do same for media files(pictures/video). The official info from Apple: https://developer.apple.com/documentation/xcode/sharing-data-with-simulator#Add-files-to-Simulator

Externalism answered 23/9, 2023 at 0:15 Comment(1)
Please do not post duplicate answers.Interpolation

© 2022 - 2024 — McMap. All rights reserved.