背景描述
毕业初入iOS,每次涉及到iOS环境配置都胆战心惊,cocopods 安装了,xcode也安装了,怎么项目就是跑不起来呢?
当然,刚毕业的时候都是其它大佬帮我解决的。结果后面入职了第二家公司,安装环境后一直跑不起来项目。折腾了整整两天才跑起来,头皮发麻。
后面发现其实不只是环境安装,还是得有一些其它关于Xcode项目管理的知识,才能让你入职后顺利跑起公司的项目。
在此记录下自己的血泪史,以照亮后来者。
环境安装及配置
首先找同事确定他们的版本,再安装。
0.配置SSH以访问公司的git仓库
1.Xcode版本
2.Flutter版本 flutter -v
3.Java SDK版本
4.CocoaPods版本 pod --version
5.ruby 版本 ruby -v
6.gem版本 gem -v
7.Homebrew版本 brew --version
配置SSH
新入职公司后,几乎都需要配置 SSH 访问 Git 仓库,尤其是使用 GitHub、GitLab、Bitbucket 或公司自建 Git 服务(如 Gitea、Gitolite) 的团队。
生成密钥 → 启动 agent → 添加密钥 → 复制公钥 → 添加到 Git 平台 → 测试连接 → 验证克隆
第 1 步:检查是否已有 SSH 密钥
ls -al ~/.ssh
查看是否有以下文件:
id_rsa和id_rsa.pub(旧版 RSA)id_ed25519和id_ed25519.pub(推荐,更安全)
第 2 步:(没有的话)生成新的 SSH 密钥
推荐使用更安全的 Ed25519 算法:
ssh-keygen -t ed25519 -C "your.email@company.com"
-t ed25519:加密算法(现代推荐)-C "...":注释,通常是你的公司邮箱,用于标识
按提示操作:
-
保存路径:直接回车(默认
~/.ssh/id_ed25519) -
输入密码(passphrase):
- 可以设置(更安全),也可以直接回车(免密,方便开发)
第 3 步:启动 SSH Agent 并添加密钥
# 启动 SSH agent
eval "$(ssh-agent -s)"
# 将私钥添加到 agent(如果设置了 passphrase,会提示输入)
ssh-add --apple-use-keychain ~/.ssh/id_ed25519
第 4 步:复制公钥到剪贴板
# macOS
pbcopy < ~/.ssh/id_ed25519.pub
# 或手动打开查看
cat ~/.ssh/id_ed25519.pub
你会看到类似:
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIG... your.email@company.com
第 5 步:将公钥添加到公司 Git 平台
根据公司使用的平台,登录并添加 SSH Key:
| 平台 | 添加路径 |
|---|---|
| GitHub | Settings → SSH and GPG keys → New SSH key |
| GitLab | Preferences → SSH Keys |
| Bitbucket | Personal Settings → SSH keys |
| 公司自建 GitLab/Gitea | 登录后找 "SSH Keys" 或联系管理员 |
粘贴你复制的 id_ed25519.pub 内容,标题可写 yourname-macbook
第 6 步:测试 SSH 连接
# 测试 GitHub
ssh -T git@github.com
# 测试 GitLab
ssh -T git@gitlab.com
# 测试公司自建 Git(如 git.yourcompany.com)
ssh -T git@git.yourcompany.com
成功返回示例:
- GitHub:
Hi username! You've successfully authenticated... - GitLab:
Welcome to GitLab, @username!
第7步:验证 SSH 是否生效?
克隆一个私有项目(使用 SSH 地址)
git clone git@github.com:your-company/ios-app.git
如果无需输入密码就能克隆,说明 SSH 配置成功。
常见问题与解决方案
| 问题 | 原因 | 解决方法 |
|---|---|---|
Permission denied (publickey) | 公钥未添加或未加载 | ssh-add -l 查看是否已加载,ssh-add ~/.ssh/id_ed25519 手动添加 |
| 多个 SSH 密钥冲突 | 默认只用 id_rsa | 使用 ~/.ssh/config 配置 Host 别名 |
| M1 Mac 钥匙串问题 | agent 无法保存 passphrase | 确保使用 --apple-use-keychain |
| 公司使用 HTTPS 但要求 SSH | 配置 Git 替换 URL | git config --global url."git@github.com:".insteadOf "https://github.com/" |
安装完环境项目还是跑不起来?
1.私有Pod源无法访问
现象:
pod install卡住或报错
错误日志:
[!] Error installing Biz.Base.JsonRouter
[!] /usr/bin/git clone https://github.com/your-company/Biz.Base.JsonRouter.git
Cloning into 'Biz.Base.JsonRouter'...
fatal: could not read Username for 'https://github.com': Device not configured
或:
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
原因:
- 私有仓库需要权限(SSH 密钥 或 Personal Access Token)
- 未配置 SSH,或用了 HTTPS 但没加 token
2.签名失败
现象:
-
Xcode 编译失败,弹窗:
"No signing certificate "iOS Development" found" "No profiles for 'com.yourcompany.app' were found"
界面特征:
- Xcode →
Runnertarget →Signing & Capabilities - 显示黄色警告或红色错误
Team为空或显示 “You don't have permission”
解决办法
如果没有证书的话, Xcode -> targets 找到自己的app -> Sigining & Capabilities 1.勾选Automatically message signing 2.Team -> add an Account 登录自己的AppleID 3.Bundle Identifier 添加个后缀,与公司的区别开来 4.删除Associated Domains 、 push notifications之类的付费能力。 然后再跑
有证书的话
让同事给你 .p12 证书和 .provisiong签名证书双击安装即可。
3.M1/M2 芯片架构问题
现象:
- 错误日志:
dlopen failed: cannot load '/.../libswiftCore.dylib' for target 'x86_64': No such file
或:
building for iOS Simulator, but linking in object file built for iOS
原因:
- 某些 Pod 是二进制预编译(
.framework),只支持arm64,但模拟器是x86_64
解决方案:
方法 1:用 arch -x86_64 安装 Pod
cd ios
arch -x86_64 pod install
方法 2:在 Podfile 中排除 arm64 模拟器
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['EXCLUDED_ARCHS[sdk=iphonesimulator*]'] = 'arm64'
end
end
end
4.Swift 版本不匹配
现象:
- 错误:
Module compiled with Swift 5.6 cannot be imported by Swift 5.7
或:
Could not find module 'Alamofire' for target 'x86_64-apple-ios-simulator'; found: arm64
✅ 解决方案:
在 Podfile 中统一 Swift 版本:
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['SWIFT_VERSION'] = '5.7'
end
end
end