介紹完了服務(wù)器,這篇我們就要介紹重點(diǎn)了,寫我們自己的IOS客戶端程序
先看一下我們完成的效果圖

首先下載xmppframework這個(gè)框架,下載

點(diǎn)Zip下載
接下來(lái),用Xcode新建一個(gè)工程
將以下這些文件拖入新建工程中
加入framework
并設(shè)置
到這里我們就全部設(shè)好了,跑一下試試,看有沒(méi)有錯(cuò)呢
如果沒(méi)有錯(cuò)的話,我們的xmppframework就加入成功了。
我們?cè)O(shè)置我們的頁(yè)面如下圖:
我們的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];
-
- }
-
- - (void )viewDidUnload
- {
- [self setTView:nil];
- [super viewDidUnload];
-
- }
-
- - (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
這里的代碼相信大家學(xué)過(guò)UITableView的話應(yīng)該很熟悉了,如果不知道的話,就查一下UITableView的簡(jiǎn)單應(yīng)用學(xué)習(xí)一下吧
接下來(lái)是登錄的頁(yè)面
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:@ "請(qǐng)輸入用戶名,密碼和服務(wù)器" 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;
- }
下面是聊天的頁(yè)面
這里著重的還是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;
-
- }
這些都比較簡(jiǎn)單,相信大家應(yīng)該都能看得懂
把這些都設(shè)置好以后,我們就要著重介紹XMPP了,怕太長(zhǎng)了,接下一章吧。