[iPhone高级] 基于XMPP的IOS聊天客户端程序(IOS端一)
2016-05-16 16:53
417 查看
介绍完了服务器,这篇我们就要介绍重点了,写我们自己的IOS客户端程序
先看一下我们完成的效果图
![](http://my.csdn.net/uploads/201207/13/1342164575_2627.png)
![](http://my.csdn.net/uploads/201207/13/1342164555_1035.png)
![](http://my.csdn.net/uploads/201207/13/1342164590_1834.png)
首先下载xmppframework这个框架,下载
![](http://my.csdn.net/uploads/201207/12/1342074297_5170.png)
点ZIP下载
接下来,用Xcode新建一个工程
将以下这些文件拖入新建工程中
![](http://my.csdn.net/uploads/201207/12/1342077119_4696.png)
![](http://my.csdn.net/uploads/201207/12/1342074830_1517.png)
加入framework
![](http://my.csdn.net/uploads/201207/12/1342077141_6924.png)
并设置
![](http://my.csdn.net/uploads/201207/12/1342077154_5471.png)
到这里我们就全部设好了,跑一下试试,看有没有错呢
如果没有错的话,我们的xmppframework就加入成功了。
我们设置我们的页面如下图:
![](http://my.csdn.net/uploads/201207/12/1342077701_5655.png)
我们的KKViewController.h
[java] view
plain copy
#import <UIKit/UIKit.h>
@interface KKViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UITableView *tView;
- (IBAction)Account:(id)sender;
@end
KKViewController.m
[java] view
plain copy
#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的简单应用学习一下吧
接下来是登录的页面
![](http://my.csdn.net/uploads/201207/13/1342164193_4863.png)
KKLoginController.m
[java] view
plain copy
- (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;
}
下面是聊天的页面
![](http://my.csdn.net/uploads/201207/13/1342164714_9508.png)
这里着重的还是UITableView
KKChatController.m
[java] view
plain copy
-(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了,怕太长了,接下一章吧。
先看一下我们完成的效果图
![](http://my.csdn.net/uploads/201207/13/1342164575_2627.png)
![](http://my.csdn.net/uploads/201207/13/1342164555_1035.png)
![](http://my.csdn.net/uploads/201207/13/1342164590_1834.png)
首先下载xmppframework这个框架,下载
![](http://my.csdn.net/uploads/201207/12/1342074297_5170.png)
点ZIP下载
接下来,用Xcode新建一个工程
将以下这些文件拖入新建工程中
![](http://my.csdn.net/uploads/201207/12/1342077119_4696.png)
![](http://my.csdn.net/uploads/201207/12/1342074830_1517.png)
加入framework
![](http://my.csdn.net/uploads/201207/12/1342077141_6924.png)
并设置
![](http://my.csdn.net/uploads/201207/12/1342077154_5471.png)
到这里我们就全部设好了,跑一下试试,看有没有错呢
如果没有错的话,我们的xmppframework就加入成功了。
我们设置我们的页面如下图:
![](http://my.csdn.net/uploads/201207/12/1342077701_5655.png)
我们的KKViewController.h
[java] view
plain copy
#import <UIKit/UIKit.h>
@interface KKViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UITableView *tView;
- (IBAction)Account:(id)sender;
@end
KKViewController.m
[java] view
plain copy
#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的简单应用学习一下吧
接下来是登录的页面
![](http://my.csdn.net/uploads/201207/13/1342164193_4863.png)
KKLoginController.m
[java] view
plain copy
- (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;
}
下面是聊天的页面
![](http://my.csdn.net/uploads/201207/13/1342164714_9508.png)
这里着重的还是UITableView
KKChatController.m
[java] view
plain copy
-(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了,怕太长了,接下一章吧。
相关文章推荐
- ios 音乐播放器
- 状态栏颜色设置
- iOS本地化 NSLocalizedString的使用
- iOS开发——发短信,邮件
- IOS 检查版本更新
- iOS开发系列--音频播放、录音、视频播放、拍照、视频录制(网址)
- ios学习笔记——GCD简介
- 使用xib文件创建CollectionView
- iOS 编译
- ios layout机制相关方法
- IOS coreAnimation
- iOS下按钮同时实现圆角与阴影效果
- iOS 构造json并提交到服务器
- iOS DrawRect简单使用
- IOS中通知中心(NSNotificationCenter)的使用总结
- iOS 警告 is missing from working copy
- iOS设备唯一标识探讨
- 模仿系统自带的集拍照,录制视频,以及iphone6S以下的手机拍摄livephoto照片(livephoto),滑动可切换
- ios图片的压缩处理
- [IOS笔记] - addChildViewController