JQ Blog

ビルド環境設定(XCode)2

問題

この前、ここの記事のようにビルド環境を分けようとしてたが、環境を分けるだけで環境別に変数を設定したりするのは一つ一つ書かないといけなかったので、あまりも効率的ではなっかたからもっといい方法があるだろうと思って調べた結果をまとめる。

ソリューション

例えば、Railsにはymlファイルに環境別に変数を設定したり、environmentsフォルダーにconfigの設定をするようにXCodeにもPlistファイルに設定して使う。前の記事に続いてセッティングしたら良いと。

使い方

1. info.plistを編集

Alt text

2. Environment.plistを追加

plistの新規作成 Alt text 項目追加 Alt text

3. AppDelegateに設定

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

  let bundle = NSBundle.bundle()
  // Configuration取得
  let configuration = bundle.infoDictionary!["Configuration"]

  // Configurations.plist読み込み
  let path = bundle.pathForResource("Environments", ofType: "plist")
  let configurations = NSDictionary(contentsOfFile: path!)

  // URL取得
  let variables = configurations?.objectForKey(configuration!) as! [String : AnyObject]

  let urlStr = variables["URL"]

  (略)

4. ViewController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import UIKit

class ViewController: UIViewController, UIWebViewDelegate {

  @IBOutlet weak var webView: UIWebView!
  var appDelegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate

  override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.webView.delegate = self
        let initialUrl = NSURL(string: self.appDelegate.urlStr)
        let request = NSURLRequest(url:initialUrl as! URL)
        self.webView.loadRequest(request as URLRequest)
        webView.scrollView.isScrollEnabled = true
  }

  (略)

5. 使いやすい方法

Configurationクラスを新規作成

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import Foundation

final class ConfigurationManager {

    private static let sharedInstance = ConfigurationManager()

    private var configuration: String
    private var variables: NSDictionary

    private init() {

        // Singletonの確認用
        print("Configuration initialization")

        let bundle = Bundle.main
        // Fetch Current Configuration
        self.configuration = (bundle.infoDictionary?["Configuration"]) as! String
        // Load Configurations
        guard let path = bundle.path(forResource: "Environments", ofType: "plist") else {
            self.variables = [:]
            return
        }
        let configurations =  NSDictionary(contentsOfFile: path)
        // Load Variables for Current Configuration
        self.variables = configurations?.object(forKey: self.configuration) as! NSDictionary
    }

    class func Configuration() -> String {
        return sharedInstance.configuration
    }

    class func URL() -> String {
        guard let url = sharedInstance.variables["URL"] else {
            return ""
        }
        return url as! String
    }
}

Appdelegate編集

1
2
3
4
5
6
7
8
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

  let urlStr = ConfigurationManager.URL()

  (略)

6. 結果

適用

1. Environment.plistに追加

Alt text

2. ConfigurationManager.swiftにコード追加

1
2
3
4
5
6
7
8
9
10
11
12
13
import Foundation

final class ConfigurationManager {

  (略)

  class func BarColor() -> NSDictionary {
      guard let barColor = sharedInstance.variables["BarColor"] else {
          return "" as! NSDictionary
      }
      return barColor as! NSDictionary
  }
}

3. AppDelegate

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

  var window: UIWindow?
  let urlStr = ConfigurationManager.URL()
  let barColor = ConfigurationManager.BarColor()
  let appEnvironment = ConfigurationManager.Configuration()


  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
      // Override point for customization after application launch.

      return true
  }

  (略)

}

4. ViewController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import UIKit

class ViewController: UIViewController, UIWebViewDelegate {

    @IBOutlet weak var webView: UIWebView!
    var appDelegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
    @IBOutlet weak var Environment: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.webView.delegate = self
        let initialUrl = NSURL(string: self.appDelegate.urlStr)
        let request = NSURLRequest(url:initialUrl as! URL)
        self.webView.loadRequest(request as URLRequest)
        webView.scrollView.isScrollEnabled = true
        Environment.text = appDelegate.appEnvironment
        Environment.backgroundColor = UIColor(red: self.appDelegate.barColor["Red"] as! CGFloat, green: self.appDelegate.barColor["Green"] as! CGFloat, blue: self.appDelegate.barColor["Blue"] as! CGFloat, alpha: 1.0)
        Environment.textColor = UIColor.white
        Environment.textAlignment = .center

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

5. 結果



参照

http://stackoverflow.com/questions/40230396/ios-simple-way-to-manage-rest-end-points/40230742#40230742
http://qiita.com/negibouze/items/1c1c977cab5ef26224fd