美文网首页
使用Xcode Targets 管理Dev和Product配置

使用Xcode Targets 管理Dev和Product配置

作者: anddygon | 来源:发表于2016-09-27 19:34 被阅读197次

    一般我们开发完新的版本,准备提交到AppStore审核的时候,我们需要把项目里的指向测试环境的URLs和API Keys切换到正式环境。每次都是如此很累而且容易出错。

    以往我们处理这种问题会通过类似的方法

    enum environmentType {
        case development, production
    }
     
    let environment:environmentType = .production
     
    switch environment {
    case .development:
        // set web service URL to development
        // set API keys to development
        print("It's for development")
    case .production:
        // set web service URL to production
        // set API keys to production
        print("It's for production")
    }
    

    这样处理之后情况好了很多,我们在提交审核的时候只需要更改这个全部变量为正式环境。

    但是这个时候如果我们想要在同一个设备上同时安装测试版本和正式版本,我们还需要更改bundle ID,我们安装的版本没法很明显区分到底是测试版本还是正式版,问题又来了。


    那么小伙伴们不用担心,下面我们一步一步来学习怎么使用Xcode的Targets完成这个任务

    • How to Create a New Target

    1.创建Target

    创建Target

    2.选择Duplicate Only

    选择Duplicate Only
    如果你的项目Device是universal devices,Xcode不会出现这个对话框。

    3.对新建的Target重命名

    • 选中新建的Target 按下Enter编辑名称,我比较喜欢 '项目名称 Dev'
    • 然后去Manage Schemes…,选中刚才创建的‘scheme’按下Enter修改为何刚才Target一样的名字
    前后对比

    4.这一步不是必须的,但是强烈推荐,因为这一步我们可以很容易区分测试和线上版本从App图标

    • 选中Assets.xcassets,给Target Dev添加图标
      添加Dev图标

    5.返回项目设置,选择Target Dev改变bundle identifier,这样我们就可以同时安装开发和生产环境的包了,然后设置启动图标为第4不添加的icon

    项目设置

    6.Xcode在我们第一步创建Target的时候就同时创建了一个xxx info.plist的副本,修改名字为 项目名称-Dev

    7.打开Target Dev 的 Build Settings,找到Packaging

    8.配置全局宏,分为OC和Swift(网上找的)

    • For Objective-C projects, go to Build Settings
      and scroll to Apple LLVM 7.0 - Preprocessing
      . Expand Preprocessor Macros
      and add a variable to both Debugand Release fields. For the development target (i.e. todo Dev), set the value toDEVELOPMENT=1
      . On the other hand, set the value to DEVELOPMENT=0
      to indicate a production version.
      OC
    • For Swift project, the compiler no longer supports preprocessor directives. Instead, it uses compile-time attributes and build configurations. To add a flag to indicate a development build, select the development target. Go to Build Settings
      and scroll down to the Swift Compiler - Custom Flags
      section. Set the value to -DDEVELOPMENT
      to indicate the target as a development build.


      Swift

    9.使用方法,分为OC和Swift

    • Objective-C:
    #if DEVELOPMENT
    #define SERVER_URL @"http://dev.server.com/api/"
    #define API_TOKEN @"DI2023409jf90ew"
    #else
    #define SERVER_URL @"http://prod.server.com/api/"
    #define API_TOKEN @"71a629j0f090232"
    #endif
    
    • Swift:
      #if DEVELOPMENT
    let SERVER_URL = "http://dev.server.com/api/"
    let API_TOKEN = "DI2023409jf90ew"
    #else
    let SERVER_URL = "http://prod.server.com/api/"
    let API_TOKEN = "71a629j0f090232"
    #endif
    

    10.现在当你选择’项目名称 Dev‘ scheme运行,Xcode就会用你定义的去配置为开发环境,你可以直接上传到TestFlight 或者其他测试平台。
    如果你想切换到生产环境,只需要切换到 ’项目名称‘cheme,不需要修改任何代码,是不是很爽😄


    对于多Targets需要注意的

    1. 当你给项目添加文件的时候,别忘了把文件同事包含到Dev Target中,让代码同步

    添加新文件
    2.如果你是使用Cocoapods来管理依赖,需要把Target Dev添加到podfile中。
    语法可能会改变具体查看Cocoapods文档
    source 'https://github.com/CocoaPods/Specs.git'
    platform :ios, '7.0'
    workspace 'todo'
    link_with 'todo', 'todo Dev'
    pod 'Mixpanel'
    pod 'AFNetworking'
    

    相关文章

      网友评论

          本文标题:使用Xcode Targets 管理Dev和Product配置

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