目录

Unity-iOS-混合集成指南CocoaPods-与手动添加.framework

Unity iOS 混合集成指南:CocoaPods 与手动添加.framework


本文以 AppLovin Custom Adapter 方式集成 TradPlus SDK 为例,详细介绍从 CocoaPods 配置到框架集成的完整过程,并提供常见编译错误的解决方案。

一、CocoaPods 依赖配置

1. Podfile文件

首先确保您的 Podfile 文件包含正确的依赖声明:

source 'https://cdn.cocoapods.org/'

platform :ios, '12.0'

target 'UnityFramework' do
  pod 'AppLovinSDK', '13.4.0'
  pod 'TradPlusAdSDK', '14.2.0'
  pod 'TradPlusAdSDK/TPCrossAdapter', '14.2.0'
end
target 'Unity-iPhone' do
end
use_frameworks! :linkage => :static

2. 安装依赖

在终端中执行以下命令:

cd /Users/test/TradplusUnity/UnityiOS0905
pod install
test@MacBook-Pro-2 Unity090417 % cd /Users/test/TradplusUnity/UnityiOS0905
test@MacBook-Pro-2 UnityiOS0905 % pod install
Analyzing dependencies
Downloading dependencies
Installing AppLovinSDK (13.4.0)
Installing TPExchange (13.8.10)
Installing TradPlusAdSDK (14.2.0)
Generating Pods project
Integrating client project

[!] Please close any current Xcode sessions and use `Unity-iPhone.xcworkspace` for this project from now on.
Pod installation complete! There are 3 dependencies from the Podfile and 3 total pods installed.
test@MacBook-Pro-2 UnityiOS0905 % 

二、手动集成 .framework

将   集成到 Xcode 项目中

1. 将框架添加到项目中

方法一:通过拖拽添加

  • 打开您的 Xcode 项目 (Unity-iPhone.xcodeproj)
  • 在 Finder 中找到下载的 ALMCMediationAdapter.framework 文件
  • 将其拖拽到 Xcode 的 Frameworks 文件夹中
  • 在弹出的对话框中,确保选择所有目标 (Unity-iPhone 和 UnityFramework),这里咱们选UnityFramework。

https://i-blog.csdnimg.cn/direct/026f0b5f851d40f38e773530863aaaa5.png

方法二:通过菜单添加

https://i-blog.csdnimg.cn/direct/aebba190b826407c91d1a19dfbbaf5c8.png

  1. 右键点击 Frameworks 文件夹
  2. 选择 “Add Files to ‘Unity-iPhone’…”
  3. 找到并选择 ALMCMediationAdapter.framework
  4. 在弹出的对话框中,确保选择所有目标 (Unity-iPhone 和 UnityFramework),这里咱们选UnityFramework。

https://i-blog.csdnimg.cn/direct/1bc167ce055b4c2d8f1cd5c2b6b390d2.png

2. 配置链接器标志

在 Build Settings 中:

  • 搜索 Other Linker Flags
  • 添加:-ObjC(如果尚未存在)

https://i-blog.csdnimg.cn/direct/2df8de9171e742d683504e2e4b80ff77.png

3. 验证集成

https://i-blog.csdnimg.cn/direct/e98326014969499a835c1d42eb9702d4.png

  1. 清理项目:Product > Clean Build Folder

  2. 编译项目检查是否有错误

  3. 确保框架出现在 Link Binary With Libraries 中:

    • 选择目标
    • 转到 Build Phases
    • 在 Link Binary With Libraries 中确认框架存在(或出现多个则移除)

https://i-blog.csdnimg.cn/direct/9227db1b0f504b79b46fc55de6df79b9.png

4. 添加文件 Action 介绍

在 Xcode 16.4 中,当您添加文件时会出现三个选项:

  • Copy files to destination /Copy items if needed (推荐)

    • 相当于原来的 “Copy items if needed”
    • 会将文件复制到项目目录中
  • Move files to destination

    • 将文件移动到项目目录中(原位置文件会被移动)
  • Reference files in place

    • 只创建引用,文件保持在原位置

建议选择 “Copy files to destination” 以确保框架文件被复制到项目目录中。

三、编译报错问题排查

3.1 配置框架搜索路径

只要看到 framework not foundld:clang: error: linker  这类报错,几乎都与「链接阶段没找到库」有关。

在 Build Settings 中配置框架搜索路径:

  1. 选择 Unity-iPhone 目标
  2. 转到 Build Settings 标签
  3. 搜索 Framework Search Paths
  4. 添加路径:$(PROJECT_DIR)/Frameworks
  5. 确保设置为 recursive

3.2 检查 Target Membership

Undefined symbol: _OBJC_CLASS_$_ALMediationAdapter
Undefined symbol: _OBJC_CLASS_$_ALSdk
​​​​​​​Undefined symbol: _OBJC_CLASS_$_ALUtils

这些错误表明 ALMCMediationAdapter.framework 依赖的其他框架没有被正确链接。

检查 Target Membership,在添加ALMCMediationAdapter.framework文件时,是否选中了Unity-iPhone,如下图所示:

https://i-blog.csdnimg.cn/direct/151a15009d52474296e6c99ffcb66930.png

Podfile 配置中,AppLovinSDK和TradPlusAdSDK 等 只添加到了 UnityFramework target,但 ALMCMediationAdapter.framework 需要在 Unity-iPhone target 中链接,如上图

解决方案
方案 1:将 AppLovinSDK和TradPlusAdSDK 等 也添加到 Unity-iPhone target
source 'https://cdn.cocoapods.org/'
platform :ios, '12.0'

target 'UnityFramework' do
  pod 'AppLovinSDK', '13.4.0'
  pod 'TradPlusAdSDK', '14.2.0'
  pod 'TradPlusAdSDK/TPCrossAdapter', '14.2.0'
end

target 'Unity-iPhone' do
  pod 'AppLovinSDK', '13.4.0'  # 添加这一行
end

use_frameworks! :linkage => :static
方案 2:统一管理依赖
source 'https://cdn.cocoapods.org/'
platform :ios, '12.0'

# 在全局安装,所有target都能访问
pod 'AppLovinSDK', '13.4.0'
pod 'TradPlusAdSDK', '14.2.0'
pod 'TradPlusAdSDK/TPCrossAdapter', '14.2.0'

target 'UnityFramework' do
  # 这里可以添加UnityFramework特有的pod
end

target 'Unity-iPhone' do
  # 这里可以添加Unity-iPhone特有的pod
end

use_frameworks! :linkage => :static
方案 3 :移除ALMCMediationAdapter.framework 在 Unity-iPhone target 中链接

https://i-blog.csdnimg.cn/direct/934deda6fabd489886ee65842a843c68.png

3.3 查看.framework是否为静态库

  • .framework ≠ 动态库,也可以是静态库
  • 你这个 .framework 是一个 静态库打包成的 framework 结构,这在 iOS 第三方 SDK 中很常见

查看 .framework 是静态还是动态:

file ALMCMediationAdapter.framework/ALMCMediationAdapter

test@MacBook-Pro-2 Unity090417 % file ALMCMediationAdapter.framework/ALMCMediationAdapter 
ALMCMediationAdapter.framework/ALMCMediationAdapter: 
Mach-O universal binary with 2 architectures: 
[x86_64:current ar archive random library] 
[arm64:current ar archive random library] 
ALMCMediationAdapter.framework/ALMCMediationAdapter (for architecture x86_64): 
current ar archive random library 
ALMCMediationAdapter.framework/ALMCMediationAdapter (for architecture arm64): 
current ar archive random library

从上面的 file 命令输出可以看出:ALMCMediationAdapter.framework 是一个 静态库(不是动态库)。

相关推荐