ApesterKit
ApesterKit提供了一种轻量级的框架,可以在webView中加载Apester Unit。
需求
- iOS 11.0+
- Xcode 10.2+
集成
更新您的app的Info.plist文件以添加Apester AdMob应用ID。(联系Apester获取它)。
For testing please use this key:
<key>GADApplicationIdentifier</key>
<string>ca-app-pub-3940256099942544~1458002511</string>
Apester Strip View
这是一个包含媒体发布者Apester Units的频道轮播组件。这些组件是在Apester平台上构建的,可以配置和显示在任意位置。按照我们的指南分步进行设置。
实现
APEStripView
的变量
1 - 声明类型为 ## Swift
private var stripView: APEStripView!
## Objective C
@property (nonatomic, strong) APEStripView *stripView;
APEStripStyle
. 配置条带视图样式,例如形状、大小、内边距、阴影、标题头部等……
2 - 初始化条带样式配置 ## Swift
let header = APEStripHeader(text: "Title", size: 25.0, family: nil, weight: 400, color: .darkText)
let style = APEStripStyle(shape: .roundSquare, size: .medium,
padding: UIEdgeInsets(top: 5.0, left: 0, bottom: 0, right: 0),
shadow: false, textColor: nil, background: nil, header: header)
## Objective C
APEStripHeader *header = [[APEStripHeader alloc] initWithText:@"Title" size:25.0 family:nil weight:400 color:[UIColor purpleColor]];
APEStripStyle *style = [[APEStripStyle alloc] initWithShape:APEStripShapeRoundSquare
size:APEStripSizeMedium
padding:UIEdgeInsetsMake(10.0, 0, 0, 0)
shadow:NO
textColor:nil
background:[UIColor whiteColor]
header:header];
APEStripConfiguration
. 设置频道令牌、样式和包参数……
3 - 初始化条带配置 ## Swift
let configuration = try? APEStripConfiguration(channelToken: channelToken,
style: style,
bundle: Bundle.main)
## Objective C
NSError *error = nil;
APEStripConfiguration *config = [[APEStripConfiguration alloc] initWithChannelToken:channelToken
style:style
bundle:[NSBundle mainBundle]
error:&error];
4 - 使用参数值初始化条带视图实例。
## Swift
self.stripView = APEStripView(configuration: config)
## Objective C
self.stripView = [[APEStripView alloc] initWithConfiguration:config];
5 - 容器视图中频道条带
5.1 - 显示(使用容器视图控制器进行导航)。
## Swift
stripView?.display(in: self.containerView, containerViewController: self)
## Objective C
[self.stripView displayIn:self.containerView containerViewController:self];
5.2 - 隐藏频道条视图。
## Swift
self.stripView.hide()
## Objective C
[self.stripView hide];
6 - 实现APEStripViewDelegate以在成功、失败或高度更新时观察stripView的更新。
Apester 单元视图
适用于Apester媒体的单元或播放列表组件。这些单元是用Apester平台构建的。
APEUnitView
的变量。
1 - 声明类型为## Swift
private var unitView: APEUnitView!
## Objective C
@property (nonatomic, strong) APEUnitView *unitView;
APEUnitParams
。设置媒体ID或播放列表的频道令牌。
2 - 初始化单元参数## Swift
let unitParams = .unit(mediaId: mediaId)
// OR
let playlistParams = .playlist(tags: tags,
channelToken: channelToken,
context: isContext,
fallback: isFallback)
APEUnitConfiguration
. 设置单元参数和包
3 - 启动单元配置 ## Swift
let unitConfig = APEUnitConfiguration(unitParams: unitParams, bundle: Bundle.main)
// OR
let playlistConfig = APEUnitConfiguration(unitParams: playlistParams, bundle: Bundle.main)
## Objective C
APEUnitConfiguration *unitConfig = [[APEUnitConfiguration alloc] initWithMediaId:meidaId bundle: NSBundle.mainBundle];
// OR
APEUnitConfiguration *playlistConfig = [[APEUnitConfiguration alloc] initWithTags: mediaIds
channelToken: channelToken
context: isContext
fallback: isFallback
bundle: NSBundle.mainBundle
gdprString: gdprString
baseUrl: baseUrl];
可选设置
- 以全屏模式设置单元(仅用于故事引擎)
## Swift
configuration.setFullscreen(true)
## Objective C
[ configuration setFullscreen: true ];
4 - 使用参数值启动单元视图实例。
## Swift
self.unitView = APEUnitView(configuration: configuration)
## Objective C
self.unitView = [[APEUnitView alloc] initWithConfiguration:unitConfig];
// OR
self.unitView = [[APEUnitView alloc] initWithConfiguration:playlistConfig];
5 - 容器视图中的单元视图
5.1 - 显示(使用容器视图控制器进行导航)。
## Swift
unitView?.display(in: self.containerView, containerViewController: self)
## Objective C
[self.unitView displayIn:self.containerView containerViewController:self];
5.2 - 隐藏单元视图。
## Swift
self.unitView.hide()
## Objective C
[self.unitView hide];
5.3 - 重新加载单元视图。
## Swift
self.unitView.reload()
## Objective C
[self.unitView reload];
APEUnitViewDelegate
以观察 stripView 的成功、失败或高度更新。
6 - 实现 The APEViewService
一个提供预缓存 Apester 单元的通用服务,可以是 APEStripView
或 APEUnitView
。
APEStripView
strip configurations
预加载多个 strip 视图
1 - 使用 ## Swift
APEViewService.shared.preloadStripViews(with: configurations)
## Objective C
[APEViewService.shared preloadStripViewsWith: configurations];
channelTokens
的缓存中删除(如果存在)
2 - 卸载 strip 视图以使其能够从具有给定 ## Swift
APEViewService.shared.unloadStripViews(with: channelTokens)
## Objective C
[APEViewService.shared unloadStripViewsWith: channelTokens];
channelToken
的缓存条带视图(如果存在)。
3 - 获取给定 ## Swift
APEViewService.shared.stripView(for: channelToken)
## Objective C
[APEViewService.shared stripViewFor: channelToken];
APEUnitView
unit configurations
预加载数个单元视图。
1 - 使用 ## Swift
APEViewService.shared.preloadUnitViews(with: configurations)
## Objective C
[APEViewService.shared preloadUnitViewsWith: configurations];
unitIds
)。
2 - 卸载单元视图以便从缓存中删除(如果存在,使用给定的 ## Swift
APEViewService.shared.unloadUnitViews(with: unitIds)
## Objective C
[APEViewService.shared unloadUnitViewsWith: unitIds];
unitId
的缓存单元视图(如果存在)。
3 - 获取给定 ## Swift
APEViewService.shared.unitView(for: unitId)
## Objective C
[APEViewService.shared unitViewFor: unitId];
事件订阅
使用以下api设置事件监听器。
## Swift
apesterUnitView.subscribe(events: ["apester_interaction_loaded", "click_next"]) // example events
## Objective C
NSArray *events = @[@"apester_interaction_loaded"];
[_apesterUnitView subscribeWithEvents:(NSArray<NSString *> * _Nonnull)events];
APEStripViewDelegate
- didReciveEvent
来观察在事件触发时 unitView 的更新。
然后实现 以下是一些订阅的事件示例:(了解更多事件信息请联系 Apester 团队)
事件名称 | 含义 |
---|---|
click_next | 点击了“下一页” |
picked_answer | 选择了答案 |
unit_started | 首次互动 |
apester_interaction_loaded | 已加载单元 |
fullscreen_off | 单元全屏关闭 |
重新开始故事
## Swift
apesterUnitView.restart()
## Objective C
[_apesterUnitView restart];
处理全屏故事
最佳实践
- 预先加载单元,以便它们快速打开
- 当活动托管暂停/恢复时,向单元发送信号。
- 设置一个“关闭全屏事件”,以便在点击时活动会自行关闭。
示例
启动时
## Swift
configuration.setFullscreen(true)
APEViewService.shared.preloadStripViews(with: configurations)
在托管活动上
apesterUnitView.subscribe(events: ["fullscreen_off"]) // add more events as needed
apesterUnitView.delegate = self
在 ApeUnitViewDelegate 上
func unitView(_ unitView: APEUnitView, didReciveEvent name: String, message: String) {
if name == "fullscreen_off" {
finish();
}
}
在视图控制器恢复或暂停功能内部
if #available(iOS 13.0, *) {
NotificationCenter.default.addObserver(self, selector: #selector(willResignActive), name: UIScene.willDeactivateNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(willBackActive), name: UIScene.willEnterForegroundNotification, object: nil)
} else {
NotificationCenter.default.addObserver(self, selector: #selector(willResignActive), name: UIApplication.willResignActiveNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(willBackActive), name: UIApplication.willEnterForegroundNotification, object: nil)
}
@objc func willResignActive(_ notification: Notification) {
apesterUnitView.stop()
}
@objc func willBackActive(_ notification: Notification) {
apesterUnitView.resume()
}
override func viewDidDisappear(_ animated: Bool) {
apesterUnitView.stop()
}
override func viewDidAppear(_ animated: Bool) {
apesterUnitView.resume()
}
安装
Swift Package Manager
Swift Package Manager (https://swiftlang.cn/package-manager/) 自动化 Swift 代码的发行。要使用 ApesterKit 与 SPM,请将依赖关系添加到您的 Package.swift
文件中
let package = Package(
dependencies: [
.package(url: "https://github.com/Qmerce/ios-sdk.git", from: "2.0.0")
]
)
CocoaPods
CocoaPods是一个Cocoa项目的依赖管理器。您可以通过以下命令安装它:
$ gem install cocoapods
ApesterKit需要CocoaPods 1.1.0+来构建。
要使用CocoaPods将ApesterKit集成到您的Xcode项目中,请在您的Podfile
中指定:
platform :ios, '11.0'
use_frameworks!
pod 'ApesterKit'
然后,运行以下命令:
$ pod install
Carthage
Carthage是一个去中心化的依赖管理器,它自动化了将框架添加到您的Cocoa应用程序的过程。
您可以使用以下命令通过Homebrew安装Carthage:
$ brew update
$ brew install carthage
要使用Carthage将ApesterKit集成到您的Xcode项目中,请在您的Cartfile
中指定:
github 'Qmerce/ios-sdk'
然后,运行以下命令:
$ carthage update --platform iOS --use-submodules
手动
如果您不想使用上述依赖管理器中的任何一个,您可以手动将ApesterKit集成到您的项目中。
Git 子模块
- 打开终端,使用
cd
进入您的顶级项目目录,然后运行以下命令(如果您的项目尚未初始化为git仓库):
$ git init
- 通过运行以下命令将ApesterKit作为git子模块添加:
$ git submodule add https://github.com/Qmerce/ios-sdk.git
$ git submodule update --init --recursive
- 打开新的
ApesterKit
文件夹,并将ApesterKit.xcodeproj
拖放到您应用程序的Xcode项目的Project Navigator中。
它应该出现在您的应用程序蓝色项目图标下。它是在所有其他Xcode组之上还是之下并不重要。
- 在Project Navigator中选中
ApesterKit.xcodeproj
,并验证部署目标是否与您的应用程序目标匹配。 - 接下来,在Project Navigator中选择您的应用程序项目(蓝色项目图标),导航到目标配置窗口,并在侧边的“Targets”标题下选择应用程序目标。
- 在该窗口的顶部标签栏中,打开“General”面板。
- 在“嵌入式二进制”部分下点击
+
按钮。 - 您将看到两个不同的
ApesterKit.xcodeproj
文件夹,每个文件夹都包含两个不同版本的ApesterKit.framework
,它们嵌套在Products
文件夹中。
您选择哪个
Products
文件夹都无关紧要。
-
选择
ApesterKit.framework
。 -
就是这样!
ApesterKit.framework
会自动添加为目标依赖项、链接框架和嵌入式框架,在复制文件构建阶段这些都是您在模拟器和设备上构建所需的。
克隆项目并运行ApesterKitDemo应用程序
1 - clone it `git clone [email protected]:Qmerce/ios-sdk.git`.
2 - run `carthage update`.
3 - select ApesterKitDemo Target.
4 - run the App and enjoy.
许可证
ApesterKit根据MIT许可证发布。有关详细信息,请参阅LICENSE。