-1

This works for the Simulator, but not for Previews: Adding images or videos to iPhone Simulator

i.e. You can't drop stuff onto the Canvas.

Is there UI for this? I want to, for example use PHPickers without having to build and run. That works, but I only know how to access the default set of photos that way.

1 Answers1

2

There doesn't seem to be a good way to do this yet for SwiftUI previews. It does seem to be related to privacy approval. There is a bit of a hack that seems to work.

You'll need a program to edit SQLite databases. I use the free DB Browser for SQLite.

Step 1. Run your app in the iOS simulator and accept all photo library permissions.

Step 2. In your SQL editor, open the simulator's privacy database:

~/Library/Developer/CoreSimulator/Devices/<SIMULATOR>/data/Library/TCC/TCC.db

Step 3. Copy the necessary kTCCServicePhotos row from the access table

Step 4. Insert the row into the SwiftUI previews' privacy database:

~/Library/Developer/Xcode/UserData/Previews/Simulator Devices/<SIMULATOR>/data/Library/TCC/TCC.db

You now have your SwiftUI Previews' photo privacy approved. You should see the default simulator photos appear in the preview now.

If you want to add your own photos into the mix, you can drag them onto the iOS Simulator. Then copy the contents of the .../<SIMULATOR>/data/Media/DCIM/100Apple and .../<SIMULATOR>/data/Media/PhotoData folders across to the corresponding SwiftUI preview simulator.

I'm developing a photo app with a custom SwiftUI photo library manager. It's a real pain not being able to see photos in the preview. I found your question and my heart sank when I saw it was unanswered. So I spent the afternoon nutting it out

JeremySom
  • 175
  • 8
  • This is great @JeremySom! It works with `kTCCServiceAddressBook` too. I just found the row for my app in the Previews TCC.db and changed the following values to match the CoreSimulator values: `auth_value=2, auth_reason=2, auth_version=1` in my case – Robert Jul 09 '23 at 16:03