基於XMPP的IOS聊天用戶端程式(IOS端一)

來源:互聯網
上載者:User

標籤:

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

首先下載xmppframework這個架構,

點ZIP下載

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

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

加入framework

並設定

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

如果沒有錯的話,我們的xmppframework就加入成功了。


我們設定我們的頁面如:

 

我們的KKViewController.h

#import <UIKit/UIKit.h>  
 
@interface KKViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>  
 
@property (strong, nonatomic) IBOutlet UITableView *tView;  
 
- (IBAction)Account:(id)sender;  
@end

 

KKViewController.m

#import "KKViewController.h"  
 
@interface KKViewController (){  
      
    //線上使用者  
    NSMutableArray *onlineUsers;  
      
}  
 
@end  
 
@implementation KKViewController  
@synthesize tView;  
 
- (void)viewDidLoad  
{  
    [super viewDidLoad];  
    self.tView.delegate = self;  
    self.tView.dataSource = self;  
      
    onlineUsers = [NSMutableArray array];  
    // Do any additional setup after loading the view, typically from a nib.  
}  
 
- (void)viewDidUnload  
{  
    [self setTView:nil];  
    [super viewDidUnload];  
    // Release any retained subviews of the main view.  
}  
 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
{  
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);  
}  
 
- (IBAction)Account:(id)sender {  
}  
 
#pragma mark UITableViewDataSource  
 
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{  
      
    return [onlineUsers count];  
}  
 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  
      
    static NSString *identifier = @"userCell";  
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];  
    if (cell == nil) {  
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];  
    }  
      
      
    return cell;  
      
      
}  
 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{  
      
    return 1;  
}  
 
#pragma mark UITableViewDelegate  
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{  
      
      
}  
 
 
@end

 

這裡的代碼相信大家學過UITableView的話應該很熟悉了,如果不知道的話,就查一下UITableView的簡單應用學習一下吧

接下來是登入的頁面

 

 

 

KKLoginController.m

- (IBAction)LoginButton:(id)sender {  
      
    if ([self validateWithUser:userTextField.text andPass:passTextField.text andServer:serverTextField.text]) {  
          
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];  
        [defaults setObject:self.userTextField.text forKey:USERID];  
        [defaults setObject:self.passTextField.text forKey:PASS];  
        [defaults setObject:self.serverTextField.text forKey:SERVER];  
        //儲存  
        [defaults synchronize];  
          
        [self dismissModalViewControllerAnimated:YES];  
    }else {  
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"請輸入使用者名稱,密碼和伺服器" delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];  
        [alert show];  
    }  
 
}  
 
- (IBAction)closeButton:(id)sender {  
      
    [self dismissModalViewControllerAnimated:YES];  
}  
 
-(BOOL)validateWithUser:(NSString *)userText andPass:(NSString *)passText andServer:(NSString *)serverText{  
      
    if (userText.length > 0 && passText.length > 0 && serverText.length > 0) {  
        return YES;  
    }  
      
    return NO;  
}

 

下面是聊天的頁面

 

這裡著重的還是UITableView

KKChatController.m

 

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{  
      
    return 1;  
}  
 
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{  
    return [messages count];  
}  
 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  
      
    static NSString *identifier = @"msgCell";  
      
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];  
      
    if (cell == nil) {  
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];  
    }  
      
    NSMutableDictionary *dict = [messages objectAtIndex:indexPath.row];  
      
    cell.textLabel.text = [dict objectForKey:@"msg"];  
    cell.detailTextLabel.text = [dict objectForKey:@"sender"];  
    cell.accessoryType = UITableViewCellAccessoryNone;  
      
    return cell;  
      
}

 

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

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

 

基於XMPP的IOS聊天用戶端程式(IOS端一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.