[IPhone advanced] XMPP-based IOS chat client (IOS client 1)

Source: Internet
Author: User

After introducing the server, we will focus on writing our own IOS client program.

Let's take a look at what we have done



First download the xmppframework framework.

Click zip to download

Next, use xcode to create a project

Drag the following files into the new project



Add framework


And set

Now we have all set up. Run it and see if there is any error.

If there is no error, our xmppframework will be successfully added.


We set our page as follows:


Our kkviewcontroller. h

#import <UIKit/UIKit.h>@interface KKViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>@property (strong, nonatomic) IBOutlet UITableView *tView;- (IBAction)Account:(id)sender;@end

Kkviewcontroller. m

# Import "kkviewcontroller. H "@ interface kkviewcontroller () {// nsmutablearray * onlineusers;} @ end @ implementation kkkviewcontroller @ 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: Ni L]; [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 identifier: identifier]; If (cell = nil) {Cell = [[uitableviewcell alloc] initwithstyle: uitableviewcellstyledefault reuseidentifier: identifier];} return cell ;} -(nsinteger) Configure :( uitableview *) tableview {return 1 ;}# Pragma mark uitableviewdelegate-(void) tableview :( uitableview *) tableview didselectrowatindexpath :( nsindexpath *) indexpath {} @ end

The code here should be familiar with uitableview. If you do not know it, check the simple application of uitableview to learn it.

Next is the login page


Kklogincontroller. m

-(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: @ "Enter the user name, password, and server" delegate: Nil cancelbuttontitle: @ "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 ;}

Below is the chat page


Here we focus on uitableview.

Kkchatcontroller. m

-(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 simple and I believe everyone can understand them.

After setting all these settings, we will focus on XMPP. It may be too long. Let's take the next chapter.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.