Xmpp-iOS-get friends list, chat information list, chat
Typedef enum {// message sending type Enumeration
Text,
Image,
Audio
} MsgType;
# import XMPPData. H
# import XMPPFramework. H
@ interface XMPPData ()
@ property (strong, nonatomic) XMPPStream * XMPPStream;
@ the property (the assign, nonatomic) BOOL isRegister;
@ property (strong, nonatomic) user nsstrings * and * PWD, * the hostName, * domain;
@ the property (the assign, nonatomic) UInt16 port;
@ property (strong, nonatomic) XMPPRosterCoreDataStorage * rosterStorage; // roster storage
@ property (strong, nonatomic) XMPPRoster * rosterModule; // roster module
@ property (strong, nonatomic) XMPPMessageArchivingCoreDataStorage * msgStorage; // message storage
@ property (strong, nonatomic) XMPPMessageArchiving * msgModule; // message module
@ property (strong, nonatomic) NSFetchedResultsController * fetFriend; // Fetch for friends
@ property (strong, nonatomic) NSFetchedResultsController * fetMsgRecord; // Fetch of query message
@ the end
@ implementation XMPPData
// start connecting to the chat server
-(BOOL) connectWithUserName (NSString*) username andPwd (NSString*) PWD andHostName (NSString*) hostname andDomain (NSString*) domain andHostPort (UInt16) port andIsRegister (BOOL) isRegister
{
The self. The hostName = the hostName;
The self. The port = port;
Self. Domain = domain;
The self. The user = username;
Self. PWD = PWD;
Self. IsRegister = isRegister;
Self.xmppstream = [[xmppStream alloc] init];
// set up the agent
[the self xmppStream addDelegate: self delegateQueue: dispatch_get_global_queue (0, 0)];
// set the chat server address
The self. The xmppStream. The hostName = the hostName;
// set the chat server port to 5222 by default
The self. The xmppStream. HostPort = port;
// setting Jid is the user name
XMPPJID *jid = [XMPPJID jidWithUser:username domain:domain resource:@test];
The self. The xmppStream. MyJID = jid.
NSError star error = nil;
// verify the connection
[the self xmppStream connectWithTimeout: XMPPStreamTimeoutNone error: & error];
{if (error)
NSLog(@ connection failure: %@,error);
Return NO;
}
The else
{
// register all modules
[the self activeModules];
Return YES;
}
}
// activate related modules
- (void) activeModules
{
//1. Roster storage objects
Self. RosterStorage = [XMPPRosterCoreDataStorage sharedInstance];
//2. Roster module
Self. RosterModule = [[XMPPRoster alloc] initWithRosterStorage: self. RosterStorage];
//3. Activate this module
[self. RosterModule activate: self xmppStream];
/ / 4. Add the roster agencies
[the self rosterModule addDelegate: self delegateQueue: dispatch_get_main_queue ()];
//1. Message storage object
Self. MsgStorage = [XMPPMessageArchivingCoreDataStorage sharedInstance];
Self. MsgModule = [[XMPPMessageArchiving alloc] initWithMessageArchivingStorage: self. MsgStorage];
[self. MsgModule activate: self xmppStream];
[the self msgModule addDelegate: self delegateQueue: dispatch_get_main_queue ()];
}
/ / offline
- (void) logout
{
//1. User logout
NSLog(@ to log out the user);
XMPPPresence * presene = [XMPPPresence presenceWithType: @ unavailable];
// set the offline state
[_xmppStream sendElement: presene];
//2. Disconnect
[_xmppStream disconnect];
}
// add friends
- (BOOL) addFriend: (friendName nsstrings *)
{
XMPPJID * friendJid = [XMPPJID jidWithString:[NSString string stringWithFormat:@%@@%@,friendName,self.domain]];
[the self rosterModule subscribePresenceToUser: friendJid];
Return YES;
}
// remove friends
- (BOOL) deleteFriend: (friendName nsstrings *)
{
XMPPJID * friendJid = [XMPPJID jidWithString:[NSString string stringWithFormat:@%@@%@,friendName,self.domain]];
[the self rosterModule removeUser: friendJid];
Return YES;
}
// get the list of friends
- getFriends NSArray (*)
{
NSManagedObjectContext * context = self. RosterStorage. MainThreadManagedObjectContext;
NSFetchRequest * request = [[NSFetchRequest alloc] initWithEntityName: @ XMPPUserCoreDataStorageObject];
// filter this user's friends
NSString *userinfo = [NSString stringWithFormat:@%@@%@,self.user,self.domain];
NSLog (@ the userinfo = % @, the userinfo);
NSPredicate * predicate =
[NSPredicate predicateWithFormat: @streambarejidstr = %@,userinfo];
Request. The predicate = predicate;
/ / sorting
NSSortDescriptor * sort = [NSSortDescriptor sort key: @displayname ascending:YES];
Request. SortDescriptors = @ [sort];
Self. FetFriend = [[NSFetchedResultsController alloc] initWithFetchRequest: request managedObjectContext: context sectionNameKeyPath: nil cacheName: nil];
Self. FetFriend. Delegate = self;
NSError * error;
[the self fetFriend performFetch: & error];
/ / the returned array is XMPPUserCoreDataStorageObject * obj type
// the name is obj.displayName
NSLog (@ % lu, (unsigned long). Self fetFriend. FetchedObjects. Count);
Return the self. FetFriend. FetchedObjects;
}
// the database has changed
- (void) controller (NSFetchedResultsController *) controller didChangeObject: (id) anObject atIndexPath: (NSIndexPath *) indexPath forChangeType: (NSFetchedResultsChangeType) type newIndexPath: (NSIndexPath *) newIndexPath
{
NSManagedObject * obj = anObject;
If ([obj isKindOfClass: [XMPPMessageArchiving_Message_CoreDataObject class]])
{
NSLog(@chat message database changes);
}
The else
NSLog(@ database changes);
}
// chatting with a friend
-(BOOL) talkToFriend:(NSString*)friendsName andMsg:(NSString*) MSG andMsgType:(MsgType) msgT
{
XMPPJID *toFriend = [XMPPJID jidWithUser:friendsName domain:self.domain resource: @a]; //resource optional, not currently affected
XMPPMessage * message = [[XMPPMessage alloc] initWithType:@chat to:toFriend]; //chat type is a normal chat type
[message addBody: MSG];
[message addAttributeWithName: @ msgType intValue: msgT];
[the self xmppStream sendElement: message];
Return YES;
}
// get a chat with a friend
- (NSArray *) getRecords: (friendsName nsstrings *)
{
// all accounts and all people's chat records are in the same database so write the query criteria
NSManagedObjectContext * context = self. MsgStorage. MainThreadManagedObjectContext;
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName: @xmppmessagearchiving_message_coredataobject];
NSString *userinfo = [NSString stringWithFormat:@%@@%@,self.user,self.domain];
NSString *friendinfo = [NSString string stringWithFormat:@%@@%@,friendsName,self.domain];
NSPredicate * predicate =
[NSPredicate predicateWithFormat: @streambarejidstr = %@ and bareJidStr = %@,userinfo,friendinfo];
Request. The predicate = predicate;
NSSortDescriptor * sort = [NSSortDescriptor sort key :@timestamp ascending:YES];
Request. SortDescriptors = @ [sort];
Self. FetMsgRecord = [[NSFetchedResultsController alloc] initWithFetchRequest: request managedObjectContext: context sectionNameKeyPath: nil cacheName: nil];
Self. FetMsgRecord. Delegate = self;
NSError * error;
[the self fetMsgRecord performFetch: & error];
// returns the value type XMPPMessageArchiving_Message_CoreDataObject
Return the self. FetMsgRecord. FetchedObjects;
}
// proxy function for successful connection
- (void) sender xmppStreamDidConnect: (XMPPStream *)
{
NSLog(@ connection successful);
If (self. IsRegister) {
NSError * error = nil;
[sender registerWithPassword: self. PWD error: & error];
{if (error)
NSLog(@ registration failure 1, %@,error);
}
}
The else
{
NSError * error = nil;
[sender authenticateWithPassword: self. PWD error: & error];
{if (error)
NSLog(@ validation failure 1, %@,error);
}
}
}
// connection failed proxy function
- (void) xmppStreamDidDisconnect: (XMPPStream *) sender withError error: (NSError *)
{
NSLog(@ connection failure, %@,error);
}
// verify the successful proxy function
- (void) sender xmppStreamDidAuthenticate: (XMPPStream *)
{
NSLog(@ validation successful);
//4. Login online after success
[the self xmppStream sendElement: [XMPPPresence presence]]. // users online
}
// failed to verify the proxy function
- (void) xmppStream: (xmppStream *) sender didNotAuthenticate error: (DDXMLElement *)
{
NSLog(@ validation failure,%@,error);
}
// register the successful proxy function
- (void) sender xmppStreamDidRegister: (XMPPStream *)
{
NSLog(@ registration successful);
}
// failed to register the proxy function
- (void) xmppStream: (xmppStream *) sender didNotRegister error: (DDXMLElement *)
{
NSLog(@ registration failure,%@,error);
}
// received friend request proxy function
- (void) xmppRoster: (xmppRoster *) sender didReceivePresenceSubscriptionRequest presence: (XMPPPresence *)
{
NSString * presenceType = [presence type];
NSLog (@ presenceType = % @, presenceType);
XMPPJID * fromJid = present.from;
If ([presenceType isEqualToString:@subscribe]) {// is the subscription request directly through
[the self rosterModule acceptPresenceSubscriptionRequestFrom: fromJid andAddToRoster: YES];
}
}
- (void) xmppRoster: (xmppRoster *) sender didReceiveRosterItem item: (DDXMLElement *)
{
Nsstrings * subscription = [item attributeStringValueForName: @ subscription];
NSLog (@ % @, subscription);
If ([subscription isEqualToString: @ both]) {
NSLog(@ both parties become friends! ;
}
}