关于objective C:MacRuby:如何创建一个可以从外部配置的应用程序? | 珊瑚贝

MacRuby: How to create an app that can be configured from the outside?


我有一个 MacRuby 应用程序,旨在由其他人定制和分发。该应用程序实际上只有一些可以自定义的东西,但我想找到一种方法来打包应用程序,以便人们可以运行脚本,指定一些配置选项,并让它设置应用程序配置,以便它已准备好分发给其他人。

例如,我有一个指向 URL 的字符串。我希望有人能够更改此 URL,而无需在 XCode 中打开项目,也无需重新构建(或重新编译),以便 Windows 或 Linux 上的某人可以进行此更改。

这样的事情可能吗?我是 MacRuby 和 Objective-C 的新手,所以可能有一个我不知道的明显解决方案。

我的解决方案:

我使用了一个名为 AppConfig.plist 的 plist 文件,它看起来像这样:

1
2
3
4
5
6
7
8
<?xml version=“1.0” encoding=“UTF-8”?>
<!DOCTYPE plist PUBLIC“-//Apple//DTD PLIST 1.0//EN”“http://www.apple.com/DTDs/PropertyList-1.0.dtd”>
<plist version=“1.0”>
<dict>
    <key>homepage</key>
    <string>http://google.com</string>
</dict>
</plist>

我的 App Delegate 可以像这样访问它:

1
2
3
config_path = NSBundle.mainBundle.pathForResource(‘AppConfig’, ofType: ‘plist’)
@config = load_plist File.read(config_path)
# then access the homepage key like this: @config[‘homepage’]

正如 jkh 所说,最简单的方法是使用 plist。例如,您可以将所需的自定义设置存储在项目根目录下的 Stuff.plist 中,并使用以下命令访问它:

1
stuff = load_plist File.read(NSBundle.mainBundle.pathForResource(‘Stuff’, ofType: ‘plist’))

或者,例如,如果 Stuff.plist 在您的 Resources 文件夹中(它可能应该在的位置)

1
stuff = load_plist File.read(NSBundle.mainBundle.pathForResource(‘Stuff’, ofType:‘plist’, inDirectory:‘Resources’))

stuff 现在是你的东西的哈希(或 NSMutableDictionary)。例如,如果 Stuff.plist 看起来像这样:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version=“1.0” encoding=“UTF-8”?>
<!DOCTYPE plist PUBLIC“-//Apple//DTD PLIST 1.0//EN”“http://www.apple.com/DTDs/PropertyList-1.0.dtd”>
<plist version=“1.0”>
<dict>
  <key>my_stuff</key>
  <dict>
    <key>favorite_color</key>
    <string>green</string>
    <key>first_car</key>
    <string>Reliant K</string>
        </dict>
        <key>his_stuff</key>
        <dict>
    <key>favorite_color</key>
    <string>blue</string>
    <key>first_car</key>
    <string>240D</string>
        </dict>
  </dict>
  </plist>

您应该能够通过以下方式访问这些值:

1
    my_favorite_color = stuff[:my_stuff][:favorite_color]

我实际上并没有在应用程序包中对此进行测试,但我确实使用 macirb 对其进行了测试。要自己尝试一下,您可以使用以下命令从 macirb 加载 plist 文件:

1
stuff = load_plist File.read(‘/path/to/Stuff.plist’)

MacRuby 在 Kernel 上实现了 load_plist,但没有 write_plist 或类似的东西,但是 MacRuby 确实在 Object 上实现了 to_plist,所以任何东西都可以作为 plist 写入磁盘!

1
File.open(‘/path/to/new_plist.plist’,‘w’){|f| f.write([‘a’,‘b’,‘c’].to_plist)}

给你:

1
2
3
4
5
6
7
8
9
<?xml version=“1.0” encoding=“UTF-8”?>
<!DOCTYPE plist PUBLIC“-//Apple//DTD PLIST 1.0//EN”“http://www.apple.com/DTDs/PropertyList-1.0.dtd”>
<plist version=“1.0”>

    <string>a</string>
    <string>b</string>
    <string>c</string>
</array>
</plist>

现在用户可以直接通过 plist 定义自定义,并且已经构建的应用程序将在运行时读取这些值。请注意这一点,因为您不想意外 eval 任何 rm *s.



是的,像其他应用一样使用 CFPreferences。然后,您的用户可以使用”默认写入”命令(带有适合您应用的参数)来自定义其行为,而无需修改应用本身。

  • 在研究了CFPreferences之后,听起来这实际上是存储在操作系统上的”用户偏好”,而不是可以在安装应用程序之前设置的”开发者配置”,这是我真正尝试的实现。我弄错了吗?
  • CFPreferences(或者,我还应该注意,NSUserDefaults)是您配置应用程序行为的方式。我不知道您所说的”在安装应用程序之前设置”是什么意思,因为根据定义,未安装的应用程序是不可配置的(您会将配置绑定到什么?没有捆绑标识符!)。当然,您的应用程序可以附带可以更改的默认首选项集,在这方面我第一次错了:如果您希望能够使用 defaults(1) 命令,请使用 NSUserDefaults,而不是 CFPreferences。
  • 附言如果您希望应用程序由第 3 方 vendor重新配置,例如您看到在用户获得之前由中间方进行了自定义,您应该简单地在您的应用程序包中粘贴一个 plist 并直接从中读取数据。然后您可以为 vendor提供一些程序来自定义 plist。这实际上可能是您在这里所要求的。
  • 我认为 plist 方法是我想要的。你介意提供一个例子吗?


来源:https://www.codenong.com/8738424/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_8859.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?