查看附加文档:iOS 文档
简介
将 SDK 集成到您的 iOS 应用中,开始为用户提供实时、位置针对性的任务和优惠。LiveShopper SDK 被设计得对用户无干扰,并智能管理跟踪频率以管理电池消耗。
身份验证
使用来自 LiveShopper 的可发布 API 密钥进行身份验证。
配置
以下条目需要添加到您的 Info.plist
文件中以启用跟踪:
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Your iOS 11 and higher background location usage description goes here. e.g., "This app uses your location in the background to recommend places nearby."</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Your iOS 10 and lower background location usage description goes here. e.g., "This app uses your location in the background to recommend places nearby."</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Your foreground location usage description goes here. e.g., "This app uses your location in the foreground to recommend places nearby."</string>permissions.
然后,在项目设置中,转到功能 > 后台模式并打开 Background fetch。为了提高后台的可靠性和响应能力,您还应该打开位置更新。
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
<string>location</string>
</array>
将 SDK 添加到项目
将 SDK 添加到您的项目最佳方式是通过 CocoaPods。
Cocoapods
对于 CocoaPods,请将以下内容添加到您的 Podfile
文件中
pod 'LiveShopperSDK', '~> 0.2.4'
然后运行 pod install
。
Carthage
要将 LiveShopper 作为 GitHub 源包括在 Carthage 中,请将以下内容添加到您的 Cartfile
文件中
github "liveshopper/liveshopper-sdk-ios" ~> 0.2.4
要将 LiveShopper 作为二进制源包括在 Carthage 中,请将以下内容添加到您的 Cartfile
文件中
binary "https://raw.githubusercontent.com/liveshopper/liveshopper-sdk-ios/master/LiveShopperSDK.json" ~> 0.2.4
手动添加
您还可以手动将 SDK 添加到项目中,尽管这样做不推荐。下载当前版本,解压缩包,并将 LiveShopperSDK.framework
拖到您的 Xcode 项目中。它将自动出现在项目设置中的 链接框架和库 部分中。
依赖
SDK 依赖于 Apple 的 CoreLocation 框架(用于位置服务)。在您的项目设置中,转到 通用 > 链接框架和库,然后添加 CoreLocation,如果您尚未添加的话。
SDK 目前支持 iOS 10 及以上版本。
将 SDK 集成到应用
初始化 SDK
导入 SDK
import LiveShopperSDK
在 AppDelegate 类中初始化 SDK,在主线程上,在调用任何其他 LiveShopper 方法之前,调用
LiveShopper.initialize(publishableKey: publishableKey)
识别用户
直到识别用户,SDK 将会自动通过 deviceId
识别用户。
当用户登录时,为了识别用户,调用
LiveShopper.setUserId(userId)
其中 userId
是用户的唯一标识符。我们不推荐使用名称、电子邮件地址等作为此值。
请求权限
SDK 尊重内置的 iOS 位置权限。在允许跟踪之前,如果用户之前未授权,用户必须授权应用程序的位置权限。这将在跟踪开始时由 SDK 处理。
跟踪
要开始跟踪用户的位置,调用
let options = LiveShopperTrackingOptions.RESPONSIVE
options.sync = .ALL
options.showBlueBar = true
LiveShopper.Tracking.start(options: options)
要停止跟踪用户的位置,调用
LiveShopper.Tracking.stop()
在客户端背景中监听事件或错误,创建一个实现 LSDelegate
的类,然后调用 setDelegate()
。
将您的 LSDelegate
设置在它将在后台初始化和执行的地点。
例如,使您的 AppDelegate
实现 LSDelegate
,而不是 ViewController
。
AppDelegate
将在后台初始化,而 ViewController
可能不会。
class AppDelegate: UIResponder, UIApplicationDelegate, LSDelegate {
func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
let publishableKey = "XXXXXX" // replace with your publishable API key
LiveShopper.initialize(publishableKey: publishableKey)
LiveShopper.setDelegate(self)
return true
}
func onClientLocationUpdated(location: CLLocation, stopped: Bool, source: LiveShopper.LSLocationSource) {
//
}
func onError(status: LiveShopper.LSStatus) {
//
}
func onEventsReceived(events: [LSEvent], user _: LSUser) {
//
}
func onLocationUpdated(location: CLLocation, user: LSUser) {
//
}
func onLog(message: String) {
//
}
}