[IPhone advanced] XMPP-based IOS chat client (IOS client 1), iPhone exmpp
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
[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 user
- 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
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
[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: @ "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
[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 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.