您的位置:首页 > 移动开发 > IOS开发

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

2013-10-22 18:10 483 查看
先看一下我们完成的效果图







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



点ZIP下载

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

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





加入framework



并设置



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

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

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



我们的KKViewController.h

[java] view
plaincopy

#import <UIKit/UIKit.h>

@interface KKViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) IBOutlet UITableView *tView;

- (IBAction)Account:(id)sender;

@end

KKViewController.m

[java] view
plaincopy

#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

[java] view
plaincopy

- (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

[java] view
plaincopy

-(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了,怕太长了,接下一章吧
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: