XMPP 配置开发

转载自:http://blog.csdn.net/kangkangz4/article/details/7740135java

[iPhone高级] 基于XMPP的IOS聊天客户端程序(IOS端一)

分类: iPhone高级    4043人阅读  评论 (9)  收藏  举报

介绍完了服务器,这篇咱们就要介绍重点了,写咱们本身的IOS客户端程序git

先看一下咱们完成的效果图github



首先下载xmppframework这个框架,下载服务器

点ZIP下载框架

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

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



加入framework学习


并设置atom

到这里咱们就所有设好了,跑一下试试,看有没有错呢spa

若是没有错的话,咱们的xmppframework就加入成功了。


咱们设置咱们的页面以下图:


咱们的KKViewController.h

 

[java]  view plain  copy
  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 plain  copy
  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 plain  copy
  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 plain  copy
  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. }  
这些都比较简单,相信你们应该都能看得懂
相关文章
相关标签/搜索