入门
要求
- iOS 8.0+
- Xcode 9.3 (Swift 4.1)
- 注册 predict.io API 密钥
CocoaPods 安装
通过将以下之一添加到您的 Podfile
中来完成 CocoaPods 安装
pod 'PredictIO', '~> 5.5.0'
Carthage 安装
将此添加到您的 Cartfile
中
github "predict-io/PredictIO-iOS" ~> 5.5.0
然后安装...
$ carthage update --platform iOS --cache-builds
与库链接二进制
使用以下 系统框架 链接您的应用
AdSupport.framework
CoreLocation.framework
CoreMotion.framework
Foundation.framework
SystemConfiguration.framework
libsqlite3.tbd
libz.tbd
运行上述 Carthage 命令后,您也可以将 SDK 及其依赖项添加到您的应用中
PredictIO.framework
RxSwift.framework
SwiftyJSON.framework
添加 '复制框架' 编译阶段
创建一个包含以下内容的 新运行脚本阶段
/usr/local/bin/carthage copy-frameworks
在 输入文件 下为以下各项添加条目
$(SRCROOT)/Carthage/Build/iOS/PredictIO.framework
$(SRCROOT)/Carthage/Build/iOS/RxSwift.framework
$(SRCROOT)/Carthage/Build/iOS/SwiftyJSON.framework
用法
配置您的项目
启用后台位置更新(仅限高功耗模式*)
注意 *如果您使用的是 高功耗模式,则需要进行此操作,否则可以跳过
后台高效使用位置,对电池使用的影响最小。要启用 后台位置更新,请打开项目设置,选择您的应用目标,选择 功能,启用 后台模式 并勾选 位置更新。
注意 您需要在应用中自行实现处理位置权限请求的方案。
Info.plist
App 使用描述到 iOS 需要您向用户说明为何要使用其位置信息,您必须在您的 Info.plist
中添加以下内容:
- 隐私 - 总是使用位置信息描述 (
NSLocationAlwaysUsageDescription
) - 对于 iOS 11 及以上版本 隐私 - 总是和在使用时使用位置信息的描述 (
NSLocationAlwaysAndWhenInUseUsageDescription
)
集成 SDK
注意:当您的应用启动时(无论是从后台还是前台启动),必须始终调用 SDK 的
start()
方法;这种操作的好地方是在您的AppDelegate
中的applicationDidFinishLaunching(_ application: UIApplication)
或application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool
方法中。
// Import the SDK in your AppDelegate
import PredictIO
let apiKey = "<YOUR_API_KEY>"
PredictIO.start(apiKey: "") { (status) in
switch status {
case .invalidKey:
// Your API key is invalid (incorrect or deactivated)
print("Invalid API Key")
case .killSwitch:
// Kill switch has been enabled to stop the SDK
print("Kill switch is active")
case .wifiDisabled:
// User has WiFi turned off significantly impacting location accuracy available.
// This may result in missed events!
// NOTE: SDK still launches after this error!
print("WiFi is turned off")
case .locationPermissionNotDetermined:
// Background location permission has not been requested yet.
// You need to call `requestAlwaysAuthorization()` on your
// CLLocationManager instance where it makes sense to ask for this
// permission in your app.
print("Location permission: not yet determined")
case .locationPermissionRestricted:
// This application is not authorized to use location services. Due
// to active restrictions on location services, the user cannot change
// this status, and may not have personally denied authorization
print("Location permission: restricted")
case .locationPermissionWhenInUse:
// User has only granted 'When In Use' location permission, and
// with that it is not possible to determine trips which are made.
print("Location permission: when in use")
case .locationPermissionDenied:
// User has flat out denied to give any location permission to
// this application.
print("Location permission: denied")
case .success:
// No error, SDK started with no problems
print("Successfully started PredictIO SDK!")
}
}
高功耗 & 低功耗
predict.io SDK 提供两种功耗等级,分别适用于不同的电池消耗和事件检测延迟要求。
注意:功耗等级设置不会在应用运行时生效,它不是在运行时切换的设置,需要重启应用才能生效。
高功耗
- 24 小时内平均电池消耗 5%
- 几分钟内即可检测到事件
- 交通方式检测(即将在未来的测试版中推出)
- 意图检测(即将在未来的测试版中推出)
PredictIO.start(apiKey: apiKey, powerLevel: .highPower) {
error in
// Handled as above
}
低功耗
- 24 小时内平均电池消耗小于 1%
- 事件最多延迟 30 分钟检测
- 不支持交通方式检测
- 意图检测(即将在未来的测试版中推出)
注意:未设置
powerLevel
参数时,默认使用低功耗模式。
PredictIO.start(apiKey: apiKey, powerLevel: .lowPower)
事件
predict.io SDK可以为检测到的事件提供回调,以便您集成到自己的应用程序功能中。
PredictIO.notify(on: .any) {
(event: PredictIOTripEvent) in
// Do something with event
}
PredictIO.notify(on: .departure) {
event in
// Do something when user has left a location
}
PredictIO.notify(on: .arrival) {
event in
// Do something when user has arrived at a location
}
PredictIOTripEvent
PredictIOTripEvent
是您将收到的事件,它描述了检测到的事件的属性,例如下列内容:
到达.
出发.
.enroute
(仅限高功率).still
(仅限高功率)
public class PredictIOTripEvent: CustomStringConvertible {
public let type: PredictIOTripEventType
public let coordinate: CLLocationCoordinate2D
public let timestamp: Date
}
Webhooks
您可以设置一个Webhooks URL,您也可以将predict.io SDK生成的事件副本发送到自己的服务器。要使用此功能,请在您的应用程序中包含以下代码。
// PredictIO.start(apiKey: "<YOUR API KEY>") {
// error in
// }...
PredictIO.setWebhookURL("https://api.yourapp.com/webhook")
Pre-flight Checklist
- 注册 predict.io API 密钥
- 在您的应用程序中请求“始终”地理位置权限;我们的SDK不会自动请求地理位置权限!
- 将集成SDK片段集成到您的应用程序中,以启动SDK
- 可选:集成webhooks
- 处理回调事件
- 提交应用程序时,请选择以下选项中的广告标识符部分
- 此应用程序使用广告标识符(IDFA)吗?是
- 在应用程序内发布广告?我们的SDK不发布广告(选择适合您应用程序的选项)
- 将应用程序安装归因于以前发布的广告 是
- 将应用程序内的动作归因于以前发布的广告 是
支持
访问我们的帮助中心,打开一个问题或向[email protected]发送电子邮件。