功能描述
在发送消息时,可能会遇到消息尚未编辑完,就要切换至其它聊天窗口的情况。这些未编辑完的消息可通过
setConversationDraft
接口保存,以便于下次回到这个聊天界面时,通过 V2TIMConversation
对象的 draftText
字段,获取到尚未编辑完的内容,继续编辑。注意
1. 会话草稿仅支持文本内容。
2. 会话草稿仅在本地保存,不会存储到服务器,因此不能多端同步,程序卸载重装会失效。
效果展示
您可以使用本功能实现如下图所示的会话草稿效果,单击该会话进入聊天界面,草稿内容 draft 会自动填充到输入框:
接口说明
设置会话草稿
示例代码如下:
String conversationID = "conversationID";String draftText = "The draft text";V2TIMManager.getConversationManager().setConversationDraft(conversationID, draftText, new V2TIMCallback() {@Overridepublic void onSuccess() {Log.i("imsdk", "success");}@Overridepublic void onError(int code, String desc) {Log.i("imsdk", "failure, code:" + code + ", desc:" + desc);}});
NSString *conversationID = @"conversationID";NSString *draftText = "The draft text";[[V2TIMManager sharedInstance] setConversationDraft:conversationID draftText:draftText succ:^{NSLog(@"success");} fail:^(int code, NSString *desc) {NSLog(@"failure, code:%d, desc:%@", code, desc);}];
class Callback final : public V2TIMCallback {public:using SuccessCallback = std::function<void()>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;Callback() = default;~Callback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);}void OnSuccess() override {if (success_callback_) {success_callback_();}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;};V2TIMString conversationID = u8"conversationID";V2TIMString draftText = u8"The draft text";auto callback = new Callback;callback->SetCallback([=]() {// 设置会话草稿成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 设置会话草稿失败delete callback;});V2TIMManager::GetInstance()->GetConversationManager()->SetConversationDraft(conversationID, draftText,callback);