RAD-iOS
RAD
Podcast Analytics
远程音频数据是一个用于在 iOS 应用中报告播客收听情况的框架。
如需更详细地查看 RAD 规范,请访问此页面。
如何集成 RAD 框架
Carthage
在您的 Cartfile 中添加 RAD 依赖项
github "npr/RAD-iOS"
然后按照 常规流程 将 RAD
及其依赖项 Reachability
框架添加到您的项目中。
CocoaPods
在您的Podfile中添加RAD
库,并在项目目录中使用命令行执行pod update
。
示例
target 'TargetName' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
# Pods for TargetName
pod "RAD"
end
项目整合
RAD框架由一个类Analytics
组成,该类提供从AVPlayer
收集数据并发送到分析服务器的支持。
将Swift模块导入使用的文件中
import RAD
在您的业务模型中创建一个Analytics
实例。
let analytics = Analytics()
或者如果您适合使用单例
static let RADAnalytics = Analytics()
Analytics对象有一个configuration
属性。通过使用此构造函数,将使用默认的Configuration
。要查看默认值,请参考Configuration
,在Analytics
类中检查。
可以通过构造函数向Analytics
实例传递自定义配置。
let configuration = Configuration(
submissionTimeInterval: TimeInterval.minutes(30), // how much time to wait until the stored events in the local storage are sent to analyics servers
batchSize: 10, // how many events are send per network request
expirationTimeInterval: DateComponents(day: 2), // how much time is an event valid
sessionExpirationTimeInterval: TimeInterval.hours(24), // how much time is a session identifier active
requestHeaderFields: [
"UserAgent": "iPhone/iOS",
"MyCustomKey": "CustomValue"] // header fields which will be added on each network request
)
let analytics = Analytics(configuration: configuration)
要开始记录数据,将AVPlayer
实例传递给Analytics
实例。在创建玩家时,必须不使用任何项目初始化,否则该项目将不会被分析。
let player = AVPlayer(playerItem: nil) // intialize the player
analytics.observePlayer(player) // start observing
player.replaceCurrentItem(with: playerItem) // change current item
要支持在应用处于后台时发送数据,需要启用Background Fetch并覆盖application(_:performFetchWithCompletionHandler:)。示例
func application(
_ application: UIApplication,
performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void
) {
analytics.performBackgroundFetch(completion: completionHandler)
}
可以随时停止或开始向分析服务器发送数据。默认情况下,创建Analytics
对象时向服务器发送数据。
您可以使用以下方法开始和停止数据发送
analytics.stopSendingData() // the next data send schedule is cancelled and data is not send to servers anymore
analytics.startSendingData() // schedule a point in time when to send data to servers based on configuration
测试整合
Analytics提供了一个调试接口(AnalyticsDebuggable
),可以用来检查是否创建了监听范围或执行了网络请求。
要注册监听范围,您的类应实现ListeningObserver
并使用调试器进行注册
analytics.debugger.addListeningObserver(yourObject)
要注册网络请求,您的类应实现NetworkObserver
并使用调试器进行注册
analytics.debugger.addNetworkObserver(yourObject)
演示应用程序使用了这个API并创建了两个可重用的单元测试,以检查RAD框架的集成是否成功。
示例
有一个示例项目可提供使用。在第一次运行之前,需要使用 Carthage 检出其依赖项。
carthage update --platform iOS --cache-builds --no-use-binaries