The introduction of the server, this article we are going to focus on, write our own iOS client program
Let's look at what we've done.
First download the xmppframework framework, download
Click Zip to download
Next, create a new project with Xcode
Drag the following files into the new project
Join the framework
and set
Here we are all set up, run for a try, see if there is a mistake
If there is no mistake, our xmppframework will join in the success.
We set up our pages such as:
Our 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 () {
- //Online users
- Nsmutablearray *onlineusers;
- }
- @end
- @implementation Kkviewcontroller
- @synthesize TView;
- -(void) Viewdidload
- {
- [Super Viewdidload];
- Self.tView.delegate = self;
- Self.tView.dataSource = self;
- Onlineusers = [Nsmutablearray array];
- 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
The code here believe that you have learned uitableview words should be very familiar with, if you do not know, just check the simple application of UITableView learn it
Next is the login page
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];
- //Save
- [Defaults synchronize];
- [Self dismissmodalviewcontrolleranimated:yes];
- }Else {
- Uialertview *alert = [[Uialertview alloc] initwithtitle:@"Prompt" message:@ "Please enter user name, password and server" Delegate:nil Cancelbutt ontitle:@ "OK" 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;
- }
Here is the chat page
The focus here is still 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;
- }
These are relatively simple, I believe we should all be able to read
Set these all up, we are going to focus on XMPP, afraid too long, take the next chapter.
[iphone advanced] XMPP-based iOS chat client program (iOS side One)