How can I use the iOS Simulator to simulate an out

2020-05-15 01:47发布

What is a practical way to test an "out of disk space" condition using the iOS Simulator?

标签: ios iphone
2条回答
▲ chillily
2楼-- · 2020-05-15 02:12

Create a small disk image:

hdiutil create -size 2m -fs HFS+ /tmp/2meg.dmg

Mount it to the relevant directory in your simulator app, e.g.:

hdiutil attach /tmp/2meg.dmg -mountpoint /Users/.../Library/Developer/CoreSimulator/Devices/.../data/Applications/.../Library/Caches

Run app and perform your tests. This can also be done while the app is running. In this case you probably don't want to mount it as Caches or Documents directory directly because this would hide current files in those folders. Mount to a sub-directory instead. If this is not easily possible without changing paths in your app, mount the image somewhere else and copy data over before mounting it on top of the nonempty directory.

To unmount:

hdiutil detach /Users/.../Library/Developer/CoreSimulator/Devi...

However, please note that the detach operation fails if the mount point is moved. This happens all the time because Apple renames simulator directories on iOS 8 every time an app is run. In this case use the mount command to find the device you have mounted, e.g. /dev/disk3s1 /Users/.../Library/..., then unmount the disk image using the device name instead of the mount point:

hdiutil detach disk3s1
查看更多
欢心
3楼-- · 2020-05-15 02:23

This isn't practical on the simulator (you'd have to fill up your Mac's disk.) The best way to achieve this test case is to use an actual device and fill with Music via iTunes and test on device.

查看更多
登录 后发表回答