iOS开发者时报ios测试iOS面试知识点

ruby库xcodeproj使用心得

2017-07-28  本文已影响300人  QA小蛋

| 导语 在iOS自动化测试工具的开发过程中,会涉及到修改项目工程的配置,通过xcodeproj可以实现脚本方式修改,不需要打开XCode手动修改配置了。

xcodeproj这个库功能很强大,XCode工程的大多数配置都可以通过通过xcodeproj完成;在这里主要给大家介绍下一些常用配置的修改方法。

使用方法:

1.安装xcodeproj

gem install xcodeproj

2.通过xcodeproj设置证书

require 'xcodeproj'

#打开项目工程A.xcodeproj
project_path = 'xxxPath/A.xcodeproj'
project = Xcodeproj::Project.open(project_path)

#修改某个target在debug模式下的证书配置
#此处遍历找到debug
project.targets[0].build_configurations.each do |config|
  if config.name == 'Debug'
    config.build_settings["PROVISIONING_PROFILE_SPECIFIER"] = "xxProfileName"
    config.build_settings["DEVELOPMENT_TEAM"] = "xxTeamName"
    config.build_settings["CODE_SIGN_IDENTITY"] = "xxIdentityName"
    config.build_settings["CODE_SIGN_IDENTITY[sdk=iphoneos*]"] = "iPhone Developer"
  end
end

project.save

此处可以通过此方法修改target下某个模式(debug, release等)的大多数配置,可通过config.build_settings查看所有的键值对,然后根据需求修改键值,如OTHER_FLAGS,PRODUCT_BUNDLE_IDENTIFIER,HEADER_SEARCH_PATHS,LIBRARY_SEARCH_PATHS等。

3.通过xcodeproj在工程的xxx group下引入xx.h和xx.m文件


require 'xcodeproj'

#打开项目工程A.xcodeproj
project_path =  'xxxPath/A.xcodeproj'
project = Xcodeproj::Project.open(project_path)

#找到要插入的group (参数中true表示如果找不到group,就创建一个group)
group = project.main_group.find_subpath(File.join('GroupName'),true)

#set一下sorce_tree
group.set_source_tree('SOURCE_ROOT')

#向group中增加文件引用(.h文件只需引用一下,.m引用后还需add一下)
file_ref = group.new_reference('xxxPath/xx.h')

file_ref = group.new_reference('xxxPath/xx.m')
ret = target.add_file_references(file_ref)

project.save

4.通过xcodeproj在工程中引入framwork、.a文件和bundle文件

require 'xcodeproj'

#打开项目工程A.xcodeproj
project_path =  'xxxPath/xx.xcodeproj'
project = Xcodeproj::Project.open(project_path)

#遍历target,找到需要操作的target

targetIndex = 0

project.targets.each_with_index do |target, index|
  if target.name  == "xxxTargetName"
    targetIndex = index
  end
end

target = project.targets[targetIndex]

#添加xx.framework的引用
file_ref = project.frameworks_group.new_file('xxPath/xx.framework')
target.frameworks_build_phases.add_file_reference(file_ref)

#添加xx.a的引用
file_ref = project.frameworks_group.new_file('xxPath/xx.a')
target.frameworks_build_phases.add_file_reference(file_ref)

#添加xx.bundle的引用
file_ref = project.frameworks_group.new_file('xxPath/xx.bundle')
target.resources_build_phase.add_file_reference(file_ref)

project.save

5.提供过xcodeproj在把xx.framework加入到copy files phase中(这里不限framework,也可以是其他类型文件)

QQ图片20170728161226.png
上一篇 下一篇

猜你喜欢

热点阅读