美文网首页iOS工程架构与组件化
iOS 3-新建Target管理项目的调试,测试,发布版本。

iOS 3-新建Target管理项目的调试,测试,发布版本。

作者: ksang | 来源:发表于2017-09-27 22:48 被阅读60次

    iOS新建Target来管理项目的调试、测试、发布等不同版本。

    应用场景

    一个项目,不同环境下的不同版本。(测试,发布,模拟器,真机)

    例如,我公司最近老大提出一个设想,希望能让公司项目在模拟器上面跑。但是我们项目中有用到POS机的SDK,不做对应的处理是不可以正常运行到模拟器上的。
    我的第一个想法就是从主线拉个分支去做,把对应引用到POS机SDK的地方都删除或者注释掉。很显然,第二天就被否了。O(∩_∩)O哈哈~因为你不可能每天都去更新这个分支。所以在得到老大跟同事的引导之后,还是确实需要用Target去做滴。

    官方文档中的介绍
    A target specifies a product to build and contains the instructions for building the product from a set of files in a project or workspace. A target defines a single product; it organizes the inputs into the build system—the source files and instructions for processing those source files—required to build that product. Projects can contain one or more targets, each of which produces one product.
    
    The instructions for building a product take the form of build settings and build phases, which you can examine and edit in the Xcode project editor. A target inherits the project build settings, but you can override any of the project settings by specifying different settings at the target level. There can be only one active target at a time; the Xcode scheme specifies the active target.
    

    英文好的同学可能很快就知道了Target的妙用,在一个Xcode工程中,每个Target单独对应一个应用(A target defines a single product),因为我们可以在每个Target中单独设置属于这个Target的配置文件属性。

    官方文档地址:《苹果官方文档-Xcode Target部分》
    滴"滴"滴"下面开始发车~

    创建Target

    1、「右键Duplicate-Duplicate Only」复制单个Target 或者 新建Target,然后重新命名
    
    2、修改Target对应生成的「xxx copy.plist」的工程配置文件的名字
    
    3、Target对应「Build Settings」下配置Info.plist路径,或者直接在「General」中选择您对应的info.plist。(建议后者,可视化操作嘿嘿~)
    
    4、因为重新命名了Target的名字,所以需要在「Schemes」中选择「Manage Schemes」删除之前为copy的schemes,然后重新添加当前所有的Target的Schemes。(这个时候有一个注意点!需要勾选添加之后的schemes的「Shared」一项。)
    
    5、配置预编译的宏,这个宏的作用就类似于我们之前在.pch之间用「#if DEBUG- #else- #endif」的DEBUG,我们切换选择项目的「Build Configuration」的时候,NSLog会不同显示。
    配置的位置:在「Target-Preprocessor Macros」下的Debug和Release中。宏名字没有特殊要求, 格式有两种,一种为:XXXXXXXXX = 0或者"NEW_TARGET名字" ; 另一种为 xxxxxxxx
    6、在类中添加匹配宏代码,区分Target,并且添加属于该Target的代码。方式有三种为:
    第一种:
    
            //#ifdef target1的预编译宏名字
            #if target1的预编译宏名字
               // target1下的特有代码,比如说请求IP地址
            #else
               // 其他target下的特有代码,比如说请求IP地址
            #endif
    
    第二种:
    
            // #ifdef xxxxxx
            #if xxxxxx == 0 或者 NEW_TARGET名字
             // 该target下的特有代码,比如说请求IP地址
            #elif xxxxxx == 1 或者 NEW_TARGET名字
             // 该target下的特有代码,比如说请求IP地址
            #else
             // 没找到匹配的预编译宏
            #endif
    
    第三种:
    
            if (xxxxxx == 1 或者 NEW_TARGET名字) { 
              // 该target下的特有代码,比如说请求IP地址
            } else {
              // 其他target下的特有代码,比如说请求IP地址
            }
    7、这步骤是可选的,但强烈推荐。如果您希望使开发和生产构建更容易,并且防伪,您应该为每个版本使用单独的图标和启动屏幕。这将使您的测试人员知道他们正在使用哪个应用程序,并希望阻止您发送开发版本。
       我们知道设置应用图标和和启动图片的有两种方式,一种是把图片加到「Assets.xcassets」中,另一种是直接把图片加到项目文件下。我只测试了图片加到「Assets.xcassets」这种,在「Assets.xcassets」创建之后,修改或者不修改图片夹的名字后,直接在「Target-General」中选择对应的图片夹即可。
    8、 如果你使用了Cocoapods的话,需要把新的Target添加到podfile文件中。有两种添加方式。
    第一种:
    
        platform :ios, '8.0'
        workspace 'xxxxxxxxx'  
        // 添加第三方框架       
        target 'xxxxxx' do
        pod 'AFNetworking', '3.1.0'
        end
        // 添加的内容和上边的完全一样
        target 'xxxxx' do
        pod 'AFNetworking', '3.1.0'
        end
    
    第二种:直接用link_with关联多个Target
    
        platform :ios, '8.0'  
        workspace 'xxxxxxxxx'  
        link_with 'target1', 'target2'  
        pod 'xxxxxx'
    
    

    图片说明

    新建target.png Duplicate only.png NewAppIDIcon.png 也可以在General中直接选择对应的info.plist.png build settings里面设置预编译宏,可在代码里直接使用.png 代码中用到之前在Preproessor Macors中设置的预编译的宏.png image-asset.jpg 新建target之后拖拽新的资源文件的时候.png
    注意点,新建Target之后,新拖入项目中的文件,勾选对应需要该文件的Target.
    
    但是做了这么多,还是不能解决我项目有POS机SDK编译不通过的问题,O(∩_∩)O哈哈~
    因为General-->Linked Frameworks and Libraries导入了对应的framework 跟 Build Phases-->Compile Sources中有文件引用到那些SDK,需要删掉。。。最终完成了编译及运行。。。
    
    好记性不如烂笔头

    如果有什么不对的,请大神们及时纠正.并且赏个 💗 吧O(∩_∩)O哈哈~

    相关文章

      网友评论

      本文标题:iOS 3-新建Target管理项目的调试,测试,发布版本。

      本文链接:https://www.haomeiwen.com/subject/gpqpextx.html