Add and delete friends for XMPP and add friends for xmpp
In the current communication service, there are various standards, so there will be a lack of connectivity, and the emergence of XMPP (Extensible Message and presence Protocol) Protocol, the entire communication service protocol is interconnected. With this agreement, even the communication services provided by any organization or individual can communicate with other users who need timely communication services without any difficulty. For example, google talk launched by Google in 2005 is an XMPP-Based Instant Messaging Software. Next we will talk about how to simply add XMPP friends.
1. Open the header file in XMPPFramework. h.
2. Create a friend list interface and add a friend interface on the storyboard
Pay attention to the line between the two when creating and adding:
3. Define the query result storage and initialize it.
NSFetchedResultsController * fetch; AppDelegate * delegate = [UIApplication sharedApplication]. delegate; // obtain the context NSManagedObjectContext * context = [delegate. rosterStorage handler]; // obtain the NSFetchRequest * request = [NSFetchRequest handler: @ "handler"]; // Add the sorting field NSSortDescriptor * des = [NSSortDescriptor sortDescriptorWithKey: @ "sectionNum" ascending: YES]; [request setSortDescriptors: @ [des]; // initialize fetch = [[delealloc] initWithFetchRequest: request managedObjectContext: context sectionNameKeyPath: @ "sectionNum" cacheName: nil]; // sets the proxy [fetch setDelegate: self]; // starts querying [fetch interval mfetch: nil];
4. Create a proxy method to implement tableView
# Pragma mark-Table view data source-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {return fetch. sections. count;}-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {id <NSFetchedResultsSectionInfo> sections = fetch. sections [section]; return [sections numberOfObjects];}-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {UITableViewCell * cell = [tableView labels: @ "tabble"]; XMPPUserCoreDataStorageObject * user = [fetch objectAtIndexPath: indexPath]; cell. textLabel. text = user. displayName; return cell;} // return group data information. The current status is controlled based on the field type-(NSString *) tableView :( UITableView *) tableView titleForHeaderInSection :( NSInteger) section {id <NSFetchedResultsSectionInfo> info = [[fetch sections] objectAtIndex: section]; NSString * type = nil; if ([info. name isEqualToString: @ "0"]) {type = @ "online";} else {type = @ "offline";} return type ;}
5. Delete friends
-(BOOL) tableView :( UITableView *) tableView canEditRowAtIndexPath :( NSIndexPath *) indexPath {return YES;} // Delete the current friend through the roster object-(void) tableView :( UITableView *) tableView commitEditingStyle :( inline) editingStyle forRowAtIndexPath :( NSIndexPath *) indexPath {if (editingStyle = inline) {AppDelegate * delegate = [UIApplication sharedApplication]. delegate; XMPPUserCoreDataStorageObject * user = [fetch objectAtIndexPath: indexPath]; [delegate. roster removeUser: user. jid] ;}}
6. Add friends
-(IBAction) add {// obtain the friend name NSString * name = self. friends. text; // obtain the friend name AppDelegate * delegate = [UIApplication sharedApplication]. delegate; XMPPJID * jid = [XMPPJID jidWithString: name]; if ([delegate. rosterStorage userExistsWithJID: jid xmppStream: delegate. stream]) {NSLog (@ "friend already exists");} else {// Add friend [delegate. roster subscribePresenceToUser: jid] ;}}
For more information, clickView Source CodeRun the test in person.
For questions or technical exchanges, please join the official QQ group: (452379712)
Author: Jerry Education
Source: http://www.cnblogs.com/jerehedu/
The copyright of this article belongs to Yantai Jerry Education Technology Co., Ltd. and the blog Park. You are welcome to repost it. However, you must keep this statement without the author's consent and provide the original article connection on the article page, otherwise, you are entitled to pursue legal liability.