Can not render SwiftUI preview from Cocoapods
Asked Answered
R

2

13

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.

Runoff answered 28/11, 2019 at 0:35 Comment(0)
F
14

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
Fimbria answered 27/5, 2020 at 8:53 Comment(2)
This does not compile ruby 2.6.0 cocoapods 1.9.3 ``` NoMethodError - undefined method `+' for nil:NilClass ```Shaunshauna
return (ld_runpath_search_paths_original || []) + framework_search_paths in class Pod::Target::BuildSettings::AggregateTargetSettings fix that :)Stubby
P
0

A workaround I implemented was creating a script in the build phases, specifically for the Lottie pod.

Adjust this script depending on your pods; this example works only for Lottie. Also, disable the "Use Script Sandboxing" option in the build settings to avoid the error operation not permitted.

Explanation: I realized that the error in the previews was due to the previews not being able to find the pods in the path where they are created. The script copies the pods to the path where the previews are looking for them.

#!/bin/bash
# Define the source and destination directories
BUILD_DIR="${BUILD_DIR:-$SRCROOT/../Build}"
CONFIGURATION_BUILD_DIR="${BUILD_DIR}/${CONFIGURATION}${EFFECTIVE_PLATFORM_NAME}"
SOURCE_DIR="${CONFIGURATION_BUILD_DIR}/lottie-ios"
DEST_DIR="${CONFIGURATION_BUILD_DIR}"


# Ensure the source directory exists
if [ ! -d "$SOURCE_DIR/Lottie.framework" ]; then
  echo "Source directory $SOURCE_DIR/Lottie.framework does not exist."
  exit 1
fi

# Copy Lottie.framework
echo "Copying Lottie.framework to $DEST_DIR"
cp -r "$SOURCE_DIR/Lottie.framework" "$DEST_DIR"

# Create a dummy output file to avoid the Xcode warning
echo "output_file" > /tmp/copy_lottie_framework_output.txt
Pulsatile answered 19/7 at 22:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.