将许可证部分添加到 iOS 设置包的最佳方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6428353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:27:33  来源:igfitidea点击:

best way to add license section to iOS settings bundle

iosplistsettings.bundle

提问by JosephH

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.

这似乎是一种几乎可以肯定有某个地方的脚本要做的事情,或者我错过了一些简单的方法。

回答by JosephH

I think I've now managed to solve all the problems I was running into.

我想我现在已经设法解决了我遇到的所有问题。

  • It seems to be best to use group element titles to hold the licenses (this is what Apple do in the iWork apps). There is however a limit on the length of these (and I've not yet discovered exactly what the limit is), so you need to break each license file into multiple strings.
  • You can create a line break within these by include a literal carriage return (ie. otherwise known as ^M, \r or 0x0A)
  • Make sure not to include any literal "s mid-text. If you do, some or all of the strings in the file will get silently ignored.
  • 似乎最好使用组元素标题来保存许可证(这是 Apple 在 iWork 应用程序中所做的)。然而,它们的长度是有限制的(我还没有发现确切的限制是什么),所以你需要将每个许可证文件分成多个字符串。
  • 您可以通过包含文字回车符(即,也称为 ^M、\r 或 0x0A)在其中创建换行符
  • 确保不要包含任何文字 "s 中间文本。如果这样做,文件中的部分或全部字符串将被静默忽略。

I've got a convenience script I use to help generate the .plist and .strings file, shown below.

我有一个方便的脚本来帮助生成 .plist 和 .strings 文件,如下所示。

To use it:

要使用它:

  1. Create a 'licenses' directory under your project
  2. Put script into that directory
  3. Put each license into that directory, one per file, with filenames that end .license
  4. Perform any necessary reformatting on the licenses. (eg. remove extra spaces at the beginning of lines, ensure that there are no line breaks mid-paragraph). There should be a blank line in-between each paragraph
  5. Change to licenses directory & run the script
  6. Edit your settings bundle Root.plist to include a child section called 'Acknowledgements'
  1. 在您的项目下创建一个“许可证”目录
  2. 将脚本放入该目录
  3. 将每个许可证放入该目录中,每个文件一个,文件名以 .license 结尾
  4. 对许可证执行任何必要的重新格式化。(例如,删除行首的多余空格,确保段落中间没有换行符)。每个段落之间应该有一个空行
  5. 更改到许可证目录和运行脚本
  6. 编辑您的设置包 Root.plist 以包含一个名为“Acknowledgements”的子部分

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);

Setting up your Settings.bundle

设置您的 Settings.bundle

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.bundlegroup and select Root.plist. You will need to add a new section where its key will be Preference Itemsof type Array. Add the following information:

展开Settings.bundle组并选择Root.plist。您将需要添加一个新部分,其键Preference Items的类型为Array。添加以下信息:

enter image description here

在此处输入图片说明

The Filenamekey points to the plist that was created by this script. You can change the titleto what ever you want.

Filename关键点,是由该脚本创建的plist中。您可以将 更改为title您想要的任何内容。

Execute Script At Build Time

在构建时执行脚本

Also, if you want this script to run whenever you build your project, you can add a build phase to your target:

此外,如果您希望在构建项目时运行此脚本,则可以向目标添加构建阶段:

  1. Go to your project file
  2. Select the target
  3. Click the Build Phases tab
  4. In the lower right corner of that pane, click on 'Add Build Phase'
  5. Select 'Add Run Script'
  6. Drag and drop your perl script into the section for your script. Modify to look something like this:
  1. 转到您的项目文件
  2. 选择目标
  3. 单击构建阶段选项卡
  4. 在该窗格的右下角,单击“添加构建阶段”
  5. 选择“添加运行脚本”
  6. 将您的 perl 脚本拖放到脚本部分。修改为如下所示:
  1. cd $SRCROOT/licenses($SRCROOTpoints to the root of your project)
  2. ./yourScriptName.pl
  1. cd $SRCROOT/licenses$SRCROOT指向您项目的根目录)
  2. ./yourScriptName.pl

After you have finished that, you can drag the Run Scriptbuild phase sooner in the build process. You'll want to move it up before Compile Sourcesso that the updates to your Settings Bundle get compiled and copied over.

完成后,您可以Run Script在构建过程中更快地拖动构建阶段。您需要先将其向上移动,Compile Sources以便对设置包的更新进行编译和复制。

Update for iOS 7:iOS 7 seems to handle the "Title" key different and is messing up the rendered text. To fix that the generated Acknowledgements.plist needs to use the "FooterText" key instead of "Title". This how to change the script:

iOS 7 更新:iOS 7 似乎处理“标题”键不同,并且弄乱了呈现的文本。要解决生成的 Acknowledgements.plist 需要使用“FooterText”键而不是“Title”的问题。这是如何更改脚本:

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);
}

回答by Sean

Here's the same solution that @JosephH provided (without translations), but done in Python for anyone who prefers python over perl

这是@JosephH 提供的相同解决方案(没有翻译),但对于喜欢 python 而不是 perl 的任何人来说,都是用 Python 完成的

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"
)

回答by JosephH

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). The property list file that can be added to the iOS settings bundle.

作为替代方案,对于那些使用 CocoaPods 的人,它将为 Podfile 中指定的每个目标生成一个“确认”plist,其中包含该目标中使用的每个 Pod 的许可证详细信息(假设详细信息已在 Pod 规范中指定)。可以添加到 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/CPAcknowledgements

回答by carloe

I thought I'd throw my iteration on Sean's awesome python code in the mix. The main difference is that it takes an input directory and then recursively searches it for LICENSE files. It derives the title value from the parent directory of the LICENSE file, so it plays well with cocoapods.

我想我会把我的迭代放在 Sean 很棒的 Python 代码上。主要区别在于它需要一个输入目录,然后递归地搜索 LICENSE 文件。它从 LICENSE 文件的父目录中获取 title 值,因此它可以很好地与 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. It also does some other things like remove forced newlines from licenses so the paragraphs look a bit better on the devices.

动机是创建一个构建脚本,以便在我添加或删除 Pod 时自动使我的应用程序的合法部分保持最新。它还执行其他一些操作,例如从许可证中删除强制换行符,以便段落在设备上看起来更好一些。

https://github.com/carloe/LicenseGenerator-iOS

https://github.com/carloe/LicenseGenerator-iOS

enter image description here

在此处输入图片说明

回答by cvknage

I made a script in Ruby inspiered by @JosephH script. This version will, in my own opinion, better represent the individual open source projects.

我在@JosephH 脚本的启发下用 Ruby 制作了一个脚本。在我看来,这个版本将更好地代表各个开源项目。

Wisit iOS-AcknowledgementGeneratorto download the script and sample project.

Wisit iOS-AcknowledgementGenerator下载脚本和示例项目。

This is what acknowledgements will look like in your App:

这就是确认在您的应用程序中的样子:

Settings.appSettings.bundleAcknowledgementsenter image description here

Settings.appSettings.bundleAcknowledgementsenter image description here

回答by mattti

This is an addendum to JosephH's answer. (I don't have the rep to comment)

这是 JosephH 答案的附录。(我没有代表发表评论)

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>向下移动 到</dict>Perl 脚本中的最后一个之上。

Before this modification, the Acknowledgements Section in the App was empty and XCode couldn't read the resulting Acknowledgements.plist. ( "The data couldn't be read because it isn't in the correct format.")

在此修改之前,App 中的 Acknowledgements 部分是空的,XCode 无法读取生成的 Acknowledgements.plist。(“无法读取数据,因为它的格式不正确。”)

(XCode 6.3.2 iOS 8.3)

(XCode 6.3.2 iOS 8.3)

回答by D. Rothschild

The Python script from Sean in this thread works. But there a couple of basic things to know.

这个线程中来自 Sean 的 Python 脚本有效。但是有一些基本的事情需要了解。

  1. in Xcode, right click on the top of the Project Navigator tree, on the name of your project, and add a New Group. This puts a new folder in your project.
  2. Add Sean's script there and make sure to save it as: Acknowledgements.py.
  3. Make sure you have Python installed on your system. I'm using a Mac.
  4. Add a first license file to the folder you created in 1. Make it simple like just having one word in the file, say: Testing. Save it in the folder as Test1.license.
  5. Set up your Settings.bundle as per JosephH above.
  6. Use your Terminal app to CD to the folder you created in 1.
  7. Run the script. Type: python Acknowledgements.py. If you get no errors it will return right back to the Terminal prompt. Do all of this before adding any run script to the Build.
  8. Build and run your app.
  9. Double tap on the iPhone home button and kill Settings. It doesn't often pick up the Settings change for your app until Settings restarts.
  10. After restarting Settings, go to your app and look to see if it worked.
  11. If that all worked, slowly add more license files but run the script each time. You can get errors running the script because of certain characters in the file so the easy way to debug is to add a file, run the script, see if it worked and proceed. Else, edit any special characters out of the .license file.
  12. I did not get the Run Build Script work per the instructions above. But this process works fine if you are not changing the .license files that often.
  1. 在 Xcode 中,右键单击项目导航树顶部的项目名称,然后添加一个新组。这会在您的项目中放置一个新文件夹。
  2. 在那里添加 Sean 的脚本并确保将其保存为:Acknowledgements.py。
  3. 确保您的系统上安装了 Python。我正在使用 Mac。
  4. 将第一个许可证文件添加到您在 1 中创建的文件夹中。让它变得简单,就像在文件中只包含一个词一样,例如:测试。将其保存在文件夹中作为 Test1.license。
  5. 按照上面的 JosephH 设置您的 Settings.bundle。
  6. 使用您的终端应用程序 CD 到您在 1 中创建的文件夹。
  7. 运行脚本。类型:python Acknowledgements.py。如果您没有收到任何错误,它将立即返回到终端提示。在将任何运行脚本添加到 Build 之前执行所有这些操作。
  8. 构建并运行您的应用程序。
  9. 双击 iPhone 主页按钮并取消设置。在设置重新启动之前,它通常不会为您的应用程序选择设置更改。
  10. 重新启动设置后,转到您的应用程序并查看它是否有效。
  11. 如果一切正常,请慢慢添加更多许可证文件,但每次都运行脚本。由于文件中的某些字符,您可能会在运行脚本时遇到错误,因此调试的简单方法是添加文件,运行脚本,查看它是否有效并继续。否则,编辑 .license 文件中的任何特殊字符。
  12. 我没有按照上面的说明运行构建脚本。但是,如果您不经常更改 .license 文件,则此过程可以正常工作。

回答by Zyphrax

Ack Ack: Acknowledgement Plist Generator
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. It does a lot of the work for you.

Ack Ack:Acknowledgment Plist Generator 不久前
,我创建了一个 Python 脚本,用于扫描许可证文件并创建一个不错的 Acknowledgments plist,您可以在 Settings.plist 中使用它。它为你做了很多工作。

https://github.com/Building42/AckAck

https://github.com/Building42/AckAck

Features

特征

  • Detects Carthage and CocoaPods folders
  • Detects existing plists for custom licenses
  • Cleans up the license texts by removing unnecessary new lines and line breaks
  • Provides many customization options (see --helpfor details)
  • Supports both Python v2 and v3
  • 检测 Carthage 和 CocoaPods 文件夹
  • 检测自定义许可证的现有 plist
  • 通过删除不必要的新行和换行符来清理许可证文本
  • 提供许多自定义选项(--help有关详细信息,请参阅)
  • 支持 Python v2 和 v3

Install

安装

wget https://raw.githubusercontent.com/Building42/AckAck/master/ackack.py
chmod +x ackack.py

Run

./ackack.py

Screenshot

截屏

Acknowledgements

Acknowledgements

If you have suggestions for improvements, feel free to post an issue or pull request on GitHub!

如果您有改进建议,请随时在 GitHub 上发布问题或拉取请求!