欢迎使用 jKool 的 iOS 流式 API。此 API 的目的是允许将数据流式传输到、从 jKool 存储库中查询和订阅。为了使用此 API,您需要一个 jKool 帐户和该帐户的访问令牌。如果您没有 jKool 帐户,您可以在 此处免费注册。参见本 Cocoa Pod 中的示例应用程序,其中包含查询、流式传输和订阅的全功能代码示例。此 API 基于安全的 jKool Restful 接口。有关数据模型、术语和概念的信息,请参阅 jKool 模型指南。
要运行示例项目,先克隆仓库,然后从 Example 目录中运行 pod install
。
jKool LLC
请参阅 LICENSE 文件。
通过在 PodFile 中放入以下内容来包含此 API
pod 'jkool-client-objc-api'
如果您希望此 CocoaPod 在 Swift 应用中工作,您只需要创建一个 Bridge.m 文件并包含以下头文件。创建 Bridge.m 时,请选择“创建桥接头”。此 ReadMe 中的代码示例同时包含 Swift 和 Objective-c。
根据您希望使用的 API 部分不同,将以下内容导入到您的应用程序中
用于流式传输
jKoolStreaming.h //Streaming Api
用于查询
jKoolQuerying.h //Querying Api
用于订阅
jkCallbackHandlerWebsocket.h //Subscription Api
jKool 对象
jkEvent.h //jKool Event
jkProperty.h //jKool Property
jkActivity.h //jKool Activity
jkSnapshot.h //jKool Snapshot
回调对象
jKoolCallbackHandler.h //Callback Interface
jKool 定位
jkLocation.h //Import if you wish to use jKool locationing to automatically detect and store device location on jKool activities and events.
请将以下变量定义在任何希望使用 API 的实现文件的顶部
Obj-c
jKoolWebsocketClient *jkWebsocketClient; // for subscriptions
jKoolStreaming *jkStreaming ; // for streaming
jKoolQuerying *jkQuerying; // for querying
jkLocation *location; // if using jKool locationing.
Swift
var jkWebsocketClient:jKoolWebsocketClient; // for subscriptions
var jkStreaming:jKoolStreaming; // for streaming
var jkQuerying:jKoolQuerying; // for querying
var location:jkLocation; // if using jKool locationing.
要流式传输,您需要初始化 jKool 流式传输接口和您的回调处理程序,如下所示
Obj-c
// Initialize streaming and specify callback handler.
<your-callback-handler> *cbStream = [[<your-callback-handler> alloc] initWithViewController:self];
jkStreaming = [[jKoolStreaming alloc] init];
[jkStreaming setToken:@“your-token”];
[jkStreaming initializeStream:cbStream];
Swift
let cbStream : jkCallback = jkCallback.init(viewController: self);
let jkStreaming : jKoolStreaming = jKoolStreaming ();
jkStreaming.token = "your-token"
jkStreaming.initializeStream(cbStream);
要查询,您需要初始化 jKool 查询接口和您的回调处理程序,如下所示
Obj-c
// Initialize Querying and specify callback handler
<your-callback-handler> *cbQuery = [[<your-callback-handler> alloc] initWithViewController:self];
jkQuerying = [[jKoolQuerying alloc] init];
[jkQuerying setToken:@“your-token”];
[jkQuerying initializeQuery:cbQuery];
Swift
// Initialize Querying and specify callback handler
let cbQuery : <your-callback-handler> = <your-callback-handler>.init(viewController: self);
let jkQuerying : jKoolQuerying = jKoolQuerying ();
jkQuerying.token = "your-token"
jkQuerying.initializeQuery(cbQuery);
要订阅,您需要按照以下方式初始化jKool订阅界面和您的回调处理器:
Obj-c
// Initialize Subscription
<your-callback-handler> *cbWebsocket = [[<your-callback-handler> alloc] initWithViewController:self];
jkWebsocketClient = [[jKoolWebsocketClient alloc] init];
Swift
// Initialize Subscription
let cbWebsocket : <your-callback-handler> = <your-callback-handler>.init(viewController: self);
let jkWebsocketClient : jKoolWebsocketClient.init();
Obj-c
To initialize jKool Locationing, do the following:
// Kick-off locationing
location = [[jkLocation alloc] init];
[location kickOffLocationing];
Swift
To initialize jKool Locationing, do the following:
// Kick-off locationing
let location : jkLocation.init();
location.kickOffLocationing();
填充您的jKool对象。这些对象包括:
按照以下方式流式传输每个对象
Obj-c
[jkStreaming stream:activity forUrl:@"activity"] ;
[jkStreaming stream:event forUrl:@“event”] ;
Swift
jkStreaming.stream(activity,forUrl:"activity");
jkStreaming.stream(event,forUrl:“event”);
(请注意,属性和快照是活动的一部分,也是事件的一部分)
Obj-c
// Query
NSString *query = @"get events";
[jkQuerying query:query withMaxRows:50];
Swift
// Query
jkQuerying.query("get events", withMaxRows: 50);
(请注意,查询字符串可以包含任何JKQL语法。请参阅JKQL查询语言)
Obj-c
[jkWebsocketClient subscribe:@"subscribe to events" withMaxRows:10 withToken:@“your-token” withSubId:@“your-subscription-id” forHandler:cbWebsocket];
Swift
jkWebsocketClient.subscribe("subscribe to events":withMaxRows:10.withToken:“your-token”,withSubId:“your-subscription-id”, forHandler:cbWebsocket);
(请注意,订阅中可以包含任何JKQL语法。)
回调处理器可以是单独的对象,也可以是执行流式传输、查询、订阅的ViewController。如果您使用的是同一个ViewController,只需指定'自己'作为处理器。需要单独的回调处理器的情况是您正在处理多个数据流。在Cocoa Pod中的示例应用程序包含单独的回调处理器,以及一个被注释掉的示例调用调用ViewController中的回调处理器方法(并使用'self')。
要关闭连接,请执行以下操作:
Obj-c
[jkWebsocketClient unsubscribe];
[jkStreaming stopStreaming];
[jkQuerying stopQuerying];
Swift
jkWebsocketClient.unsubscribe();
jkStreaming.stopStreaming();
jkQuerying.stopQuerying();
如上所述,请参阅此Pod中的示例应用程序。它包含了一个完整的工作应用程序,其中包含所有上述提到的代码。只需替换“你的-Token”中的“你的-Token”,以查看应用程序的工作方式。我们建议您执行以下操作:
如果您有任何问题或疑虑,请通过电子邮件联系我们 [email protected]。我们将尽快回复您。