测试已测试 | ✗ |
语言语言 | Obj-CObjective C |
许可协议 | 自定义 |
发布最后发布 | 2017年6月 |
由 Ovidiu D. Nitan 维护。
依赖关系 | |
Objective-LevelDB | ~> 2.1 |
JSONModel | ~> 1.1.0 |
AFNetworking | ~> 2.5 |
SIOSocket | ~> 0.2.0 |
CocoaLumberjack | ~> 2.3 |
Telepat iOS SDK 提供与 Telepat Sync API 交互所需绑定,同时还实现了 WebSocket 和 Apple Push 通知,以便从 Telepat 云实例接收更新。
克隆此仓库,然后将 Telepat 目录拖放到您的项目中。您还需要添加 Objective-LevelDB 2.1, JSONModel 1.1.0, AFNetworking 2.5.0, SIOSocket 0.2.0 和 CocoaLumberjack 2.0。
首先在您的 Info.plist
文件中添加以下键和值
<key>kTelepatAPIURL</key>
<string>http://YOUR_API_SERVER:3000/</string>
<key>kTelepatWebSocketsURL</key>
<string>ws://YOUR_API_SERVER:80</string>
(当然,不要忘记将 YOUR_API_SERVER 替换为实际值。端口号可能会因配置而异)。
然后,在您的 AppDelegate.m
文件中导入 Telepat
#import <Telepat/Telepat.h>
在 [UIApplicationDelegate application:didFinishLaunchingWithOptions:]
中以您的应用程序标识符和 API 密钥初始化 Telepat
[Telepat setApplicationId:@"your_application_id" apiKey:@"your_api_key"];
现在您可以使用 Telepat 注册您的设备。为了接收更新,您需要使用 WebSocket 或 Apple 的推送通知系统进行注册。您可能会想使用 APN,以便在应用程序处于后台时接收更新;如果不是这样,您可以使用配置更简单的 WebSocket。
为了使用 WebSocket 进行注册,您只需调用
[[Telepat client] registerDeviceForWebsocketsWithBlock:^(TelepatResponse *response) {
NSLog(@"Registered for websockets");
} shouldUpdateBackend:YES];
如果您想使用 Apple 推送通知进行注册,请首先启用您的应用程序使用 远程通知。当您准备好时,并在 [UIApplicationDelegate application:didRegisterForRemoteNotificationsWithDeviceToken:]
中收到 deviceToken
时,将其发送给 Telepat
const unsigned *tokenBytes = [deviceToken bytes];
NSString *hexToken = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x",
ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]),
ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]),
ntohl(tokenBytes[6]), ntohl(tokenBytes[7])];
[[Telepat client] registerDeviceWithToken:hexToken shouldUpdateBackend:YES withBlock:^(TelepatResponse *response) {
if ([response isError]) {
NSLog(@"Failed to register device: %@", response.error);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failed to register device" message:response.message delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
[alert show];
} else {
NSLog(@"Registered device %@ for push notifications with token: %@", [Telepat client].deviceId, hexToken);
}
}];
每次 Telepat 发来更新时,[UIApplicationDelegate application:didReceiveRemoteNotification:fetchCompletionHandler:]
都会被调用。在这个方法中,请调用
[[NSNotificationCenter defaultCenter] postNotificationName:TelepatRemoteNotificationReceived object:userInfo];
以便 Telepat SDK 可以通知这些更新。
为了接收更新,您需要订阅到一个上下文和模型名称。您可以通过调用
[[Telepat client] getAll:^(TelepatResponse *response) {
if (!response.isError) {
NSArray *contexts = [response getObjectOfType:[TelepatContext class]];
TelepatContext *myContext = [contexts firstObject];
}
}];
然后您进行订阅
TelepatChannel *myChannel = [[Telepat client] subscribe:myContext
modelName:@"model"
classType:[MyObject class]
withBlock:^(TelepatResponse *response) {
if (!response.isError) {
NSLog(@"Successfully subscribed!");
}
}];
现在每当您订阅的对象被修改时,Telepat 将接收更新并在后台处理更新后的对象,然后通过 NSNotificationCenter 发布通知。如果新增了新对象,将发布类型为 TelepatChannelObjectAdded
的通知,对于更新后的对象发布 TelepatChannelObjectUpdated
,每当删除一个对象时,都会发布 TelepatChannelObjectDeleted
。您可以在 notification.userInfo
字典的 kNotificationObject
键中找到被修改的对象。