我应该设置在Xcode中创建
答
我建议设立两个模式。
第一个:开发 - >设置与Debug
构建配置。 您可以在开发应用时使用此功能。这会给你日志记录,调试容易等。
第二个:配置 - >设置与Release
构建配置。 日志记录不会发生在这个模式上,调试也将不可用,因为构建没有为此优化。
当您准备向App Store提交资料时,请使用Release
构建配置来归档Distribution模式。
有关Debug
和Release
构建配置之间的区别的更多详细说明here。
答
这将深入探讨您的问题。我使用了构建配置环境,以便在发布配置中进行构建。您的自动值将根据发布版本进行调整。您还可以从下面的链接下载示例代码,以实际了解更改方案时会发生什么情况。
第一步。
在info.plist中添加变量“Configuration”,并在其中添加值“$(CONFIGURATION)”。
制作一个Config.swift文件并复制粘贴下面的代码。
`
import Foundation
private let configManagerSharedInstance = ConfigManager()
class Config {
class var sharedInstance: ConfigManager {
return configManagerSharedInstance
}
}
// You can put as much Environment as you need but you make sure you also put these environment in the config.plist file.
enum Environment: String {
case Debug = "Debug"
case Production = "Release"
}
class ConfigManager: NSObject {
private var environment: Environment?
var config: NSDictionary?
override init() {
super.init()
// Retrieve the current evironment from the main bundle
if let currentEnvironment = Bundle.main.infoDictionary?["Configuration"] as? String {
// Store the current environment for later use
environment = Environment(rawValue: currentEnvironment)
if let projectConfigPath = Bundle.main.path(forResource: "Config", ofType: "plist") {
if let projectConfigContents = NSDictionary(contentsOfFile: projectConfigPath) as? Dictionary<String, AnyObject> {
config = projectConfigContents[currentEnvironment] as? Dictionary<String, AnyObject> as NSDictionary?
}
} else {
print("config file not found")
}
}
}
func getCurrentEnvironment() -> Environment? {
return self.environment
}
func configForKey(key: String) -> String {
return config?[key] as! String
}
//It will use to get sub dictionary and their values.
func configForCategory(category: String, andKey: String) -> String {
let configuration = config?.value(forKeyPath: category) as! NSDictionary
return configuration.value(forKeyPath: andKey) as! String
}
}
`
我也取得了文件Constants.swift在我所使用上述代码设定的可变因素。 `
//
// Constants.swift
// BuildConfiguration
//
// Created by Ourangzaib khan on 4/6/17.
// Copyright © 2017 Ourangzaib khan. All rights reserved.
//
let kBASE_URL : String = {
print(Config.sharedInstance.configForKey(key: "kBASE_URL"));
return Config.sharedInstance.configForKey(key: "kBASE_URL")
}()
let STRIPEKEY : String = {
return Config.sharedInstance.configForCategory(category: "Stripe", andKey: "Publishable Key")
}()
let PUBNUBKEYSUBSCRIBE : String = {
return Config.sharedInstance.configForCategory(category: "PubNub", andKey: "Publish Key")
}()
let PUBNUBKEYPUBLISH : String = {
return Config.sharedInstance.configForCategory(category: "PubNub", andKey: "Subscribe Key")
}()
let WOWZAKEY : String = {
return Config.sharedInstance.configForKey(key: "Wowza");
}()
`
现在你只需要使用编辑sceme进入编辑方案来选择环境和选择构建配置现在,当你运行该项目,你会看到这样的输出WRT在下面的图像中构建配置。
如果你是指打印()日志记录,将在发布发生还建立,如果你不积极镇压。 – shallowThought