Trustylib 库是 iOS 平台的信任商店库
该项目目前正在开发中,仅供少数客户使用。API 可能尚不稳定,并且可能在没有进一步通知的情况下进行更改。
Trustylib 库可以帮助您将 Trusted Shops 的 Trustmark
、Shop Grade
、Product Grade
和 Buyer Protection
UI 小部件集成到您的 iOS 应用中。
Trustmark
小部件显示由 Trusted Shops 颁发的信任证书的有效性。
如果在信任证书到期的情况下,Trustmark 小部件将显示为:
Shop Grade
小部件通过一个漂亮的动画效果展开,以显示商店的评级和状态(优秀、良好、公平等)。但是小部件仅显示整体评级和状态,并不显示商店实际评论的详细信息,例如评论文本、评论日期、附件等。
Buyer Protection
小部件显示商店买家保护金额的详细信息。此小部件从 CocoadPod 版本 1.1.0+
和 Swift Package 版本 1.1.0+
开始提供。
Product Grade
小部件显示产品图片、评分和状态(优秀、良好、公平等),有一个动画用户界面。此小部件从 CocoadPod 版本 1.2.1+
和 Swift Package 版本 1.2.1+
开始提供。
1. 安装
可以通过 Swift Package Manager 和 Cocoapods 将 Trustylib 添加到您的 iOS 项目中。
示例项目 有 Trustylib 库针对不同 iOS 技术栈的工作集成。
Swift Package Manager
Trustylib 库可以通过 Swift Package Manager 简单地添加到 xcode 项目中。以下是实现方法:
- 当 xcode 项目打开时,前往
文件 > 添加包...
- 在搜索框中输入 Trustylib 库的 git URL(《https://github.com/trustedshops-public/etrusted-ios-trustbadge-library.git》),xcode 将显示库详细信息。请选择“直到下一个主要版本”作为依赖规则,xcode 将自动填写 Trustylib 的最新版本号,即《
1.2.6
》
- 点击“添加包”按钮。Xcode 将克隆 Trustylib git 仓库并将其附加到 xcode 项目上,它应该看起来像这样:
- 太棒了!库已添加到 xcode 项目中,现在可以开始使用。
Cocoapods
如果尚未安装,您需要先安装 Cocoapods。有关安装和使用 Cocoapods 的详细信息,请参阅使用指南。
安装后,您需要在 podfile 中将 Trustylib
库作为 pod 包含进去,
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '13.0'
use_frameworks!
target 'MyApp' do
pod 'Trustylib'
end
然后在终端中运行 pod install 命令,
pod install
您现在应该已经将 Trustylib 库的最新版本(《1.2.6
》)添加到您的 xcode 项目中!
2. 配置
Trustylib 调用 TrustedShops 后端 API 为加载详细的信任证书有效期、等级/评分、买家保护等信息。TrustedShops 后端 API 有三个环境 开发
、测试
和 生产
,Trustylib 可以配置连接到这些环境之一,以加载您的商店和产品等级/评分详细信息。
Trustylib 在您的 iOS 应用程序的 info.plist
文件中寻找一个名为 TrustbadgeEnvironment
的属性。如果找到该属性,Trustylib 环境将相应设置。如果没有找到,库的默认环境将设置为 生产
。
为了向 TrustbadgeEnvironment
属性提供一个有效的值,您需要在您的 xcode 项目目标的构建设置中添加一个用户定义的属性(可能的值为 开发
、测试
和 生产
)。以下是完成方法:
3. 添加 Trustylib UI 小部件
Trustylib 拥有一个 TrustbadgeView
视图,这使得展示 Trustmark
、商店等级
、产品等级
和 买家保护
小部件变得非常简单。我们只需提供如 TSID
、频道 id
、产品 id
和 上下文
这样的输入,然后 TrustbadgeView 就会展示所需的小部件。
以下是创建 Trustmark 小部件的方法:
TrustbadgeView(
tsId: "X330A2E7D449E31E467D2F53A55DDD070",
context: .trustMark
)
以下几行代码创建商店等级小部件:
TrustbadgeView(
tsId: "X330A2E7D449E31E467D2F53A55DDD070",
channelId: "chl-b309535d-baa0-40df-a977-0b375379a3cc",
context: .shopGrade
)
Buyer Protection 小部件可以通过以下几行代码创建:
TrustbadgeView(
tsId: "X330A2E7D449E31E467D2F53A55DDD070",
channelId: "chl-b309535d-baa0-40df-a977-0b375379a3cc",
context: .buyerProtection
)
以下是添加产品等级小部件的方法:
TrustbadgeView(
tsId: "X330A2E7D449E31E467D2F53A55DDD070",
channelId: "chl-c0ad29ff-a086-4191-a663-82fed64f6f65",
productId: "31303033",
context: .productGrade
)
产品 ID 只在产品等级小部件中需要,它代表产品 SKU。有关产品 ID 的任何帮助,请通过电子邮件联系 Trsuted Shop 的 CSM:[email protected]。
您的 TSID
通常在加入过程期间与 Trusted Shops 共享。如果您没有它,请通过电子邮件联系 Trsuted Shop 的 CSM:[email protected]。您也可以通过电子邮件与 Trusted Shop 的移动工程团队联系:[email protected]
ChannelId
在 Trusted Shop 的 控制中心 网址中可用。例如,在以下网址中https://app.etrusted.com/etrusted/reviews/inbox?channels=chl-2bf4346e-9897-4e3c-8793-bdbf15c007ae,频道 id 是 chl-2bf4346e-9897-4e3c-8793-bdbf15c007ae
。以下是控制中心网址中的样子:
4. 展示 Trustmark 小部件
在将 Trustylib 库添加到您的 XCode 项目之后,在 iOS 应用中展示 Trustmark 小部件相当简单。以下是针对不同 iOS 技术栈添加小部件的代码示例:
Swift with SwiftUI
由于 Trustylib 小部件使用 SwiftUI 创建,因此在 SwiftUI 项目中使用它们非常简单。
import Trustylib
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
Spacer()
TrustbadgeView(
tsId: "X330A2E7D449E31E467D2F53A55DDD070",
context: .trustMark
)
.frame(height: 75)
}
.padding()
}
}
Swift with UIKit
由于 Trustylib 小部件使用 SwiftUI 框架创建,因此我们需要使用 UIHostingController 包装 Trustbadge 小部件,以便将其添加到使用 UIKit 框架创建的视图中。以下是完成方法:
import UIKit
import SwiftUI
import Trustylib
class RootViewController: UIViewController {
private lazy var trustbadgeView: UIHostingController = {
let trustbadge = TrustbadgeView(
tsId: "X330A2E7D449E31E467D2F53A55DDD070",
context: .trustMark
)
return UIHostingController(rootView: trustbadge)
}()
override func viewDidLoad() {
super.viewDidLoad()
self.addTrustbadgeView()
}
private func addTrustbadgeView() {
self.addChild(self.trustbadgeView)
self.view.addSubview(self.trustbadgeView.view)
/// Setup the constraints to update the SwiftUI view boundaries.
self.trustbadgeView.view.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
self.trustbadgeView.view.centerYAnchor.constraint(equalTo: self.view.centerYAnchor),
self.trustbadgeView.view.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 16),
self.trustbadgeView.view.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -16),
self.trustbadgeView.view.heightAnchor.constraint(equalToConstant: 75)
])
}
}
Objective-C with UIKit
Trustylib 库有一个 TrustbadgeViewWrapper,用于将 Trustbadge 视图添加到 Objective-C 代码中。下面的代码展示了如何完成:
#import "RootViewControllerObjectiveC.h"
#import <Trustylib/Trustylib-Swift.h>
@interface RootViewControllerObjectiveC ()
@end
@implementation RootViewControllerObjectiveC{
UILabel *label;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
UIViewController *trustbadgeViewController = [
TrustbadgeViewWrapper
createTrustbadgeViewWithTsId:@"X330A2E7D449E31E467D2F53A55DDD070"
context: TrustbadgeContextTrustMark
];
[self addChildViewController: trustbadgeViewController];
[self.view addSubview: trustbadgeViewController.view];
trustbadgeViewController.view.translatesAutoresizingMaskIntoConstraints = NO;
[trustbadgeViewController.view.centerYAnchor constraintEqualToAnchor: self.view.centerYAnchor].active = YES;
[trustbadgeViewController.view.leadingAnchor constraintEqualToAnchor: self.view.leadingAnchor constant: 16].active = YES;
[trustbadgeViewController.view.trailingAnchor constraintEqualToAnchor: self.view.trailingAnchor constant: -16].active = YES;
[trustbadgeViewController.view.heightAnchor constraintEqualToConstant: 75].active = YES;
}
@end
5. 展示商店等级小部件
要显示商店等级小部件,您只需在上面的代码示例中将 shopGrade
上下文和有效的频道 id 传递给 TrustbadgeView 即可。
Swift with SwiftUI
TrustbadgeView(
tsId: "X330A2E7D449E31E467D2F53A55DDD070",
channelId: "chl-b309535d-baa0-40df-a977-0b375379a3cc",
context: .shopGrade
)
.frame(height: 75)
Swift with UIKit
private lazy var trustbadgeView: UIHostingController = {
let trustbadge = TrustbadgeView(
tsId: "X330A2E7D449E31E467D2F53A55DDD070",
channelId: "chl-b309535d-baa0-40df-a977-0b375379a3cc",
context: .shopGrade
)
return UIHostingController(rootView: trustbadge)
}()
private func addTrustbadgeView() {
self.addChild(self.trustbadgeView)
self.view.addSubview(self.trustbadgeView.view)
/// Setup the constraints to update the SwiftUI view boundaries.
self.trustbadgeView.view.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
self.trustbadgeView.view.centerYAnchor.constraint(equalTo: self.view.centerYAnchor),
self.trustbadgeView.view.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 16),
self.trustbadgeView.view.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -16),
self.trustbadgeView.view.heightAnchor.constraint(equalToConstant: 75)
])
}
Objective-C with UIKit
UIViewController *trustbadgeViewController = [
TrustbadgeViewWrapper
createTrustbadgeViewWithTsId:@"X330A2E7D449E31E467D2F53A55DDD070"
channelId:@"chl-b309535d-baa0-40df-a977-0b375379a3cc"
context: TrustbadgeContextShopGrade
];
[self addChildViewController: trustbadgeViewController];
[self.view addSubview: trustbadgeViewController.view];
6. 展示买家保护小部件
要显示买家保护小部件,您只需在上面的代码示例中将 buyerProtection
上下文传递给 TrustbadgeView 即可。
Swift with SwiftUI
TrustbadgeView(
tsId: "X330A2E7D449E31E467D2F53A55DDD070",
channelId: "chl-b309535d-baa0-40df-a977-0b375379a3cc",
context: .buyerProtection
)
.frame(height: 75)
Swift with UIKit
private lazy var trustbadgeView: UIHostingController = {
let trustbadge = TrustbadgeView(
tsId: "X330A2E7D449E31E467D2F53A55DDD070",
channelId: "chl-b309535d-baa0-40df-a977-0b375379a3cc",
context: .buyerProtection
)
return UIHostingController(rootView: trustbadge)
}()
private func addTrustbadgeView() {
self.addChild(self.trustbadgeView)
self.view.addSubview(self.trustbadgeView.view)
/// Setup the constraints to update the SwiftUI view boundaries.
self.trustbadgeView.view.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
self.trustbadgeView.view.centerYAnchor.constraint(equalTo: self.view.centerYAnchor),
self.trustbadgeView.view.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 16),
self.trustbadgeView.view.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -16),
self.trustbadgeView.view.heightAnchor.constraint(equalToConstant: 75)
])
}
Objective-C with UIKit
UIViewController *trustbadgeViewController = [
TrustbadgeViewWrapper
createTrustbadgeViewWithTsId:@"X330A2E7D449E31E467D2F53A55DDD070"
channelId:@"chl-b309535d-baa0-40df-a977-0b375379a3cc"
context: TrustbadgeContextBuyerProtection
];
[self addChildViewController: trustbadgeViewController];
[self.view addSubview: trustbadgeViewController.view];
7. 展示产品等级小部件
以下是如何根据应用于App的iOS技术栈添加产品评级小部件的方法。
Swift with SwiftUI
TrustbadgeView(
tsId: "X330A2E7D449E31E467D2F53A55DDD070",
channelId: "chl-c0ad29ff-a086-4191-a663-82fed64f6f65",
productId: "31303033",
context: .productGrade
)
.frame(height: 75)
Swift with UIKit
private lazy var trustbadgeView: UIHostingController = {
let trustbadge = TrustbadgeView(
tsId: "X330A2E7D449E31E467D2F53A55DDD070",
channelId: "chl-c0ad29ff-a086-4191-a663-82fed64f6f65",
productId: "31303033",
context: .productGrade
)
return UIHostingController(rootView: trustbadge)
}()
private func addTrustbadgeView() {
self.addChild(self.trustbadgeView)
self.view.addSubview(self.trustbadgeView.view)
/// Setup the constraints to update the SwiftUI view boundaries.
self.trustbadgeView.view.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
self.trustbadgeView.view.centerYAnchor.constraint(equalTo: self.view.centerYAnchor),
self.trustbadgeView.view.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 16),
self.trustbadgeView.view.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -16),
self.trustbadgeView.view.heightAnchor.constraint(equalToConstant: 75)
])
}
Objective-C with UIKit
UIViewController *trustbadgeViewController = [
TrustbadgeViewWrapper
createTrustbadgeViewWithTsId:@"X330A2E7D449E31E467D2F53A55DDD070"
channelId:@"chl-c0ad29ff-a086-4191-a663-82fed64f6f65"
productId:@"31303033"
context: TrustbadgeContextProductGrade
];
[self addChildViewController: trustbadgeViewController];
[self.view addSubview: trustbadgeViewController.view];
8. 设置小部件水平对齐
您可以将小部件水平对齐设置为leading或trailing以符合您的设计规范。此功能在CocoadPod版本1.1.0+
和Swift Package版本1.1.0+
中可用。
TrustbadgeView具有一个可选的alignment
参数,接受.leading
或.trailing
值。如果您未传递alignment
参数,则小部件使用.leading
作为默认值。
以下是您如何使用对齐参数配置TrustbadgeView的方法:
TrustbadgeView(
tsId: "X330A2E7D449E31E467D2F53A55DDD070",
channelId: "chl-b309535d-baa0-40df-a977-0b375379a3cc",
context: .shopGrade,
alignment: .trailing
)
Trustbadge视图会考虑对齐值以扩展到正确的方向以显示小部件详细信息。如果对齐值设置为.trailing,则Trustbadge内容向右扩展,如对齐设置为.trailing,内容向左侧扩展。
以下是店铺评级小部件如何根据leading和trailing对齐显示其内容的方法:
注意:如果您同时在Android和iOS项目中整合Trustylib,请注意这两个平台上徽标的对齐方式略有不同。
9. 暗黑模式支持
Trustylib库支持暗黑模式色彩方案。它默认会根据iOS系统提供的光暗色彩方案进行适配。如果您的iOS应用中只需要支持亮或暗模式,库同样也会根据这些配置进行适配。暗黑模式支持从CocoadPod版本1.2.4+
和Swift Package版本1.2.4+
开始可用。
要强制库使用亮或暗色彩模式,以便它不根据iOS设备的色彩方案变化,请在您的应用Info.plist文件中添加以下键值对(如果尚未添加):
<key>UIUserInterfaceStyle</key>
<string>Light/Dark</string>
UIUserInterfaceStyle键是Apple推荐的坚持亮或暗模式的方式。因此,Trustylib会在应用的Info.plist文件中搜索此键,并根据其值设置小部件的色彩方案模式。如果没有找到此键,Trustylib会根据iOS系统提供的动态色彩方案进行适配。
10. 支持
如果您有任何建议或问题,请让我们知道。您也可以通过电子邮件联系Trusted Shop的移动工程团队:[email protected]