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

Source: Internet
Author: User

[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
  1. # Import <UIKit/UIKit. h>
  2. @ Interface KKViewController: UIViewController <UITableViewDelegate, UITableViewDataSource>
  3. @ Property (strong, nonatomic) IBOutlet UITableView * tView;
  4. -(IBAction) Account :( id) sender;
  5. @ End

KKViewController. m

 

[Java]View plain copy


  1. # Import "KKViewController. h"
  2. @ Interface KKViewController (){
  3. // Online user
  4. NSMutableArray * onlineUsers;
  5. }
  6. @ End
  7. @ Implementation KKViewController
  8. @ Synthesize tView;
  9. -(Void) viewDidLoad
  10. {
  11. [Super viewDidLoad];
  12. Self. tView. delegate = self;
  13. Self. tView. dataSource = self;
  14. OnlineUsers = [NSMutableArray array];
  15. // Do any additional setup after loading the view, typically from a nib.
  16. }
  17. -(Void) viewDidUnload
  18. {
  19. [Self setTView: nil];
  20. [Super viewDidUnload];
  21. // Release any retained subviews of the main view.
  22. }
  23. -(BOOL) shouldAutorotateToInterfaceOrientation :( UIInterfaceOrientation) interfaceOrientation
  24. {
  25. Return (interfaceOrientation! = UIInterfaceOrientationPortraitUpsideDown );
  26. }
  27. -(IBAction) Account :( id) sender {
  28. }
  29. # Pragma mark UITableViewDataSource
  30. -(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {
  31. Return [onlineUsers count];
  32. }
  33. -(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {
  34. Static NSString * identifier = @ "userCell ";
  35. UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: identifier];
  36. If (cell = nil ){
  37. Cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: identifier];
  38. }
  39. Return cell;
  40. }
  41. -(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {
  42. Return 1;
  43. }
  44. # Pragma mark UITableViewDelegate
  45. -(Void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {
  46. }
  47. @ 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
  1. -(IBAction) LoginButton :( id) sender {
  2. If ([self validateWithUser: userTextField. text andPass: passTextField. text andServer: serverTextField. text]) {
  3. NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
  4. [Defaults setObject: self. userTextField. text forKey: USERID];
  5. [Defaults setObject: self. passTextField. text forKey: PASS];
  6. [Defaults setObject: self. serverTextField. text forKey: SERVER];
  7. // Save
  8. [Defaults synchronize];
  9. [Self dismissModalViewControllerAnimated: YES];
  10. } Else {
  11. UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "prompt" message: @ "Enter the user name, password and server" delegate: nil cancelButtonTitle: @ "OK" otherButtonTitles: nil, nil];
  12. [Alert show];
  13. }
  14. }
  15. -(IBAction) closeButton :( id) sender {
  16. [Self dismissModalViewControllerAnimated: YES];
  17. }
  18. -(BOOL) validateWithUser :( NSString *) userText andPass :( NSString *) passText andServer :( NSString *) serverText {
  19. If (userText. length> 0 & passText. length> 0 & serverText. length> 0 ){
  20. Return YES;
  21. }
  22. Return NO;
  23. }

Below is the chat page

Here we focus on UITableView.

KKChatController. m

 

[Java]View plain copy
  1. -(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {
  2. Return 1;
  3. }
  4. -(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {
  5. Return [messages count];
  6. }
  7. -(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {
  8. Static NSString * identifier = @ "msgCell ";
  9. UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: identifier];
  10. If (cell = nil ){
  11. Cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: identifier];
  12. }
  13. NSMutableDictionary * dict = [messages objectAtIndex: indexPath. row];
  14. Cell. textLabel. text = [dict objectForKey: @ "msg"];
  15. Cell. detailTextLabel. text = [dict objectForKey: @ "sender"];
  16. Cell. accessoryType = UITableViewCellAccessoryNone;
  17. Return cell;
  18. }

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.