[iPhone進階] 基於XMPP的IOS聊天用戶端程式(IOS端一),iphonexmpp

來源:互聯網
上載者:User

[iPhone進階] 基於XMPP的IOS聊天用戶端程式(IOS端一),iphonexmpp

介紹完了伺服器,這篇我們就要介紹重點了,寫我們自己的IOS用戶端程式

先看一下我們完成的

首先下載xmppframework這個架構,下載

點ZIP下載

接下來,用Xcode建立一個工程

將以下這些檔案拖入建立工程中

加入framework

並設定

到這裡我們就全部設好了,跑一下試試,看有沒有錯呢

如果沒有錯的話,我們的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. }  

這些都比較簡單,相信大家應該都能看得懂

把這些都設定好以後,我們就要著重介紹XMPP了,怕太長了,接下一章吧。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.