Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not render SwiftUI preview from Cocoapods

I have a SwiftUI struct in a common library that I ship to myself.

public struct NTextField: View {
    public var body: some View {
        Text("Hello, World!")
    }

    public init() {

    }
}

struct NTextField_Previews: PreviewProvider {
    static var previews: some View {
        NTextField()
    }
}

I click on the file and the WYSIWYG preview fails to load

enter image description here

When clicking 'Diagnostics' I see something like this:

Error Domain=com.apple.dt.UITestingAgent Code=-1 "failed to load library at path "/Users/<my_app_name>/Library/Developer/Xcode/DerivedData/<my_app_name>/Build/Intermediates.noindex/Previews/<my_app_name>/Products/Debug-iphonesimulator/<my_custom_pod>/<my_custom_pod>.framework/<my_custom_pod>": Library not loaded: 

Any ideas? I deleted derived data, cleaned and built.

Also found this on the cocoapods git hub issue tracker.

like image 552
Jon Vogel Avatar asked Nov 28 '19 00:11

Jon Vogel


People also ask

How do I create a preview in SwiftUI?

You can also create new preview structures in an existing SwiftUI view file by choosing Editor > Create Preview. For the complete list of preview customizations, see Previews in Xcode.

How do I open preview in SwiftUI?

There are two shortcuts that you should remember. Both of them will make your life easier during the development cycle of your SwiftUI views. Cmd + Option + Enter shows or hides previews. Cmd + Option + P runs the previews.

How do I show SwiftUI?

Display the SwiftUI Preview To show the preview, select a file that uses SwiftUI in the project navigator, and choose Editor > Canvas. Then click the Resume button in the upper-right corner of the canvas to start the preview. Xcode builds and runs the code, displaying the results directly in the canvas.


Video Answer


1 Answers

You could try adding this in your Podfile (as suggested by JamesHurst, cltnschlosser and andersio)

class Pod::Target::BuildSettings::AggregateTargetSettings
    alias_method :ld_runpath_search_paths_original, :ld_runpath_search_paths

    def ld_runpath_search_paths
        return ld_runpath_search_paths_original unless configuration_name == "Debug"
        return ld_runpath_search_paths_original + framework_search_paths
    end
end

class Pod::Target::BuildSettings::PodTargetSettings
    alias_method :ld_runpath_search_paths_original, :ld_runpath_search_paths

    def ld_runpath_search_paths
        return (ld_runpath_search_paths_original || []) + framework_search_paths
    end
end
like image 118
Tieme Avatar answered Oct 10 '22 07:10

Tieme