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。
方法二:通过菜单添加
- 右键点击
Frameworks
文件夹 - 选择 “Add Files to ‘Unity-iPhone’…”
- 找到并选择
ALMCMediationAdapter.framework
- 在弹出的对话框中,确保选择所有目标 (Unity-iPhone 和 UnityFramework),这里咱们选UnityFramework。
2. 配置链接器标志
在 Build Settings
中:
- 搜索
Other Linker Flags
- 添加:
-ObjC
(如果尚未存在)
3. 验证集成
清理项目:
Product > Clean Build Folder
编译项目检查是否有错误
确保框架出现在
Link Binary With Libraries
中:- 选择目标
- 转到
Build Phases
- 在
Link Binary With Libraries
中确认框架存在(或出现多个则移除)
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 found
、ld:
、clang: error: linker
这类报错,几乎都与「链接阶段没找到库」有关。
在 Build Settings
中配置框架搜索路径:
- 选择
Unity-iPhone
目标 - 转到
Build Settings
标签 - 搜索
Framework Search Paths
- 添加路径:
$(PROJECT_DIR)/Frameworks
- 确保设置为
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,如下图所示:
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 中链接
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
是一个 静态库(不是动态库)。