title: 实战教程

本文档将给出一些详尽的示例教程。

如需了解创建应用、读写数据等基础操作,请参考文档 快速入门

示例说明

本教程以弹幕为例,讲解如何通过 Wilddog Sync 实现多端实时互动。百余行代码即可完全实现该功能,可见 Sync 的简单与强大。

示例的最终的展示效果如下: title: 实战教程 - 图1

具体步骤

1. 安装 SDK

SDK 的安装方式有两种,你可以任选其一

  • 使用 CocoaPods

要将 Wilddog SDK 导入到你的工程中,推荐使用 CocoaPods,如果没用过 CocoaPods,请先访问 CocoaPods getting started

打开工程目录,新建一个 Podfile 文件

  1. $ cd your-project-directory
  2. $ pod init
  3. $ open -a Xcode Podfile # opens your Podfile in XCode

然后在 Podfile 文件中添加以下语句

  1. pod 'Wilddog'

最后安装 SDK

  1. $ pod install
  2. $ open your-project.xcworkspace


  • 手动集成
  1. 下载 Sync SDK 点此下载
  2. 下载 Core SDK 点此下载
  3. 把 WilddogSync.framework 和 WilddogCore.framework 拖到工程目录中。
  4. 选中 Copy items if needed 、Create Groups,点击 Finish。
  5. 点击工程文件 -> TARGETS -> General,在 Linked Frameworks and Libraries 选项中点击 ‘+’,将 JavaScriptCore.framework、 libsqlite3 加入列表中。

2. 初始化

  1. //初始化 WDGApp,建议自己创建一个应用,把 danmu 换成你自己的 SyncAppID
  2. WDGOptions *option = [[WDGOptions allosc] initWithSyncURL:@"https://danmu.wilddogio.com"];
  3. [WDGApp configureWithOptions:option];
  4. // 创建 WDGSyncReference 实例。
  5. _wilddog = [[WDGSync sync] referenceWithPath:@"message"];

3. 发送弹幕

使用写入数据的 API childByAutoId,它用来在当前节点下生成随机子节点,以保证 key 的不重复和有序。

  1. // 获取输入框的数据,将数据写入到云端 message 节点下,`childByAutoId` 将生成子节点的唯一 key
  2. - (BOOL)textFieldShouldReturn:(UITextField*)aTextField
  3. {
  4. [[self.wilddog childByAutoId] setValue:aTextField.text];
  5. [aTextField setText:@""];
  6. return NO;
  7. }

数据库中的数据结构如下图所示: title: 实战教程 - 图2

4. 设置监听

  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. //初始化 WDGApp
  5. WDGOptions *option = [[WDGOptions alloc] initWithSyncURL:kWilddogUrl];
  6. [WDGApp configureWithOptions:option];
  7. //获取一个指向根节点的 WDGSyncReference 实例
  8. _wilddog = [[WDGSync sync] reference];
  9. _snaps = [[NSMutableArray alloc] init];
  10. _originFrame = self.view.frame;
  11. // 设置监听
  12. // 绑定 WEventTypeChildAdded 事件,当 message 节点下有子节点新增时,就会触发回调,回调的 snapshot 对象包含了新增的数据
  13. [self.wilddog observeEventType:WDGDataEventTypeChildAdded withBlock:^(WDGDataSnapshot *snapshot) {
  14. [self sendLabel:snapshot];
  15. [self.snaps addObject:snapshot];
  16. }];
  17. //添加定时器
  18. [NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:@selector(timer) userInfo:nil repeats:YES];
  19. }

5. 取数据

  1. //随机取出弹幕数据
  2. - (void)timer
  3. {
  4. if (_snaps.count < 2) {
  5. return;
  6. }
  7. int index = arc4random()%(self.snaps.count-1);
  8. WDGDataSnapshot *snapshot = [self.snaps objectAtIndex:index];
  9. [self sendLabel:snapshot];
  10. }

6. 在屏幕显示

WDataSnapshot 是 Sync 的一个快照,包含着正在监听的节点下,从云端取下来的数据。

  1. //设置随机颜色,并显示在屏幕上
  2. - (UILabel *)sendLabel:(WDGDataSnapshot *)snapshot
  3. {
  4. float top = (arc4random()% (int)self.view.frame.size.height)-100;
  5. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width, top, 100, 30)];
  6. label.textColor = [UIColor colorWithRed:arc4random()%255/255.f green:arc4random()%255/255.f blue:arc4random()%255/255.f alpha:1];
  7. label.text = snapshot.value;
  8. [UIView animateWithDuration:7 animations:^{
  9. label.frame = CGRectMake(-label.frame.size.width, top, 100, 30);
  10. }completion:^(BOOL finished){
  11. [label removeFromSuperview];
  12. }];
  13. [self.view addSubview:label];
  14. return label;
  15. }

获取示例源码

点此获取完整的示例源码