Where to store global constants in an iOS application?

后端 未结 11 1650
孤城傲影
孤城傲影 2020-11-28 18:01

Most of the models in my iOS app query a web server. I would like to have a configuration file storing the base URL of the server. It will look something like this:

11条回答
  •  無奈伤痛
    2020-11-28 18:06

    The way I define global constants:


    AppConstants.h

    extern NSString* const kAppBaseURL;
    

    AppConstants.m

    #import "AppConstants.h"
    
    #ifdef DEBUG
    NSString* const kAppBaseURL = @"http://192.168.0.123/";
    #else
    NSString* const kAppBaseURL = @"http://website.com/";
    #endif
    

    Then in your {$APP}-Prefix.pch file:

    #ifdef __OBJC__
      #import 
      #import 
      #import "AppConstants.h"
    #endif
    

    If you experience any problems, first make sure that you have the Precompile Prefix Header option set to NO.

提交回复
热议问题