[iPhone進階] 基於XMPP的IOS聊天用戶端程式(IOS端一),iphonexmpp
介紹完了伺服器,這篇我們就要介紹重點了,寫我們自己的IOS用戶端程式
先看一下我們完成的
首先下載xmppframework這個架構,下載
點ZIP下載
接下來,用Xcode建立一個工程
將以下這些檔案拖入建立工程中
加入framework
並設定
到這裡我們就全部設好了,跑一下試試,看有沒有錯呢
如果沒有錯的話,我們的xmppframework就加入成功了。
我們設定我們的頁面如:
我們的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的簡單應用學習一下吧
接下來是登入的頁面
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;
- }
下面是聊天的頁面
這裡著重的還是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了,怕太長了,接下一章吧。