English 中文(简体)
处理iOS复杂配置的首选方式/设计模式
原标题:Preferred way / Design pattern to handling complex configuration on iOS

假设您需要为一系列不同的对象设置一系列复杂配置。 这个配置可以是 NNSString 服务服务器地址, NNSNOD 像超时等等。 我想施加以下限制 :

  • Configuration must not be visible and/or editable to user so plist in the app bundle is not an acceptable solution
  • I need different configuration value for debug, AdHoc or release target (I will likely achieve this with a series of #ifdef but i write for sack of completeness)
  • All the object that i want to configure belongs to a Static Library and i want to inject the configuration from the code that use this static library (inject is in italic as my personal hint to a dependency injection)

我的问题是:实现这一点的最佳途径是什么?你曾经遇到过这样的问题吗?你是如何解决的?

最佳回答

利用 < 坚固> Xcode 构建系统 < / 坚固 > 。 您可以使用 < a href=" https:// stackoverflow.com/a/620437/ 143097" > Xcode 配置文件 来配置任何构建配置( 调试、 释放等) 和目标组合 。

这些文件支持继承以简化共享配置。 没有一种单一的方法来定义选项。 一个简单的办法是使用 GCC_PREPROESSOR_DEFINITONS CFLAGS 或任何您的编译器。

TagetX.xcconfig :

GCC_PREPROCESSOR_DEFINITIONS = STAGE=2 SERVER_URL= www.wuonm.com 

配置的数值对于构建所需的 NSStringNSNOUL 对象来说是微不足道的。 提示 : 您需要某种 < a href=>" https:// stackoverflow.com/ a/7953958/ 143097" >stringification 。

它与使用 相当相似,但IMHO如果投入一些时间 < a href=> http://code.google.com/p/google-toolbox-for-mac/source/browse/trunk/XcodeConfig/xcconfigs-readme.txt?r=523" rel="nofolfoln noreferr" > 解释 how xcconfig < a href=" http://code.google.com/p/google-toolbox-for-mac/poor/browse/trunk/Xcodeconfig/?? r=523#Xcodeconfig/subconfig" rel=“ nofol=“nofolnnoferr> > file < files < files < file < file < a/ a>

问题回答

如果这些值对每个构建配置都是不变的, 为什么不把它们作为常数放入您的代码中? 如果这些常数因配置而异, 我实在不明白为什么你需要将这些设置外部化。 您选择设置的 ifdef 将会同样有效( 更快), 这些常数会在私人信头中声明 。

如果这些设置在应用程序运行期间以某种方式发生变化(不是从构建到构建,而是在运行一个应用程序的单一构建的范围内),那么将这些设置外部化是合适的。

这个职位有许多问题:

  1. Why do you need this level of protection on your input configuration?
  2. Is the point that you dont wish the user to be able to modify your configuration, or is it that you dont wish them to be able to KNOW it at all?
  3. If you beleive the theory that SW cannot protect SW, then truly what level of pain are you willing to put yourself through in order to put the hacker through enough pain they dont want to hack your SW (because lets face it, thats all you are really going to get, see my comments above about encryption)
  4. I somewhat agree with getting your configuration from the internet, but this only will work IFF you are expecting an internet capability present in order to use your app. If you do this and you also want some level of protection then you are going to have to use some secure protocol (Like SSL) for downloading it.
  5. If you truly wish your app to be able to be used locally (without internet access), then I would say that true security of your configuration is simply immposible, as you dont control the OS, your dont control the HW your SW is running on, etc...
  6. Putting your app on the iPhone probably means that you are hoping for a fairly large amount of downloads. This means the amount of hands touching it and potentially being able to take a CRACK at it are also large. The only way to gain security in this case (as no SW really is), is to make the value of hacking it to get your configuration not worth all that much. If the value is low, then no hacker will care to try and crack it!

不同类别使用的服务器 URL 和 颜色、 大小 和 几乎任何配置 的 常数 等, 都需要 轻松 更改 。 我找到的最佳解决方案是创建像 Constants. h 这样的页眉文件, 此页眉文件的内容是 :

#define MAX_DIST 1000
#define MIN_DIST 300
#define ANIMATION_DURATION 0.010
#define PIXEL_MOVES 7
#define SENSITIVITY 14
#define VIEW_ANGLE 30 //Range of vision divided by 2
#define RAD_POS_X 415
#define RAD_POS_Y 15
#define BUTTON_VIEW_WIDTH PIXEL_MOVES*360
#define SCREEN_HEIGHT 480
#define BUTTON_WIDTH_CLOSE 180
#define BUTTON_HEIGHT_CLOSE 100
#define BUTTON_WIDTH_MEDIUM 100
#define BUTTON_HEIGHT_MEDIUM 60
#define BUTTON_WIDTH_FAR 60
#define BUTTON_HEIGHT_FAR 40
#define BUTTON_Y_POSITION_CLOSE 200
#define BUTTON_Y_POSITION_MEDIUM 135
#define BUTTON_Y_POSITION_FAR 90
#define SERVICE_URL @"http://my.server.com/SoapServer/SoapServiceWS"//

您甚至可以定义共同函数,例如

#define DEGREES_TO_RADIANS(__ANGLE__) ((__ANGLE__) / 180.0 * M_PI)

这样我就可以从一个文件配置我的应用程序的每个方面, 您可以使用您所说的 #ifdef 来管理不同的编译。 当然, 您必须导入文件常数 。 h 在任何需要访问任何这些常数的类别中 。

您的静态库应该提供一些接口, 以设置运行时将设置的当前配置 。

SetCurrentConfiguration(Configuration config);

Then "client" can configure it with needed configuration in runtime. Second step is load configuration from specified source, let s say xml file and inject it to static library via method above.

如果您不想让用户修改配置文件, 您可以使用某种加密, 然后解密 。

第一次使用时, 需要通过 HTTPS 显示并下载互联网连接, 您的配置并保存在记忆中( 如果您想要加密) 。

然后利用iOS的钥匙链, 并存储您的配置作为字符串( 基本上按序列顺序排列 ) 。

NSString *serializedConfig = ...;
[keychain setObject:serializedConfig  forKey@"MyConfig"];

获取键链中存储的配置 :

NSString *serializedConfig = [keychain objectForKey:@"MyConfig"];





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签