[英]best way to add license section to iOS settings bundle
My iOS application uses a number of third party components licensed under Apache 2.0 and similar licenses, which requires me to include various bits of text, this kind of thing:我的 iOS 应用程序使用了一些在 Apache 2.0 和类似许可证下获得许可的第三方组件,这需要我包含各种文本,例如:
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
There seems to be a reasonable precedent for putting this information under a 'License' subentry in settings bundle (on the ipad facebook, pages, keynote, numbers and wikipanion all seem to do this).将此信息放在设置包中的“许可证”子条目下似乎有一个合理的先例(在 ipad facebook 上,页面、主题演讲、数字和 wikipanion 似乎都这样做)。
I'm struggling a bit to actually achieve the same though;不过,我正在努力实现同样的目标; I seem to need to split the text up line by line and enter into xcode a line at a time (and xcode4 seems to have a crashing problem when editing the plists).
我似乎需要逐行拆分文本并一次输入 xcode 一行(并且 xcode4 在编辑 plist 时似乎有崩溃问题)。
It seems like the kind of thing that there's almost certainly a somewhere script to do, or some simple way to do it that I've missed.看起来几乎肯定有某个地方的脚本可以做,或者我错过了一些简单的方法。
I think I've now managed to solve all the problems I was running into. 我想我现在已经设法解决了我遇到的所有问题。
I've got a convenience script I use to help generate the .plist and .strings file, shown below. 我有一个方便脚本,用于帮助生成.plist和.strings文件,如下所示。
To use it: 要使用它:
Here's the script: 这是脚本:
#!/usr/bin/perl -w
use strict;
my $out = "../Settings.bundle/en.lproj/Acknowledgements.strings";
my $plistout = "../Settings.bundle/Acknowledgements.plist";
unlink $out;
open(my $outfh, '>', $out) or die $!;
open(my $plistfh, '>', $plistout) or die $!;
print $plistfh <<'EOD';
<?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>StringsTable</key>
<string>Acknowledgements</string>
<key>PreferenceSpecifiers</key>
<array>
EOD
for my $i (sort glob("*.license"))
{
my $value=`cat $i`;
$value =~ s/\r//g;
$value =~ s/\n/\r/g;
$value =~ s/[ \t]+\r/\r/g;
$value =~ s/\"/\'/g;
my $key=$i;
$key =~ s/\.license$//;
my $cnt = 1;
my $keynum = $key;
for my $str (split /\r\r/, $value)
{
print $plistfh <<"EOD";
<dict>
<key>Type</key>
<string>PSGroupSpecifier</string>
<key>Title</key>
<string>$keynum</string>
</dict>
EOD
print $outfh "\"$keynum\" = \"$str\";\n";
$keynum = $key.(++$cnt);
}
}
print $plistfh <<'EOD';
</array>
</dict>
</plist>
EOD
close($outfh);
close($plistfh);
If you haven't created a Settings.bundle, go to File --> New --> New File... 如果您尚未创建Settings.bundle,请转到文件 - >新建 - >新建文件...
Under the Resource section, find the Settings Bundle. 在“资源”部分下,找到“设置包”。 Use the default name and save it to the root of your project.
使用默认名称并将其保存到项目的根目录。
Expand the Settings.bundle
group and select Root.plist
. 展开
Settings.bundle
组并选择Root.plist
。 You will need to add a new section where its key will be Preference Items
of type Array
. 您需要添加一个新的部分,其中的键将是
Array
类型的Preference Items
。 Add the following information: 添加以下信息:
The Filename
key points to the plist that was created by this script. Filename
键指向此脚本创建的plist。 You can change the title
to what ever you want. 您可以将
title
更改为您想要的内容。
Also, if you want this script to run whenever you build your project, you can add a build phase to your target: 此外,如果您希望在构建项目时运行此脚本,则可以向目标添加构建阶段:
cd $SRCROOT/licenses
($SRCROOT
points to the root of your project)cd $SRCROOT/licenses
($SRCROOT
指向项目的根目录)./yourScriptName.pl
After you have finished that, you can drag the Run Script
build phase sooner in the build process. 完成后,您可以在构建过程中更快地拖动“
Run Script
构建阶段。 You'll want to move it up before Compile Sources
so that the updates to your Settings Bundle get compiled and copied over. 您需要在
Compile Sources
之前将其移动,以便编辑和复制设置包的更新。
Update for iOS 7: iOS 7 seems to handle the "Title" key different and is messing up the rendered text. iOS 7的更新: iOS 7似乎处理不同的“标题”键并且正在弄乱渲染的文本。 To fix that the generated Acknowledgements.plist needs to use the "FooterText" key instead of "Title".
要解决这个问题,生成的Acknowledgements.plist需要使用“FooterText”键而不是“Title”。 This how to change the script:
这个如何更改脚本:
for my $str (split /\r\r/, $value)
{
print $plistfh <<"EOD";
<dict>
<key>Type</key>
<string>PSGroupSpecifier</string>
<key>FooterText</key> # <= here is the change
<string>$keynum</string>
</dict>
EOD
print $outfh "\"$keynum\" = \"$str\";\n";
$keynum = $key.(++$cnt);
}
Here's the same solution that @JosephH provided (without translations), but done in Python for anyone who prefers python over perl 这是@JosephH提供的相同解决方案(没有翻译),但是在Python中为任何喜欢python over perl的人完成
import os
import sys
import plistlib
from copy import deepcopy
os.chdir(sys.path[0])
plist = {'PreferenceSpecifiers': [], 'StringsTable': 'Acknowledgements'}
base_group = {'Type': 'PSGroupSpecifier', 'FooterText': '', 'Title': ''}
for filename in os.listdir("."):
if filename.endswith(".license"):
current_file = open(filename, 'r')
group = deepcopy(base_group)
title = filename.split(".license")[0]
group['Title'] = title
group['FooterText'] = current_file.read()
plist['PreferenceSpecifiers'].append(group)
plistlib.writePlist(
plist,
"../Settings.bundle/Acknowledgements.plist"
)
As an alternative, for those using CocoaPods, it will generate an 'Acknowledgements' plist for each target specified in your Podfile which contains the License details for each Pod used in that target (assuming details have been specified in the Pod spec). 作为替代方案,对于那些使用CocoaPods的人,它将为Podfile中指定的每个目标生成一个“Acknowledgements”plist,其中包含该目标中使用的每个Pod的许可证详细信息(假设已在Pod规范中指定了详细信息)。 The property list file that can be added to the iOS settings bundle.
可以添加到iOS设置包的属性列表文件。
There's also projects under way to allow this data to be converted and displayed within the app instead: 还有正在进行的项目允许在应用程序中转换和显示此数据:
https://github.com/CocoaPods/cocoapods-install-metadata https://github.com/CocoaPods/cocoapods-install-metadata
https://github.com/cocoapods/CPDAcknowledgements https://github.com/cocoapods/CPDAcknowledgements
I thought I'd throw my iteration on Sean's awesome python code in the mix. 我以为我会把我的迭代扔在Sean的混合中很棒的python代码上。 The main difference is that it takes an input directory and then recursively searches it for LICENSE files.
它的主要区别在于它需要一个输入目录,然后以递归方式搜索它以获取LICENSE文件。 It derives the title value from the parent directory of the LICENSE file, so it plays well with cocoapods.
它从LICENSE文件的父目录派生标题值,因此它可以很好地与cocoapods一起使用。
The motivation was to create a build script to automatically keep the legal section of my app up to date as I add or remove pods. 我的动机是创建一个构建脚本,以便在添加或删除pod时自动保持我的应用程序的合法部分是最新的。 It also does some other things like remove forced newlines from licenses so the paragraphs look a bit better on the devices.
它还执行其他一些操作,例如从许可证中删除强制换行符,以便段落在设备上看起来更好一些。
https://github.com/carloe/LicenseGenerator-iOS https://github.com/carloe/LicenseGenerator-iOS
I made a script in Ruby inspiered by @JosephH script. 我在Ruby编写了一个由@JosephH脚本激发的脚本。 This version will, in my own opinion, better represent the individual open source projects.
在我看来,这个版本将更好地代表各个开源项目。
Wisit iOS-AcknowledgementGenerator to download the script and sample project. Wisit iOS-AcknowledgementGenerator下载脚本和示例项目。
This is what acknowledgements will look like in your App: 这是您的应用程序中的确认:
This is an addendum to JosephH's answer. 这是JosephH回答的补遗。 (I don't have the rep to comment)
(我没有代表评论)
I had to move <key>StringsTable</key> <string>Acknowledgements</string>
down to above the last </dict>
in the Perl script. 我不得不将
<key>StringsTable</key> <string>Acknowledgements</string>
向下移动到Perl脚本中的最后一个</dict>
之上。
Before this modification, the Acknowledgements Section in the App was empty and XCode couldn't read the resulting Acknowledgements.plist. 在此修改之前,应用程序中的Acknowledgements部分为空,XCode无法读取生成的Acknowledgements.plist。 ( "The data couldn't be read because it isn't in the correct format.")
(“无法读取数据,因为格式不正确。”)
(XCode 6.3.2 iOS 8.3) (XCode 6.3.2 iOS 8.3)
The Python script from Sean in this thread works. Sean在这个线程中的Python脚本有效。 But there a couple of basic things to know.
但有一些基本的事情要知道。
Ack Ack: Acknowledgement Plist Generator Ack Ack:确认Plist生成器
A while back I've created a Python script that scans for license files and creates a nice Acknowledgements plist that you can use in your Settings.plist. 前段时间我创建了一个Python脚本,用于扫描许可证文件并创建一个很好的Acknowledments plist,您可以在Settings.plist中使用它。 It does a lot of the work for you.
它为你做了很多工作。
https://github.com/Building42/AckAck https://github.com/Building42/AckAck
Features 特征
--help
for details) --help
) Install 安装
wget https://raw.githubusercontent.com/Building42/AckAck/master/ackack.py
chmod +x ackack.py
Run 跑
./ackack.py
Screenshot 截图
If you have suggestions for improvements, feel free to post an issue or pull request on GitHub! 如果您有改进建议,请随时在GitHub上发布问题或提出请求!
Aknowlist is a strong CocoaPod candidate that is actively maintained at the time of this writing. Aknowlist是一个强大的 CocoaPod 候选者,在撰写本文时正在积极维护。 It automates this licensing if you are okay with housing the licenses in your app rather than the settings bundle.
如果您可以在应用程序而不是设置包中容纳许可证,它会自动执行此许可。 It worked great for the project I was working on.
它对我正在从事的项目非常有用。
I had to modify sean's script for modern python3:我不得不为现代 python3 修改肖恩的脚本:
import os
import sys
import plistlib
from copy import deepcopy
os.chdir(sys.path[0])
plist = {'PreferenceSpecifiers': [], 'StringsTable': 'Acknowledgements'}
base_group = {'Type': 'PSGroupSpecifier', 'FooterText': '', 'Title': ''}
for filename in os.listdir("."):
if filename.endswith(".license"):
with open(filename, 'r') as current_file:
group = deepcopy(base_group)
title = filename.split(".license")[0]
group['Title'] = title
group['FooterText'] = current_file.read()
plist['PreferenceSpecifiers'].append(group)
with open("Acknowledgements.plist", "wb") as f:
plistlib.dump(plist, f)
Wait, license notation is not a setting.等等,许可证符号不是设置。
Edit: I think license notice is not a setting.编辑:我认为许可证通知不是设置。 I think it is irrational to expect users who want to check the license notice to open the settings app.
我认为期望想要查看许可通知的用户打开设置应用程序是不合理的。 Therefore, I thought we should create a page for the license notice in the appropriate place in the app.
因此,我认为我们应该在应用程序的适当位置为许可声明创建一个页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.