You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用pod lib create创建私有库时,如何将bundle打包进xcframework?

为pod lib create生成的私有库打包Bundle进XCFramework的操作步骤

1. 准备并添加Bundle到项目

  • 在私有库的项目目录下,新建Resources文件夹,把需要的资源(图片、本地化文件、xib等)放进去。
  • 打开私有库的Xcode项目,右键点击主库Target(比如你的库叫MySDK),选New Target,在弹窗里挑iOS -> Resource -> Bundle,命名成MySDKResources(和库名对应好识别),语言随便选不影响。
  • 把Resources里的资源拖进新建的MySDKResources Target,勾选「Copy items if needed」,且Target Membership只勾选这个Bundle Target。

2. 让主库依赖并嵌入Bundle

  • 选中主库Target(MySDK),切到Build Phases标签:
    • 在Target Dependencies里点+,添加MySDKResources,保证编译主库前先把Bundle编译好。
    • 点左上角+号,选New Copy Files Phase,把这个阶段的Destination设为Frameworks,再把MySDKResources.bundle拖进文件列表,取消勾选「Copy only when installing」。

3. 配置Podspec文件(可选但推荐)

  • 打开私有库的.podspec文件,加一段资源配置,确保pod安装时资源能正确被引用:
    s.resource_bundles = {
      'MySDKResources' => ['MySDK/Resources/**/*']
    }
    
    路径要对应你实际的资源存放位置,MySDK是pod lib create生成的库根目录。

4. 打包XCFramework

  • 用xcodebuild命令打包时,指定主库Target,Bundle会自动被打包进XCFramework:
    # 打包iOS真机版本
    xcodebuild archive -scheme MySDK -destination "generic/platform=iOS" -archivePath ./build/MySDK-iOS.xcarchive -derivedDataPath ./build/ios SKIP_INSTALL=NO BUILD_LIBRARY_FOR_DISTRIBUTION=YES
    # 打包iOS模拟器版本
    xcodebuild archive -scheme MySDK -destination "generic/platform=iOS Simulator" -archivePath ./build/MySDK-iOSSimulator.xcarchive -derivedDataPath ./build/simulator SKIP_INSTALL=NO BUILD_LIBRARY_FOR_DISTRIBUTION=YES
    # 合并成XCFramework
    xcodebuild -create-xcframework -framework ./build/MySDK-iOS.xcarchive/Products/Library/Frameworks/MySDK.framework -framework ./build/MySDK-iOSSimulator.xcarchive/Products/Library/Frameworks/MySDK.framework -output ./build/MySDK.xcframework
    
  • 打包完成后,打开生成的MySDK.xcframework,就能在对应平台目录(比如ios-arm64)的MySDK.framework里看到MySDKResources.bundle。

验证Bundle是否可用

  • 把XCFramework集成到测试项目里,用代码访问资源试试,比如拿图片:
    // Swift示例
    guard let bundleURL = Bundle(for: MySDKClass.self).url(forResource: "MySDKResources", withExtension: "bundle"),
          let resourceBundle = Bundle(url: bundleURL) else {
        fatalError("加载资源Bundle失败")
    }
    let image = UIImage(named: "icon", in: resourceBundle, compatibleWith: nil)
    
    // Objective-C示例
    NSBundle *mainBundle = [NSBundle bundleForClass:[MySDKClass class]];
    NSURL *bundleURL = [mainBundle URLForResource:@"MySDKResources" withExtension:@"bundle"];
    NSBundle *resourceBundle = [NSBundle bundleWithURL:bundleURL];
    UIImage *image = [UIImage imageNamed:@"icon" inBundle:resourceBundle compatibleWithTraitCollection:nil];
    

内容的提问来源于stack exchange,提问作者AdamShi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 03:40:29