有没有一种方法可以根据静态或动态用法有条件地定义Podspec属性? [英] Is there a way to conditionally define a Podspec property depending on static or dynamic usage?

查看:139
本文介绍了有没有一种方法可以根据静态或动态用法有条件地定义Podspec属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次在我的CocoaPods库中包含一个Swift源。为了使项目得以编译,我需要将生成的Swift标头导入到我的Objective-C源代码中。这有两种不同的形式,具体取决于项目是作为静态库还是动态框架来构建:

I'm including a Swift source in my CocoaPods library for the first time. For me to get the project to compile, I need to import the generated Swift header into my Objective-C source. This takes two different forms, depending on whether the project is being built as a static library or a dynamic framework:

#ifdef BUILT_AS_FRAMEWORK
    #import <UnzipKit/UnzipKit-Swift.h>
#else
    // Used when built as a static library
    #import "UnzipKit-Swift.h"
#endif

我在Xcode项目中定义 BUILT_AS_FRAMEWORK 来进行开发,但是当我将库动态框架,并且因为我尚未在Podspec中定义该标志,所以它尝试解析第二个导入,但找不到它。

I'm defining BUILT_AS_FRAMEWORK in my Xcode project for development time, but when I lint the library as a dynamic framework, and because I haven't defined that flag in the Podspec, it attempts to resolve the second import, and can't find it.

我可以定义 BUILT_AS_FRAMEWORK 预处理器宏的方式,但是仅当使用方Podfile不能将其构建为静态库时才可以?

Is there a way I can define the BUILT_AS_FRAMEWORK preprocessor macro, but only if the consuming Podfile doesn't build it as a static library?

为此问题,我在CocoaPods项目中创建了问题#9101

I created issue #9101 in the CocoaPods project for this question.

推荐答案

我能够结合使用Xcode环境变量( PACKAGE_TYPE )带有预编译的运行脚本构建阶段,以动态生成要导入的标头,从而使修正t生成了Swift标头导入。

I was able to use an Xcode environment variable (PACKAGE_TYPE) in conjunction with a pre-compilation Run Script build phase to dynamically produce a header to import, which in turn makes the correct generated Swift Header import.

#!/bin/sh

[[ "${PACKAGE_TYPE}" = "com.apple.package-type.wrapper.framework" ]] \
    && SWIFTIMPORT="<${PRODUCT_MODULE_NAME}/${PRODUCT_MODULE_NAME}-Swift.h>" \
    || SWIFTIMPORT="\"${PRODUCT_MODULE_NAME}-Swift.h\""

if [ -z "$PODS_TARGET_SRCROOT" ]; then
    PODS_TARGET_SRCROOT=${SOURCE_ROOT}
    echo "Building in Xcode instead of CocoaPods. Overriding PODS_TARGET_SRCROOT with SOURCE_ROOT"
fi

_Import_text="
#ifndef GeneratedSwiftImport_h
#define GeneratedSwiftImport_h

#import ${SWIFTIMPORT}

#endif /* GeneratedSwiftImport_h */
"
echo "$_Import_text" > ${PODS_TARGET_SRCROOT}/Source/GeneratedSwiftImport.h



Podspec更新



Podspec update

s.script_phases = { :name => "Generate UnzipKit Swift Header",
                    :script => "\"${PODS_TARGET_SRCROOT}\"/Scripts/generate-swift-import-header.sh",
                    :execution_position => :before_compile }



库源



I

Library source

I replaced my conditional import with this:

#import "GeneratedSwiftImport.h"

我还忽略了Git中的 GeneratedSwiftImport.h 文件。

I also ignored the GeneratedSwiftImport.h file in Git.

这篇关于有没有一种方法可以根据静态或动态用法有条件地定义Podspec属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆