编辑快捷键
⌘ + L
跳转代码具体行数
ctrl + shift + 单击
多选,选中某一行开始编辑
option + 上下拖动
多行编辑
⌘ + ctrl + e
单个代码文件内,修改同一个单词
⌘ + shift + o
全局快速打开某个代码文件
⌘ + option + f
替换
⌘ + f
查找
⌘ + shift + L
代码区是代码块 IB区是控件
粗略统计代码行数脚本
find . "(" -name "*.h" -or -name "*.mm" -or -name "*.m" -or -name "*.swift" ")" -print | xargs wc -l
主题和样式
主题文件的存放路径 ~/Library/Developer/Xcode/UserData/FontAndColorThemes/
主题集合可参考 https://github.com/hdoria/xcode-themes
代码块: ~/Library/Developer/Xcode/UserData/CodeSnippets
参考 https://github.com/tripleCC/TPCXcodeCodeSnippetsji 脚本同步代码块
经常遇到手机系统版本高于Xcode SDK版本,导致无法调试,需要下载调试包兼容.
Xcode真机调试包的路径:
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport
可在 github 搜关键词 DeviceSupport
json与plist文件格式互转(自动操作)
fileSubffix=${1##*.} #后缀名
if [[ "${fileSubffix}"x == "json"x ]] ;
then
plutil -convert xml1 "${1}" -o "${1%.*}".plist
fi
if [[ "${fileSubffix}"x == "plist"x ]] ;
then
plutil -convert json "${1}" -o "${1%.*}".json
fi
上传IPA到蒲公英(自动操作)
if [[ -n "${1}" ]]; then
ipaName=${1##*/}
cd $(dirname $1)
api_key='replace apikey'
result=$(curl -F 'file=@'$ipaName -F '_api_key='$api_key https://www.pgyer.com/apiv2/app/upload)
ShortcutUrl_01=${result#*buildShortcutUrl\":\"}
ShortcutUrl=${ShortcutUrl_01%%\"*}
ipa_url=https://www.pgyer.com/"$ShortcutUrl"
echo "$ipa_url" | pbcopy
osascript -e "display notification \"测试包ipa上传成功🚀🚀🚀: "${ipa_url}" \" with title \"通知\" subtitle \"链接🔗已拷贝至剪贴板\" sound name \"Funk\""
else
echo "调用 sh pgyer_upload_ipa.sh [ipa path] 需要传入ipa完整路径参数"
fi
cocoapods 执行pod install(自动操作)
只需找到podfile文件右键菜单执行该workflow即可
PodfilePath="${1%/*}"
osascript <<EOF
tell application "iTerm"
tell current window
create tab with default profile
tell current session
write text "cd ${PodfilePath}"
write text "clear;"
write text "pod install"
end tell
end tell
end tell
EOF
Xcode打白包即.app => .ipa (自动操作)
AppName_all=${1##*/}
AppName=${AppName_all%.*}
cd ~/Desktop
result_dir="white_pack_ipa"
mkdir $result_dir
cd $result_dir
mkdir -p Payload
cp -Rf $1 ./Payload/
zip -qryX ./$AppName.ipa ./Payload
rm -rf Payload
静态库相关(自动操作)
查看静态库信息
.a文件直接读取,framework文件则需要读取编译的mach-O产物
lipo_info=`lipo -info $1`
osascript -e "display dialog \"${lipo_info}\" with title \"查看静态库信息\" buttons {\"OK\"} default button 1 with icon file \"Macintosh HD:Applications:Xcode.app:Contents:Resources:Xcode.icns\""
合并静态库mach-O产物
lib_path=~/Desktop/libmix
if [[ "${1##*.}" == "a" ]]; then
lib_path=~/Desktop/libmix.${1##*.}
fi
lipo -create ${*} -output ${lib_path}
osascript -e "display dialog \"静态库合并成功!\n路径为:${lib_path}\" with title \"合并静态库\" buttons {\"OK\"} default button 1 with icon file \"Macintosh HD:Applications:Xcode.app:Contents:Resources:Xcode.icns\""
移除指定架构
一次只能移除一种架构
select_arch=$(osascript <<EOF
choose from list {"i386", "x86_64", "armv7", "arm64", "arm64e"} with title "移除指定架构的静态库" with prompt "选择一架构" OK button name "提取" cancel button name "取消" default items {"arm64"}
EOF
)
if [[ "${select_arch}" != "false" ]]; then
lib_path=~/Desktop/libno${select_arch}
else
exit
fi
if [[ "${1##*.}" == "a" ]]; then
lib_file_path=$lib_path${1##*.}
else
lib_file_path=${lib_path}
fi
lipo ${1} -remove ${select_arch} -output ${lib_file_path}
osascript -e "display dialog \"移除${select_arch}架构成功\" with title \"移除静态库指定架构\" buttons {\"OK\"} default button 1 with icon file \"Macintosh HD:Applications:Xcode.app:Contents:Resources:Xcode.icns\""
提取指定的一种架构
select_arch=$(osascript <<EOF
choose from list {"i386", "x86_64", "armv7", "arm64", "arm64e"} with title "提取指定架构的静态库" with prompt "选择一架构" OK button name "提取" cancel button name "取消" default items {"arm64"}
EOF
)
if [[ "${select_arch}" != "false" ]]; then
lib_path=~/Desktop/lib$select_arch
else
exit
fi
if [[ "${1##*.}" == "a" ]]; then
lib_file_path=$lib_path${1##*.}
else
lib_file_path=$lib_path
fi
lipo ${1} -thin ${select_arch} -output ${lib_file_path}
osascript -e "display dialog \"${select_arch}静态库提取成功!\n路径为:${lib_path}\" with title \"提取指定架构的静态库\" buttons {\"OK\"} default button 1 with icon file \"Macintosh HD:Applications:Xcode.app:Contents:Resources:Xcode.icns\""
编译SDK的脚本
Xcode需要创建 Aggregate 的Target,并在Build Phases
新建一个RunScript
里面执行脚本
编译.framework
<<EOF
#注释说明: 保存本文件名为xcode_build_framework.sh,放到工程根目录即可
# Aggregate里新建RunScript 调用如下:
#一键产出通用包 传参指定架构打包
sh xcode_build_framework.sh "armv7,armv7s,arm64,arm64e,x86_64,i386"
#sh xcode_build_framework.sh # 不传参则弹出GUI选择菜单
EOF
set -e
#SDK的名字
SDK_NAME=${PROJECT_NAME}
#SDK产出目录
INSTALL_DIR=${SRCROOT}/Products/${SDK_NAME}.framework
#清空编译缓存
rm -rf ${BUILD_DIR}
if [ "x$1" = "x" ] ; #无入参则弹窗去选择
then
#弹出选择架构菜单
select_arch=$(osascript <<EOF
choose from list {"i386","x86_64","armv7","armv7s","arm64","arm64e"} with title "指定架构编译SDK" with prompt "选择架构" OK button name "编译" cancel button name "取消" default items {"armv7","arm64"} with multiple selections allowed
EOF
)
else
#输入指定架构 "arm64,armv7,..." 用逗号隔开即可
select_arch=${1}
fi
# 默认已选择armv7和arm64 点取消才会走这里
if [[ $select_arch = false ]] ;
then
echo "未选择架构,已取消本次编译."
exit 0;
fi
simulator_archs=()
if [[ $select_arch =~ "i386" ]] ;
then
simulator_archs+=("i386")
fi
if [[ $select_arch =~ "x86_64" ]] ;
then
simulator_archs+=("x86_64")
fi
echo "模拟器架构为: "${simulator_archs[*]}
#模拟器架构个数,如果个数为0,则不需要模拟器参与编译了
simulator_archs_length=${#simulator_archs[*]}
if [[ $simulator_archs_length == 0 ]] ;
then
echo "模拟器架构数为0,模拟器无需参与编译"
else
# 剔除模拟器架构
select_arch=${select_arch//i386/ }
select_arch=${select_arch//x86_64/ }
fi
# 获取真机的架构
iphoneos_archs=(${select_arch//,/ })
echo "真机架构为: "${iphoneos_archs[*]}
if [[ $simulator_archs_length > 0 ]] ;
then
# 执行模拟器编译指令
xcodebuild -configuration ${CONFIGURATION} -target "${SDK_NAME}" -sdk iphonesimulator build ARCHS="${simulator_archs[*]}"
fi
#执行真机编译指令
xcodebuild -configuration ${CONFIGURATION} -target "${SDK_NAME}" -sdk iphoneos build ARCHS="${iphoneos_archs[*]}"
#真机目录
DEVICE_DIR="build"/${CONFIGURATION}-iphoneos/${SDK_NAME}.framework
#模拟器目录
SIMULATOR_DIR="build"/${CONFIGURATION}-iphonesimulator/${SDK_NAME}.framework
#清除旧的输出目录
if [ -d "${INSTALL_DIR}" ]
then
rm -rf "${INSTALL_DIR}"
fi
#创建新的输出目录
mkdir -p "${INSTALL_DIR}"
#拷贝编译产物到输出目录
cp -R "${DEVICE_DIR}/" "${INSTALL_DIR}/"
# 有模拟器编译才需要合并
if [[ $simulator_archs_length > 0 ]] ;
then
#合并真机和模拟器的编译产物
lipo -create "${DEVICE_DIR}/${SDK_NAME}" "${SIMULATOR_DIR}/${SDK_NAME}" -output "${INSTALL_DIR}/${SDK_NAME}"
fi
#打开输出目录查看结果
open "${SRCROOT}/Products/"
rm -rf "${SRCROOT}/build"
#查看此次编译的架构支持
lipo_info=`lipo -info ${INSTALL_DIR}/${SDK_NAME}`
icon_path=`pwd`/Xcode.icns
icon_file=$(osascript -e "set thePath to POSIX file \"${icon_path}\" as string")
echo $icon_file
osascript -e "display dialog \"SDK包含架构: ${lipo_info##*are:}\" with title \"当前SDK架构信息\" buttons {\"OK\"} default button 1 with icon file \"${icon_file}\""
echo "脚本跑🏃完了"
<<EOF
#注释说明: 保存本文件名为xcode_build_lib.sh,放到工程根目录即可
# Aggregate里新建RunScript 调用如下:
#一键产出通用包 传参指定架构打包
sh xcode_build_lib.sh "armv7,armv7s,arm64,arm64e,x86_64,i386"
#sh xcode_build_lib.sh # 不传参则弹出GUI选择菜单
EOF
set -e
#SDK的名字
SDK_NAME=${PROJECT_NAME}
#SDK产出目录
INSTALL_DIR=${SRCROOT}/Products/${SDK_NAME}
#清空编译缓存
rm -rf ${BUILD_DIR}
if [ "x$1" = "x" ] ; #无入参则弹窗去选择
then
#弹出选择架构菜单
select_arch=$(osascript <<EOF
choose from list {"i386","x86_64","armv7","armv7s","arm64","arm64e"} with title "指定架构编译SDK" with prompt "选择架构" OK button name "编译" cancel button name "取消" default items {"armv7","arm64"} with multiple selections allowed
EOF
)
else
#输入指定架构 "arm64,armv7,..." 用逗号隔开即可
select_arch=${1}
fi
# 默认已选择armv7和arm64 点取消才会走这里
if [[ $select_arch = false ]] ;
then
echo "未选择架构,已取消本次编译."
exit 0;
fi
simulator_archs=()
if [[ $select_arch =~ "i386" ]] ;
then
simulator_archs+=("i386")
fi
if [[ $select_arch =~ "x86_64" ]] ;
then
simulator_archs+=("x86_64")
fi
echo "模拟器架构为: "${simulator_archs[*]}
#模拟器架构个数,如果个数为0,则不需要模拟器参与编译了
simulator_archs_length=${#simulator_archs[*]}
if [[ $simulator_archs_length == 0 ]] ;
then
echo "模拟器架构数为0,模拟器无需参与编译"
else
# 剔除模拟器架构
select_arch=${select_arch//i386/ }
select_arch=${select_arch//x86_64/ }
fi
# 获取真机的架构
iphoneos_archs=(${select_arch//,/ })
echo "真机架构为: "${iphoneos_archs[*]}
if [[ $simulator_archs_length > 0 ]] ;
then
# 执行模拟器编译指令
xcodebuild -configuration ${CONFIGURATION} -target "${SDK_NAME}" -sdk iphonesimulator build ARCHS="${simulator_archs[*]}"
fi
#执行真机编译指令
xcodebuild -configuration ${CONFIGURATION} -target "${SDK_NAME}" -sdk iphoneos build ARCHS="${iphoneos_archs[*]}"
#真机目录
DEVICE_DIR="build"/${CONFIGURATION}-iphoneos
#模拟器目录
SIMULATOR_DIR="build"/${CONFIGURATION}-iphonesimulator
#清除旧的安装目录
if [ -d "${INSTALL_DIR}" ]
then
rm -rf "${INSTALL_DIR}"
fi
#创建新的安装目录
mkdir -p "${INSTALL_DIR}"
#拷贝编译产物到安装目录
cp -R "${DEVICE_DIR}/" "${INSTALL_DIR}/"
rm -rf "${INSTALL_DIR}/usr"
# 有模拟器编译才需要合并
if [[ $simulator_archs_length > 0 ]] ;
then
#合并真机和模拟器的编译产物
lipo -create "${DEVICE_DIR}/lib${SDK_NAME}.a" "${SIMULATOR_DIR}/lib${SDK_NAME}.a" -output "${INSTALL_DIR}/lib${SDK_NAME}.a"
fi
#打开安装目录查看结果
open "${SRCROOT}/Products/"
#删除编译冗余数据
rm -rf "${SRCROOT}/build"
#查看此次编译的架构支持
lipo_info=`lipo -info ${INSTALL_DIR}/lib${SDK_NAME}.a`
icon_path=`pwd`/Xcode.icns
icon_file=$(osascript -e "set thePath to POSIX file \"${icon_path}\" as string")
echo $icon_file
osascript -e "display dialog \"SDK包含架构: ${lipo_info##*are:}\" with title \"当前SDK架构信息\" buttons {\"OK\"} default button 1 with icon file \"${icon_file}\""
echo "脚本跑🏃完了"