5.5 生命周期处理

在使用 UI 集成的 SDK 中, 【必须】AppDelegate 中添加下面代码:

- (void)applicationDidEnterBackground:(UIApplication *)application {
  //退出到后台时,通知 SDK 用户离线
  [[V5ClientAgent shareClient] onApplicationDidEnterBackground];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
  //移动到前台时,通知 SDK 用户上线并连接
  [[V5ClientAgent shareClient] onApplicationWillEnterForeground];
}

此外,不使用客服功能时需要关闭会话服务以节省资源并避免客户离线的状态未更新到座席(已配置离线推送情况下还会影响消息推送)。

下面列举几种情况下关闭会话的示例:

使用push推出页面的,在开启会话界面的前一个界面的 viewDidAppear:方法中手动调用关闭客服的方法:

- (void)viewDidAppear:(BOOL)animated { // 前一 viewController 中
  // 不使用客服功能时(在会话 VC 关闭后)退出消息客户端
  if ([V5ClientAgent shareClient].isConnected) {
    [[V5ClientAgent shareClient] stopClient];
  }
}

使用present弹出页面的,在关闭页面的selector中调用

// done按钮点击关闭页面的selector
- (void)closeChat:(id)sender {
    [navVC dismissViewControllerAnimated:YES completion:nil];
    if ([V5ClientAgent shareClient].isConnected) {
      [[V5ClientAgent shareClient] stopClient];
    }
}

UIPopoverPresentationController方式打开的,则需要通过实现UIPopoverPresentationControllerDelegatepopoverPresentationControllerDidDismissPopover:来调用[[V5ClientAgent shareClient] stopClient];:

- (void)popoverPresentationControllerDidDismissPopover:(UIPopoverPresentationController *)popoverPresentationController {
    // 不使用客服功能时(在会话 VC 关闭后)退出消息客户端 
    if ([V5ClientAgent shareClient].isConnected) {
        [[V5ClientAgent shareClient] stopClient];
    }
}

此外,使用UIPopoverController或者其他方式开启的也是同理,需要手动关闭会话。

results matching ""

    No results matching ""

    results matching ""

      No results matching ""