基于XMPP的IOS聊天客户端程序(IOS端一)

来源:互联网 发布:计算机就业方向 知乎 编辑:程序博客网 时间:2024/06/06 00:53

本文转自:http://blog.csdn.net/kangx6/article/details/7740135


介绍完了服务器,这篇我们就要介绍重点了,写我们自己的IOS客户端程序

先看一下我们完成的效果图



首先下载xmppframework这个框架,下载


点ZIP下载

接下来,用Xcode新建一个工程

将以下这些文件拖入新建工程中



加入framework


并设置

到这里我们就全部设好了,跑一下试试,看有没有错呢

如果没有错的话,我们的xmppframework就加入成功了。


我们设置我们的页面如下图:


我们的KKViewController.h

[java] view plaincopy
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface KKViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>  
  4.   
  5. @property (strong, nonatomic) IBOutlet UITableView *tView;  
  6.   
  7. - (IBAction)Account:(id)sender;  
  8. @end  

KKViewController.m

[java] view plaincopy
  1. #import "KKViewController.h"  
  2.   
  3. @interface KKViewController (){  
  4.       
  5.     //在线用户  
  6.     NSMutableArray *onlineUsers;  
  7.       
  8. }  
  9.   
  10. @end  
  11.   
  12. @implementation KKViewController  
  13. @synthesize tView;  
  14.   
  15. - (void)viewDidLoad  
  16. {  
  17.     [super viewDidLoad];  
  18.     self.tView.delegate = self;  
  19.     self.tView.dataSource = self;  
  20.       
  21.     onlineUsers = [NSMutableArray array];  
  22.     // Do any additional setup after loading the view, typically from a nib.  
  23. }  
  24.   
  25. - (void)viewDidUnload  
  26. {  
  27.     [self setTView:nil];  
  28.     [super viewDidUnload];  
  29.     // Release any retained subviews of the main view.  
  30. }  
  31.   
  32. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  33. {  
  34.     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);  
  35. }  
  36.   
  37. - (IBAction)Account:(id)sender {  
  38. }  
  39.   
  40. #pragma mark UITableViewDataSource  
  41.   
  42. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{  
  43.       
  44.     return [onlineUsers count];  
  45. }  
  46.   
  47. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  
  48.       
  49.     static NSString *identifier = @"userCell";  
  50.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];  
  51.     if (cell == nil) {  
  52.         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];  
  53.     }  
  54.       
  55.       
  56.     return cell;  
  57.       
  58.       
  59. }  
  60.   
  61. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{  
  62.       
  63.     return 1;  
  64. }  
  65.   
  66. #pragma mark UITableViewDelegate  
  67. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{  
  68.       
  69.       
  70. }  
  71.   
  72.   
  73. @end  
这里的代码相信大家学过UITableView的话应该很熟悉了,如果不知道的话,就查一下UITableView的简单应用学习一下吧

接下来是登录的页面


KKLoginController.m

[java] view plaincopy
  1. - (IBAction)LoginButton:(id)sender {  
  2.       
  3.     if ([self validateWithUser:userTextField.text andPass:passTextField.text andServer:serverTextField.text]) {  
  4.           
  5.         NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];  
  6.         [defaults setObject:self.userTextField.text forKey:USERID];  
  7.         [defaults setObject:self.passTextField.text forKey:PASS];  
  8.         [defaults setObject:self.serverTextField.text forKey:SERVER];  
  9.         //保存  
  10.         [defaults synchronize];  
  11.           
  12.         [self dismissModalViewControllerAnimated:YES];  
  13.     }else {  
  14.         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入用户名,密码和服务器" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];  
  15.         [alert show];  
  16.     }  
  17.   
  18. }  
  19.   
  20. - (IBAction)closeButton:(id)sender {  
  21.       
  22.     [self dismissModalViewControllerAnimated:YES];  
  23. }  
  24.   
  25. -(BOOL)validateWithUser:(NSString *)userText andPass:(NSString *)passText andServer:(NSString *)serverText{  
  26.       
  27.     if (userText.length > 0 && passText.length > 0 && serverText.length > 0) {  
  28.         return YES;  
  29.     }  
  30.       
  31.     return NO;  
  32. }  
下面是聊天的页面


这里着重的还是UITableView

KKChatController.m

[java] view plaincopy
  1. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{  
  2.       
  3.     return 1;  
  4. }  
  5.   
  6. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{  
  7.     return [messages count];  
  8. }  
  9.   
  10. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  
  11.       
  12.     static NSString *identifier = @"msgCell";  
  13.       
  14.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];  
  15.       
  16.     if (cell == nil) {  
  17.         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];  
  18.     }  
  19.       
  20.     NSMutableDictionary *dict = [messages objectAtIndex:indexPath.row];  
  21.       
  22.     cell.textLabel.text = [dict objectForKey:@"msg"];  
  23.     cell.detailTextLabel.text = [dict objectForKey:@"sender"];  
  24.     cell.accessoryType = UITableViewCellAccessoryNone;  
  25.       
  26.     return cell;  
  27.       
  28. }  
这些都比较简单,相信大家应该都能看得懂

把这些都设置好以后,我们就要着重介绍XMPP了,怕太长了,接下一章吧。


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 左侧脑室增宽该怎么办 腿上的血管堵塞怎么办 做b超看不清骶尾怎么办 孕中期羊水过少怎么办 心脏办膜关闭不全怎么办 9个月胎儿脑积水怎么办 怀孕三个月胎盘低置怎么办 怀孕第一个月打针了怎么办 唐氏筛查神经管缺陷高风险怎么办 门诊处方笺丢了怎么办 孕中期睡觉手麻怎么办 怀孕2个月了没胎心胎芽怎么办 怀孕腿疼的厉害怎么办 孕妇老是失眠多梦怎么办 孕妇会失眠多梦怎么办 怀孕5个月睡不着怎么办 6个月孕妇失眠怎么办 彩超脉络丛囊肿怎么办 双侧脉络丛囊肿怎么办 唐筛神经管缺陷高风险怎么办 雌激素低怎么办吃什么东西补 我怀了狗的孩子怎么办 结婚2年不要孩子怎么办 备孕一直没怀孕怎么办 刚生的婴儿打嗝怎么办 小孩40天黄疸高怎么办 婴儿身高长得慢怎么办 四个月的宝宝哭怎么办 孕39周羊水偏多怎么办 孕39周羊水浑浊怎么办 孕晚期羊水过少怎么办 怀孕脐带绕颈一周怎么办 nt检查宝宝趴着怎么办 四维胎儿有问题怎么办 怀孕70天没有胎心怎么办 怀孕20天不想要怎么办 换轮胎胎压监测怎么办 怀孕了吐的厉害该怎么办 怀孕吐完嗓子疼怎么办 怀孕16周不想要怎么办 怀孕四个月胎盘低置怎么办