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



介绍完了服务器,这篇我们就要介绍重点了,写我们自己的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了,怕太长了,接下一章吧。

42
介绍完了服务器,这篇我们就要介绍重点了,写我们自己的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了,怕太长了,接下一章吧。

42
电动汽车数据集:2025年3K+记录 真实电动汽车数据:特斯拉、宝马、日产车型,含2025年电池规格和销售数据 关于数据集 电动汽车数据集 这个合成数据集包含许多品牌和年份的电动汽车和插电式车型的记录,捕捉技术规格、性能、定价、制造来源、销售和安全相关属性。每行代表由vehicle_ID标识的唯车辆列表。 关键特性 覆盖范围:全球制造商和车型组合,包括纯电动汽车和插电式混合动力汽车。 范围:电池化学成分、容量、续航里程、充电标准和速度、价格、产地、自主水平、排放、安全等级、销售和保修。 时间跨度:模型跨度多年(包括传统和即将推出的)。 数据质量说明: 某些行可能缺少某些字段(空白)。 几个分类字段包含不同的、特定于供应商的值(例如,Charging_Type、Battery_Type)。 各列中的单位混合在起;注意kWh、km、hr、USD、g/km和额定值。 列 列类型描述示例 Vehicle_ID整数每个车辆记录的唯标识符。1 制造商分类汽车品牌或OEM。特斯拉 型号类别特定型号名称/变体。型号Y 与记录关联的年份整数模型。2024 电池_类型分类使用的电池化学/技术。磷酸铁锂 Battery_Capacity_kWh浮充电池标称容量,单位为千瓦时。75.0 Range_km整数表示充满电后的行驶里程(公里)。505 充电类型主要充电接口或功能。CCS、NACS、CHAdeMO、DCFC、V2G、V2H、V2L Charge_Time_hr浮动充电的大致时间(小时),上下文因充电方法而异。7.5 价格_USD浮动参考车辆价格(美元).85000.00 颜色类别主要外观颜色或饰面。午夜黑 制造国_制造类别车辆制造/组装的国家。美国 Autonomous_Level浮点自动化能力级别(例如0-5),可能包括子级别的小
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值