SwiftUI: PreviewProvider crashes when used in apps targeting below iOS 13

Number:rdar://6140404 Date Originated:Jun 9, 2019
Status:Open Resolved:
Product:Swift Product Version:
Classification:Incorrect/Unexpected Behavior Reproducible:
Please describe the issue:
In an app targeting versions < iOS 13, implementing `UIViewControllerRepresentable` and `PreviewProvider` always results in a crash in the Canvas preview.
Please list the steps you took to reproduce the issue:
Implement `UIViewControllerRepresentable` and `PreviewProvider` from SwiftUI in an extension, using @available(iOS 13, *) for the conforming extension. Ensure the app targets iOS 11 (or anything below 13). Attempt to view the preview canvas.
What did you expect to happen?
Preview works even though app target targets iOS 11.
What actually happened?
Preview crashes with “'MainScreenViewControllerPreviews' is only available in iOS 13 or newer”. Swift generated extension doesn’t include @available(iOS 13, *), which causes a crash. Modifying the app’s deployment targets all to iOS 13 avoids the crash, but I assume it’s unintended behavior to have to keep changing my deployment target just to try out a non-deployment-related preview feature.


I would also love this

By Ethan.J.Diamond at June 18, 2019, 12:49 a.m. (reply...)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!