简体   繁体   English

如何将文件添加到 iOS 模拟器?

[英]How can I add files to the iOS simulator?

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.我将 PDF 拖到模拟器上,这就是我得到的全部。 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.由于给定的解决方案都不适用于 iOS 13/Xcode 11 的模拟器(尝试添加文件,从 Finder 中拖出只会出错),因此我找到了一种解决方法。

This takes a bit of setting up at first, but you get free access to the actual folder right from the Finder.这首先需要进行一些设置,但您可以直接从 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 ".模拟器中名为“ On my phone ”的文件夹称为“ File Provider Storage ”。 The aim is to find it in the Finder.目的是在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.直到当前版本 (13.1),模拟器不能用于拾取文件。 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. 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. 3a。 Navigate to: username/Library/Developer/Core Simulator/Devices .导航到: username/Library/Developer/Core Simulator/Devices
3b. 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. 3a。 Run an App and print (from Xcode) the path to documentDirectory (using FileManager ).运行一个应用程序并打印(从 Xcode)到documentDirectory的路径(使用FileManager )。
3b. 3b。 Open it in Finder and navigate back to "Containers folder".在 Finder 中打开它并导航回“Containers 文件夹”。
3c. 3c。 Navigate to Shared/AppGroup .导航到Shared/AppGroup
3d. 3d。 You're looking for Shared/AppGroup/some_hashFolder/File Provider Storage/your_folder .您正在寻找Shared/AppGroup/some_hashFolder/File Provider Storage/your_folder
3e. 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.因为 xcode11/sdk13 在模拟器上拖动文件无论出于何种原因都不起作用。 you can send media files (jpg, png, mov, etc) via terminal with xcrun simctl addmedia <device> <path> .您可以使用xcrun simctl addmedia <device> <path>通过终端发送媒体文件(jpg、png、mov 等)。

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 .就我而言,我想发送根证书以允许连接到本地服务器,这不适用于addmedia the solution was to force the simulator to navigate to the full macOS local path to the .cer file with safari解决方案是强制模拟器使用 safari 导航到 .cer 文件的完整 macOS 本地路径

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.事实证明,启动 safari 并在地址栏中输入file:///Users/my.user/Desktop/my.cer也可以。 use this for pdfs and other types.将此用于pdf和其他类型。

original answer:原答案:
On XCode 9 simulator, if it's an image/video, simply drag it over the simulator .在 XCode 9 模拟器上,如果是图像/视频,只需将其拖到模拟器上即可。 It adds it to the Photos app.它将它添加到照片应用程序中。 Other types of files seem to randomly open the file browser and generally fail.其他类型的文件似乎会随机打开文件浏览器并且通常会失败。 :P :P

The easiest solution can be found here: https://apple.stackexchange.com/a/299565/140768最简单的解决方案可以在这里找到: 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.正如链接帖子中所建议的,只需创建一个空的“基于文档”的应用程序并运行一次。

在此处输入图像描述

您可以将文件上传到您在模拟器上登录的帐户的 iCloud Drive,并通过导航到 Files 应用程序或文件上传对话框中的Browse/iCloud Drive来访问它们。

Multiple steps but works多个步骤但有效

1) Create a new project Document Based app and run it on the same simulator. 1)创建一个新项目基于文档的应用程序并在同一个模拟器上运行它。

在此处输入图像描述

2) Now open safari in simulator and google for pdf. 2) 现在在模拟器中打开 safari 并在 google 中打开 pdf。 Select save to files option from action sheet and then select you newly created app folder.从操作表中选择保存到文件选项,然后选择您新创建的应用程序文件夹。

在此处输入图像描述

在此处输入图像描述

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).将文件从 Filer 拖放到模拟器上有 macOS Catalina 的问题(我目前在 10.15.5 上并看到它)。 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).将文件放在您的 $HOME 目录中,然后将其拖动(或使用共享按钮共享到模拟器)。 See thread: https://forums.developer.apple.com/thread/126307见线程: https ://forums.developer.apple.com/thread/126307

  1. Use safari on your simulator在模拟器上使用 safari

  2. Vist https://en.unesco.org/inclusivepolicylab/sites/default/files/dummy-pdf_2.pdf for a dummy pdf访问https://en.unesco.org/inclusivepolicylab/sites/default/files/dummy-pdf_2.pdf获取虚拟 pdf

  3. Click the middle icon as shown below点击中间的图标,如下图在此处输入图像描述

  4. Click on save to files and you are done点击保存到文件,你就完成了在此处输入图像描述

只需打开 Safari 并下载一些东西。

Just drag and drop on the simulator window.只需拖放到模拟器窗口上即可。 That's it.而已。

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.如果未与任何应用程序关联,则文件应用程序将打开,您可以选择保存“在我的 iPhone 上”或其他位置。

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. @Palli Kominak 给出了这个答案,但在其他答案之一下的评论中留下了它。

I found one easy way to do it with a little help from Python3 (I suppose all Macs should have it installed by default.Python3的帮助下,我找到了一种简单的方法(我想所有的 Mac 都应该默认安装它。

Here are the steps.以下是步骤。

  1. Open your terminal app打开您的终端应用程序
  2. Go to the directory which contains your file(s), eg转到包含您的文件的目录,例如

在此处输入图像描述

(I need to transfer a self-signed certificate to the simulator) (我需要将自签名证书传输到模拟器)

  1. Run python3 -m http.server to launch an HTTP server运行python3 -m http.server以启动 HTTP 服务器
  2. Open Safari in your similator, and open http://localhost:8000, you should see a list of file(s), eg在你的模拟器中打开 Safari,然后打开 http://localhost:8000,你应该会看到一个文件列表,例如

在此处输入图像描述

  1. Download the file(s) you need from Safari从 Safari 下载您需要的文件

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,最小化您当前的应用程序并搜索照片应用程序,

在此处输入图像描述

  1. Open photos app in simulator, it will just look like real iPhone在模拟器中打开照片应用程序,它看起来就像真正的 iPhone

在此处输入图像描述

  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, ]像这样的东西,] 在此处输入图像描述

Simple, now use it just like it acts on real device.很简单,现在就像在真实设备上一样使用它。

You can put files in your APP's local /documents folder.您可以将文件放在 APP 的本地 /documents 文件夹中。 To get the directory name in your APP do要在您的应用程序中获取目录名称,请执行

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

you will also need to enable file sharing by putting您还需要通过放置来启用文件共享

UIFileSharingEnabled UIFileSharingEnabled

in info.plist在 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:就我而言,我想在没有任何确认对话框的情况下使用 shell 上传代理证书,所以我想出了这个:

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.第一行只是获取已启动模拟器的 UDID,第二行是在已启动模拟器中查找所有File Provider Storage目录,然后将文件复制到它们。

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>当然如果要指定模拟器或者启动多个模拟器, grep Booted可以改成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.这行可以直接将文件复制到LocalStorage,与之前的解决方案相反,没有其他目录。

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这种方法利用xcrun simctl listapps命令返回 FileProvider.LocalStorage 的 AppGroup id

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.如果你想在“文件”应用程序中添加一些文件,首先你必须在你的 mac 中找到它的目录,在“文件”应用程序中创建一个名为“MagicTheGreate”的目录,然后转到命令行并在 ./Library 文件夹中搜索名称。

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.坏消息是,目录偶尔会更改,您必须再次找到它。

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:然后你会看到一个选项列表:

在此处输入图像描述

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.答案要简单得多,只需将zip文件在macOS中拖放到模拟器中,它会自动在文件应用程序中打开,您可以在那里解压并使用它。

You can browse to Library/Developer/CoreSimulator/Devices/Your App ID/data您可以浏览到 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.您可以将文件复制到您喜欢的任何位置,它将出现在您的模拟器中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM