環信客服的問候語直接打開開關就能夠,可是歡迎語須要本身去掉環信的接口獲取。web
獲取歡迎語的思路是,經過環信的接口獲取歡迎語,而後拿到歡迎語封裝成一條消息,導入數據庫中,刷新tableView便可。數據庫
具體的步驟:(先貼代碼,具體看附件,先把附件的1,2步驟完成)json
1.經過post請求先獲取管理員的tokenapi
// 本身封裝好一個方法 獲取token 這裏採用的是AFNetworking
+ (void)HXGetTokenWithReturnValeuBlock: (HXGetTokenSuccessBlock) successblock WithFailureBlock: (FailureBlock) failureBlock
{
NSURL *url = [NSURL URLWithString:@"https://a1.easemob.com/下圖中的「組織」/下圖中的應用名稱/token"];
NSDictionary *body = @{@"grant_type":@"password",@"username": 「手機app登陸的用戶名」,@"password":@"環信密碼"};
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[[self DictionaryConversionJson:body] dataUsingEncoding:NSUTF8StringEncoding]];
AFHTTPSessionManager *manager = [self manager];
SKURLSessionTask *task = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (responseObject==nil) {
return;
}
id obj = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
NSLog(@"服務器返回信息%@",obj);
if (obj==nil) {
return;
}
NSString* token = [obj safeJsonObjForKey:@"access_token"];
successblock(token);
}];
[task resume];
}
服務器
+(NSString*)DictionaryConversionJson:(NSDictionary *)Dictionary
{
NSError *parseError = nil;
NSString *jsonStr = @"";
if ([Dictionary isKindOfClass:[NSDictionary class]]){
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:Dictionary options:NSJSONWritingPrettyPrinted error:&parseError];
jsonStr = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
}
return jsonStr;
}
app
2.獲取token後,再去獲取歡迎語webapp
+ (void)HXGetWelComeStrWithToken:(NSString *)token ReturnValeuBlock: (HXGetTokenSuccessBlock) successblock WithFailureBlock: (FailureBlock) failureBlock;
{
NSString *userName = USER_NAME;
NSString *url = [NSString stringWithFormat:@"http://kefu.easemob.com/v1/tenantapi/welcome?tenantId=「填寫tenantId」&orgName=「填寫組織」&appName=「填寫應用名稱」&userName=%@&token=%@",userName,token];post
AFHTTPSessionManager *manager = [self manager];
[manager GET:url parameters:@"" progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
id obj = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"服務器返回信息%@",obj);
successblock(obj);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
failureBlock(error.code);
}];
}
3.獲取到歡迎語後構造一條消息,咱們的聊天界面採用的是環信的EaseMessageViewControllerurl
因此在viewDidLoad裏面去構造這個消息spa
// 獲取管理員的token
[SKRequest HXGetTokenWithReturnValeuBlock:^(NSString *token) {
//獲取歡迎語
[SKRequest HXGetWelComeStrWithToken:token ReturnValeuBlock:^(NSString *token) {
EMTextMessageBody *body = [[EMTextMessageBody alloc] initWithText:token];
NSString *from = [[EMClient sharedClient] currentUsername];
//生成Message
EMMessage *message = [[EMMessage alloc] initWithConversationID:@"填寫IM服務號,注意所有小寫" from:from to:@"填寫IM服務號,注意所有小寫" body:body ext:nil];
message.chatType = EMChatTypeChat;// 設置爲單聊消息
message.status=EMMessageStatusSuccessed;
message.direction = EMMessageDirectionReceive;
EMConversation *conversation = [[EMClient sharedClient].chatManager getConversation:from type:EMConversationTypeChat createIfNotExist:YES];
[conversation insertMessage:message];
[self.tableView reloadData];
} WithFailureBlock:^(NSInteger errorCode) {
}];
} WithFailureBlock:^(NSInteger errorCode) {
}];
4.完成