iOS-Senior21-鐜俊(浠g爜)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS-Senior21-鐜俊(浠g爜)相关的知识,希望对你有一定的参考价值。
鏍囩锛?/p>
//鍏堝紩鍏ョ被搴?/span>
#import "EMSDK.h"
1.鍦ˋppDelegate.m涓?/span>
//鍒濆鍖栦竴涓幆淇″璞?/span>
EMOptions *emOp =[EMOptionsoptionsWihtAppkey:@"lyf520zln#bjs160305"];
[[EMClient sharedClient] initializeSDKWithOptions : emOp];
//鑾峰彇鍒発eyWindow
[self.window makeKeyAndVisible];
//鑾峰彇main.storyboard
UIStoryboard *main = [UIStoryboard storyboardWithName : @"Main" bundle : nil];
UINavigationController *loginNC = [main instanttiateInitialViewController];
//閫氳繃chat.storyboard鐨勬牴瑙嗗浘鎺у埗鍣紝妯℃€佹帹鍑轰笅涓晫闈?/span>
[self.window.rootViewController presentViewController : loginNC animated : YES completion : nil];
2.鍦╲iewController.m閲?/span>
//澹版槑涓や釜鎻掑骇鍙橀噺
@property (weak, nonatomic) IBOutlet UITextField *userNameField;
@property (weak, nonatomic) IBOutlet UITextField *passwordField;
#pragma mark - 鐐瑰嚮鐧诲綍鎸夐挳
- (IBAction) loginButton : (id) sender {
if (self.userNameField.text.length == 0 || self.passwordField.text.length == 0){
NSLog(@"鐢ㄦ埛鍚嶅瘑鐮佷笉鑳戒负绌?);
return ;
}
//鐜俊鐧诲綍鐨勬柟娉?/span>
EMError *error = [[EMClient sharedClient] loginWithUserName : self.userNameField.text password : self.passwordField.text];
NSLog(@"error = %@",error);
if (!error){
NSLog(@"鐧诲綍鎴愬姛");
//杩斿洖浼氳瘽鍒楄〃
[self dismissViewControllerAnimated : YES completion : nil];
}
}
2.鍦≧egisterViewController.m閲?/span>
//澹版槑涓や釜鎻掑骇鍙橀噺
@property (weak, nonatomic) IBOutlet UITextField *userNameField;
@property (weak, nonatomic) IBOutlet UITextField *passwordField;
#pragma mark - 鐐瑰嚮娉ㄥ唽鎸夐挳
- (IBAction)didClickRegistButton : (UIButton *)sender {
if (self.userNameField.text.length == 0 || self.passwordField.text.length == 0) {
NSLog(@"鐢ㄦ埛鍚嶆垨瀵嗙爜涓嶈兘涓虹┖");
return ;
}
//鐜俊鐨勬敞鍐?/span>
EMError *error =[ [EMClient sharedClient] registerWithUserName: self.userNameField.text password : self.passwordField.text];
NSLog(@"error = %@",error);
if (!error){
NSLog(@"娉ㄥ唽鎴愬姛");
}
}
3.鍦╟hatTableViewController.m閲?/span>
//浼氳瘽灞炴€?/span>
@property (nonatomic,strong) EMConversation *conversation;
//娑堟伅鏁扮粍
@property (nonatomic,strong) NSMutableArray *messageArray;
#pragma mark - 瑙嗗浘鏄剧ず
- (void) viewDidAppear :(BOOL) animationed {
[super viewDidAppear : animated];
NSLog(@"%@",[EMClient sharedClient].currentUsername);
//鍒涘缓涓€涓細璇?/span>
self.conversation = [[EMClient sharedClient].chatManager getConversation : @"[EMClicent sharedClient].currentUsername" type : EMConversationTypeChat createIfNotExist : YES];
//娉ㄥ唽鎺ユ敹娑堟伅
[[EMClient sharedClient].chatManager addDelegate : self delegateQueue : nil];
//绉婚櫎娑堟伅鍥炶皟
[[EMClient sharedClient].chatManager removeDelegate : self];
}
- (void) viewDidLoad {
[super viewDidLoad];
self.messageArray = [NSMutableArray array];
//娉ㄥ唽鎺ユ敹娑堟伅
[[EMClient sharedClient].chatManager addDelegate : self delegateQueue : nil];
//娉ㄥ唽cell
[self.tableView registerClass : [UITableViewCell class] forCellReuseIdent ifier : @"cell"];
}
#pragma mark - 鎺ユ敹娑堟伅鐨勪唬鐞嗘柟娉?/span>
- (void) didReceiveMessages : (NSArray *)aMessages {
NSLog (@"鎺ユ敹鍒帮細 %@",aMessages);
//鍒锋柊tableview
[self.tableView reloadData];
}
#pragma mark - 鐐瑰嚮娉ㄩ攢鎸夐挳
- (void)didClickLogoutButton :(UIBarButtonItem *)sender {
EMError *error = [[EMClient sharedClient]logout : YES];
if(!error){
NSLog(@"閫€鍑烘垚鍔?);
}
//杩斿洖鐧诲綍椤甸潰
UIStoryboard *main = [UIStoryboard storyboardWithName : @"Main" bundle : nil];
UINavigationController *loginNC = [main instantiateInitialViewController];
[self pressentViewController :loginNC animated :YES completion : nil];
}
#pragma mark - 鐐瑰嚮鍙戦€佹寜閽?/span>
- (void) didClickSendMessageButton :(UIBarButtonItem *) sender {
//鍒涘缓娑堟伅鍐呭messageBody
EMTextMessageBody *body = [[EMTextMessageBody alloc] initWithText : @"鍚戝鏂规墧涓€鍧ㄥ睅"];
NSString *from = [[EMClient sharedClient] currentUsername];
//娑堟伅瀵硅薄
EMMessage *message = [[EMMessage alloc] initWithConversationID : from from : from to : from body :body ext : nil];
message.chatType = EMChatTypeChat;
#pragma mark - 鍙戦€佹秷鎭?/span>
[[EMClient sharedClient].chatManager asyncSendMessage:message progress:^(int progress) {
} completion:^(EMMessage *message, EMError *error) {
NSLog(@"鍙戦€佺殑娑堟伅%@",message);
if (!error) {
NSLog(@"鍙戦€佹垚鍔?);
message.to = @"";
//璁剧疆鍙戦€佹柟鍚?/span>
message.direction = EMMessageDirectionReceive;
[self.messageArray addObject:message];
//鍒锋柊tableview
[self.tableView reloadData];
}
}];
}
#pragma mark - 璁剧疆cell
鍒嗗尯锛?
鍒楁暟锛歴elf.messageArray.count
cell:鐢ㄧ郴缁熻嚜甯﹀垵濮嬪寲
{
UITableViewCell = *cell = [tableView dequeueReusableCellWithIdentifier :@"cell" forIndexPath : indexPath];
EMMessage *message = self.messageArray[indexPath.row];
if (message.direction == EMMessageDirectionSend){
cell.textLabel.text = @"";
cell.detailTextLabel.text = [self textFromMessage : message];
cell.detailTextLabel.textColor = [UIColor magentaColor];
}else {
cell.textLabel.text = [self textFromMessage : message];
cell.detailTextLabel.text = @"";
}
return cell;
}
#pragma mark - 鍙栧嚭娑堟伅涓殑鏂囨湰
- (NSString *)textFromMessage :(EMMessage *)message {
EMMessageBody *body = message.body;
EMTextMessageBody *textBody = (EMTextMessageBody *)body;
//杈撳嚭鏂囨湰鍐呭
NSLog(@"textBody = %@",textBody.text);
NSString *text = textBody.text;
return text;
}
以上是关于iOS-Senior21-鐜俊(浠g爜)的主要内容,如果未能解决你的问题,请参考以下文章
[瓒i椈] 浠g爜椤哄簭涔熶細褰卞搷 LuaJIT 鐨勬墽琛屾晥鐜?/a>