【问题标题】:Is it possible to read entitlements files in swift是否可以快速读取权利文件
【发布时间】:2016-05-17 15:30:44
【问题描述】:
我正在我的应用中实现通用深度链接。当我注册不同的域时,它会创建一个 AppName.entitlements 文件
我想像 plist 一样读取这个文件的值。
我试过了
if let path = NSBundle.mainBundle().pathForResource("AppName", ofType:
"entitlements") { }
但它返回nil
这样的文件可以读取吗?
【问题讨论】:
标签:
swift
xcode
readfile
entitlements
【解决方案1】:
该文件不会复制到您的应用中(请参阅 Xcode 的目标复选框)。仅用于建筑
权利是 Xcode 的配置文件
所以:没有
【解决方案2】:
我找到的解决方案:
将appGroup 添加到Info.Plist 文件中
<key>appGroup</key>
<string>group.com.acronis.mobile.ios.development</string>
要读取字符串,请使用以下代码:
extension String {
// MARK: - Static
static var appGroup: String = {
guard let bundleAppGroup = Bundle.main.infoDictionary?["appGroup"] as? String else {
fatalError("The application must contain appGroup in Info.plist file")
}
return bundleAppGroup
}()
}
如果需要,使用您的自定义脚本在构建之前更改entitlements 和Info.plist。
【解决方案3】:
我深入研究了这个主题,其实很简单。
您使用运行脚本来获取权利并将其合并到 Info.plist:
ENTITLEMENTS= # Path to Project.entitlements #
INFOPLIST= # Path to Info.plist #
echo "Writing entitlements to Info.plist";
KEY="entitlements-from-script";
/usr/libexec/PlistBuddy -c "delete $KEY" "$INFOPLIST";
/usr/libexec/PlistBuddy -c "add $KEY dict" "$INFOPLIST";
/usr/libexec/PlistBuddy -c "merge $ENTITLEMENTS $KEY" "$INFOPLIST";
现在你可以在代码中使用它了:
let entitlements = Bundle.main.infoDictionary?["entitlements-from-script"]