How can I add files to the iOS simulator? - ios

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.

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.

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

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.

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.

Multiple steps but works
1) Create a new project Document Based app and run it on the same simulator.
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.

Use safari on your simulator
Vist https://en.unesco.org/inclusivepolicylab/sites/default/files/dummy-pdf_2.pdf for a dummy pdf
Click the middle icon as shown below
Click on save to files and you are done

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

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.
Open your terminal app
Go to the directory which contains your file(s), e.g.
(I need to transfer a self-signed certificate to the simulator)
Run python3 -m http.server to launch an HTTP server
Open Safari in your similator, and open http://localhost:8000, you should see a list of file(s), e.g.
Download the file(s) you need from Safari

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.

Just open Safari and download something.

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

Simple answer:
Put the simulator into home screen
Drag and drop a file onto the simulator home screen
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.

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,
Minimize your current applications and search for photos app,
Open photos app in simulator, it will just look like real iPhone
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, ]
Simple, now use it just like it acts on real device.

Currently, you can click using your right mouse button on the folder / image that you wanna send and select from the list Share -> Simulator
then you'll see a list of options:

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.

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.

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

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.

Related

How to open database sqlite file on iPhone real device?

I'm debbuging a app in my real device by cable. I've a iPhone 6. I want check my database and operate with sqlite3 to query my results. The other questions and tutorials explain to do this only in simulator but I'm using a real iPhone.
In AppDelegate, I prints the path of database:
print(NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last! as String)
/Users/myname/Library/Developer/CoreSimulator/Devices/DAE93E57-7004-45F6-9B93-E79CA1AEEEFA/data/Containers/Data/Application/D7A4F27E-6F11-4941-A1B0-0337ABF788AB/Documents
So, I take the path and access from terminal and access my database with sqlite3 DatabaseFile
But when I debugging in my device, the path that's printed not works. I tried use the printed path
cd /var/mobile/Containers/Data/Application/3257D423-C198-41A5-B29D-B31E99F84F34/Documents
/usr/bin/CD: line 4: cd: /var/mobile/Containers/Data/Application/3257D423-C198-41A5-B29D-B31E99F84F34/Documents: No such file or directory
This error happens because this is of iOS system, I think.
Xcode 10.1
In Xcode, go to Window > Devices and Simulators
In the Devices tab, select your application, then click on the Gear icon, then click Download Container...
Choose the location you want to save the file, then click on Save.
Go to Finder and find the .xcappdata file you just downloaded, then right click and select Show Package in Contents
The contents folder will open. Your .sql file is in AppData > Library > Application Support
I recommend downloading DB Browser for SQL File, and opening the .sql file with that. It works wonders!
In recent versions of Xcode (8 and up, I think), open this with Window menu --> Devices. Find the device in the device list and look at the installed apps list for the device. Each app that you've built and installed on the device will be listed (and maybe some others). Select your app and click the gear menu below the list. Clicking the "Download" item on the pop-up menu will enable you to copy the app's documents, library, and other directories to your Mac.
NB: This will download an .xcappdata file. Right-clicking on that and selecting "Show Package Contents" will allow you to drill down to the actual sqlite file.
Here's another way:
In debug mode, add a UIActivityViewController to share(copy) your SQLite database to a Mac or to a third party iOS app like SQLiteFlow(iOS), then you can debug your SQLite database.
Files that you've placed in the Documents directly should show up in File Sharing inside iTunes. You can download them locally and edit them there. You'll need to enable it in your app's Info.plist by adding:
<key>UIFileSharingEnabled</key>
<true/>
<key>LSSupportsOpeningDocumentsInPlace</key>
<true/>
The second key is actually only required if you want to enable showing these in the Files app.
You cannot access the .sqlite file in realtime when you debug your app using a device. Try accomplishing the same in the simulator and it will work.

With Xcode how can I export and share an app with a team member in the simulator?

So I have a teammate/client I need to share an app I'm developing.
They have android and a Mac Desktop so it would be shared through the Simulator only.
I've tried the archive app and there seems to be no way to get this work with the .ipa file which is generated.
Does anyone know of a way to achieve this?
Share the source code, then run the app in Xcode. Voila, it is now in the Simulator. They have to install Xcode anyway to get the Simulator so there really isn't any extra work involved other than building the app.
The .ipa is built for iOS devices and cannot run in the Simulator. I suppose you could transfer the binary to another simulator and get it to work, but I have not heard of anyone doing it that way and, frankly, it seems like potentially more work than "clone this git repo, load the project/workspace in Xcode, click the triangle button and wait a bit."
All you can do is you can just generate the .app file. But how you can run it on a simulator (applies only if Xcode 8.2 or above isn't installed), I really don't have the idea. If you want to have your .app generated, follow this:
Connect any iPhone with your mac
Open your project in Xcode
Wait for the setup (processing symbol files, blah, blah) to be completed
Run your project in that iPhone once
Now just build your project while the iPhone is connected
Search for the Product group (or, folder) and expand it
You will see a file (maybe more) named as your target (project name)
Right click on that file and select show in finder. And voila, you have your .app file
What generates the .app
You will need to have previously built the app for the intended target. The "Product" option in the top menu bar includes a couple of ways to trigger this including: "Build for" of "Destination".
Where is the .app?
The yourApp.app you are looking for is stored in: <<YOURUSERHOME>>/Library/Developer/Xcode/DerivedData/<<YOURAPPNAME-UID>>/Build/Products/Debug-iphonesimulator
Easier way to locate
Open you app in XCODE
Browse the file tree in the left sidebar
Expand the "Products" directory
Right click the .app file you want to share, and select "Show In Finder"
What you are trying to do is not going to work. You can not share the .ipa file for someone else to install it on an iPhone simulator.

Debugging Realm in iOS 8.3

I need to export .realm from Documents folder on real iOS device. Since iOS 8.3 Apple has disabled direct access to device's Documents folder how can I do that. For example I can send it by email, but is there any easier way?
You can download a device's container for your app from Xcode's "Devices" window with the following steps:
Connect your device to your Mac.
In the Devices window under Devices, select your device.
Under Installed Apps, select the app from the list.
From the Action menu (the gear icon), choose a menu item:
Show Container. A dialog shows the file contents of the container.
Download Container. In the dialog that appears, enter a location and click Save. The saved file will have a .xcappdata extension.
Replace Container. In the dialog that appears, select a file with a .xcappdata extension, and click Open.
Source: https://developer.apple.com/library/ios/recipes/xcode_help-devices_organizer/articles/manage_containers.html
You can still get to the Documents folder on a simulator. To find the folder of the simulator you're running on, first type the following in Terminal:
xcrun simctl list
And note the UUID of the simulator you're currently using.
Then navigate to ~/Library/Developer/CoreSimulator/Devices/<Simulator UUID>/data/Containers/Data/Application/<Application UUID>/Documents/.
You could also, in your app's code somewhere, get the path of your app's Document's folder and print it to the console with NSLog. Then copy the path, switch to Finder and press Shift-Cmd-G. Paste the path into the Finder field and it will take to your Documents folder.

iCloud documents no longer in Mobile Documents folder?

I have an app that creates a file in the iCloud documents folder. It is working fine, in that the devices that the app is installed on can access and update the file successfully. In my code, a NSLog of the file path gives me:
///private/var/mobile/Library/Mobile%20Documents/iCloud~my-company-name~appname/Documents/document_name
As part of my testing, I need to be able to view this file from my mac. Previously, I was able to do this by looking in my library/Mobile documents folder at the respective app folder.
However, I can't see the app folder in there. Any ideas why this would now not be showing up or pointers to configuration settings I could check? (I'm on Xcode 6.1, mac is Yosemite and all devices are iOS8).
Finder now treats this as a 'special' folder, so actively prevents you browsing the structure. You can do this if you use Terminal instead.

can't find /var/mobile/applications directory for ios documents

I'm trying to run a release build on my iPad device connected to xcode for debugging. I'm not certain but guessing that when I do this the documents directory ends up somewhere on my local machine and not on the iPad (as when I run it without xcode). I get an output like this for the directory:
/var/mobile/Applications/FF0F1AB2-AD1F-4E42-8815-9E399EEF5027/Documents/
but I can't access that directory. Going to /var does not reveal any 'mobile' subdir. This seems different from when I run the iPad unconnected and end up with files viewable on the device through Organizer within xcode (making me conclude that it saves them locally in this mysterious dir when run connected to xcode).
Can someone help me access this mystery directory?
For "xcode 6":
Connect your device with your MAC (You can sure about that having a look into the simulator list, it should be there)
Now open xcode and go to "Windows" from upper menu
Go to "Devices" from drop down list
Now this window will pop up. Your app should be there in the "Installed Apps" section. Like the picture below. Double click on it.
Here you can see the "Documents" folder and the photos you saved earlier. You can't delete photo from this list. Because this is the iPhone device section, you accessing through the xcode.
If you want to delete the photos, you have to delete the whole project. To do that, scroll down below the screen and there should be a minus "-" button. After selecting your project just click on it. It will delete your photos as well as whole project.
Window -> Organizer -> select your's Iphone -> Applications -> select your iphone project name and you can see blow in the "Data files in Sandbox"
(Original poster here)
A few things I got wrong:
Running it on the device whether with the debugger or not always puts the file on the device, not on the local drive. This /var/mobile directory must be essentially some location on the device itself that can't normally be accessed browsing from the mac.
I needed to refresh the view in Organizer to see the files being placed there. I did so by detaching the iPad and then reattaching it (the usb), and the files would refresh.
Instead of removing the question, I'll leave this here for others and myself for the tip in viewing/refreshing files in organizer and the clarification of where the files are being placed when run on the device.
The directory you are looking for is located at
/Users/User Name/Library/Application Support/iPhone
Simulator/Simulator iOS Version/Applications/Application
ID/Documents/
Thank you they are both right in different contexts
If you are running it on device then what #ethemsulan is correct.
And then on the simulator the folder is found on your mac and the file path is as #Vin and #Osiris has given
Add this to your info.plist source code and you will find the recorded files inside files of iPhone device.
<key>UIFileSharingEnabled</key>
<true/>
<key>LSSupportsOpeningDocumentsInPlace</key>
<true/>
To see the file saved in iPhone -
Go to plist file of your project.
Set Supports Document Browser to "YES".
You will see the saved file in FILES in your device.
I am using iOS 13 and Xcode 11.6, it worked for me.

Resources