Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // AppDelegate.m
- // SoketDemo
- //
- // Created by Sweeta Ahuja on 15/04/16.
- // Copyright (c) 2016 Sweeta Ahuja. All rights reserved.
- //
- #import "AppDelegate.h"
- #import "NSString+Hashes.h"
- #import "GCDAsyncSocket.h"
- #import "XMPP.h"
- #import "XMPPReconnect.h"
- #import "XMPPCapabilitiesCoreDataStorage.h"
- #import "XMPPRosterCoreDataStorage.h"
- #import "XMPPRoomMemoryStorage.h"
- #import "XMPPvCardAvatarModule.h"
- #import "XMPPvCardCoreDataStorage.h"
- #import "XMPPMessageDeliveryReceipts.h"
- #import "XMPPMessage+XEP_0184.h"
- #import "XMPPMessage+XEP_0085.h"
- #import "NSXMLElement+XEP_0203.h"
- #import "DDLog.h"
- #import "DDTTYLogger.h"
- #import "SharedManager.h"
- #import <CFNetwork/CFNetwork.h>
- @import GoogleMaps;
- @interface AppDelegate ()
- - (void)setupStream;
- - (void)teardownStream;
- - (void)goOnline;
- - (void)goOffline;
- @end
- @implementation AppDelegate
- {
- id _services;
- }
- @synthesize xmppStream;
- @synthesize xmppReconnect;
- @synthesize xmppRoster;
- @synthesize xmppRosterStorage;
- @synthesize xmppvCardTempModule;
- @synthesize xmppvCardAvatarModule;
- @synthesize xmppCapabilities;
- @synthesize xmppCapabilitiesStorage;
- @synthesize xmppLastActivity;
- @synthesize xmppMUC;
- @synthesize hostReach;
- #pragma mark Private
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- -(void)sendGroupMessage:(Message *)objMessage
- to:(NSString *)receiverJId
- {
- XMPPJID* recipient = [XMPPJID jidWithString:receiverJId];
- XMPPMessage* message = [[XMPPMessage alloc] initWithType:@"groupchat" to:recipient];
- [message addAttributeWithName:@"type" stringValue:@"groupchat"];
- NSMutableDictionary *msgDic = [[NSMutableDictionary alloc] init];
- NSMutableDictionary *senderProperty = [[NSMutableDictionary alloc] init];
- [senderProperty setObject:[NSString stringWithFormat:@"%@ %@",ShareObj.loginUser.firstName,ShareObj.loginUser.lastName] forKey:kuserName];
- [senderProperty setObject:[NSString stringWithFormat:@"%ld",(long)ShareObj.loginUser.userId] forKey:kUserId];
- [senderProperty setObject:ShareObj.loginUser.mobileNumber forKey:kMobileNumber];
- [msgDic setObject:senderProperty forKey:@"sender"];
- [msgDic setObject:[objMessage getDic] forKey:@"message"];
- NSError *err = nil;
- NSData *jsonData = [NSJSONSerialization dataWithJSONObject:msgDic options:NSJSONWritingPrettyPrinted error:&err];
- NSString *str = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
- [message addBody:str];
- // [message addAttributeWithName:@"id" stringValue:objMessage.messageId];
- //
- // // User this code to send markable seen message...
- // [message addMarkableChatMarker];
- [[APP_DELEGATE xmppStream] sendElement:message];
- // XMPPJID* recipient = [XMPPJID jidWithString:receiverJId];
- //
- // XMPPRoomMemoryStorage *roomMemoryStorage = [[XMPPRoomMemoryStorage alloc] init];
- // XMPPRoom *xmppRoom = [[XMPPRoom alloc]
- // initWithRoomStorage:roomMemoryStorage
- // jid:recipient
- // dispatchQueue:dispatch_get_main_queue()];
- //
- // NSMutableDictionary *msgDic = [[NSMutableDictionary alloc] init];
- //
- // NSMutableDictionary *senderProperty = [[NSMutableDictionary alloc] init];
- // [senderProperty setObject:[NSString stringWithFormat:@"%@ %@",ShareObj.loginUser.firstName,ShareObj.loginUser.lastName] forKey:kuserName];
- //
- // [senderProperty setObject:[NSString stringWithFormat:@"%ld",(long)ShareObj.loginUser.userId] forKey:kUserId];
- // [senderProperty setObject:ShareObj.loginUser.mobileNumber forKey:kMobileNumber];
- //
- // [msgDic setObject:senderProperty forKey:@"sender"];
- // [msgDic setObject:[objMessage getDic] forKey:@"message"];
- //
- // NSError *err = nil;
- //
- // NSData *jsonData = [NSJSONSerialization dataWithJSONObject:msgDic options:NSJSONWritingPrettyPrinted error:&err];
- //
- // NSString *str = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
- //
- // [xmppRoom sendMessageWithBody:str];
- //
- // [xmppRoom sendMessageWithBody:str];
- //
- // NSXMLElement *x = [NSXMLElement elementWithName:@"groupchat" xmlns:XMPPMUCNamespace];
- //
- // XMPPMessage* message = [[XMPPMessage alloc] initWithType:@"groupchat" to:recipient];
- // [message addChild:x];
- // [message addBody:str];
- //
- // [[APP_DELEGATE xmppStream] sendElement:message];
- }
- -(void)sendMessage:(Message *)objMessage
- to:(NSString *)receiverJId
- isGroup:(BOOL)isGroupMessage
- {
- XMPPJID* recipient = [XMPPJID jidWithString:receiverJId];
- XMPPMessage* message = [[XMPPMessage alloc] initWithType:@"chat" to:recipient];
- [message addAttributeWithName:@"type" stringValue:@"chat"];
- NSMutableDictionary *msgDic = [[NSMutableDictionary alloc] init];
- NSMutableDictionary *senderProperty = [[NSMutableDictionary alloc] init];
- [senderProperty setObject:[NSString stringWithFormat:@"%@ %@",ShareObj.loginUser.firstName,ShareObj.loginUser.lastName] forKey:kuserName];
- [senderProperty setObject:[NSString stringWithFormat:@"%ld",(long)ShareObj.loginUser.userId] forKey:kUserId];
- [senderProperty setObject:ShareObj.loginUser.mobileNumber forKey:kMobileNumber];
- [msgDic setObject:senderProperty forKey:@"sender"];
- [msgDic setObject:[objMessage getDic] forKey:@"message"];
- NSError *err = nil;
- NSData *jsonData = [NSJSONSerialization dataWithJSONObject:msgDic options:NSJSONWritingPrettyPrinted error:&err];
- NSString *str = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
- [message addBody:str];
- [[APP_DELEGATE xmppStream] sendElement:message];
- }
- #pragma mark Groups...
- - (void)createGroup:(Groups *)objGroup
- {
- XMPPRoomMemoryStorage *roomStorage = [[XMPPRoomMemoryStorage alloc] init];
- /*
- * Remember to add 'conference' in your JID like this:
- * e.g. uniqueRoomJID@conference.yourserverdomain
- */
- XMPPJID *roomJID = [XMPPJID jidWithString:objGroup.jId];
- XMPPRoom *xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:roomStorage
- jid:roomJID
- dispatchQueue:dispatch_get_main_queue()];
- [xmppRoom changeNickname:objGroup.groupName];
- [xmppRoom activate:self.xmppStream];
- [xmppRoom addDelegate:self
- delegateQueue:dispatch_get_main_queue()];
- [xmppRoom joinRoomUsingNickname:objGroup.groupName
- history:nil
- password:nil];
- }
- - (void)xmppRoomDidCreate:(XMPPRoom *)sender
- {
- NSLog(@"Group Created");
- }
- - (void)xmppRoomDidJoin:(XMPPRoom *)sender
- {
- NSLog(@"xmppRoomDidJoin");
- [sender fetchConfigurationForm];
- NSString *strRoomJId = [[sender myRoomJID] user];
- NSMutableDictionary *dic = [ShareObj.sharedDB getRecordFromQuery:[NSString stringWithFormat:
- @"Select * from Groups Where jId Like '%@'",kOFGroup(strRoomJId)]];
- Groups *objGroup = [[Groups alloc] initWithObject:dic];
- if(objGroup.groupMembers.length>0)
- {
- NSArray *arrUsers = [objGroup.groupMembers componentsSeparatedByString:@","];
- for(NSString *sreMobileNumber in arrUsers)
- {
- if(![sreMobileNumber isEqualToString:ShareObj.loginUser.mobileNumber])
- {
- XMPPJID *jId = [XMPPJID jidWithString:kOFUser([sreMobileNumber sha1])];
- [sender inviteUser:jId
- inGroup:objGroup
- withMessage:[NSString stringWithFormat:@"You are added in %@ group.",objGroup.groupName]];
- }
- if(![sender isJoined] )
- {
- }
- }
- }
- }
- - (void)xmppRoom:(XMPPRoom *)sender didFetchModeratorsList:(NSArray *)items
- {
- //DDLogInfo(@"%@: %@ --- %@", THIS_FILE, THIS_METHOD, sender.roomJID.bare);
- NSLog(@"%@: %@ --- %@", THIS_FILE, THIS_METHOD, sender.roomJID.bare);
- // if (check the flag for room and invite) // This has to be done only when we intended
- // {
- // NSArray* users = list of users we need to invite.
- //
- // if (users.count > 0)
- // {
- // for (int i=0; i< users.count; i++)
- // {
- // NSString *jid = [NSString stringWithFormat:@"%@@xyz.biz", [users objectAtIndex:i]];
- // XMPPJID *xmppJID=[XMPPJID jidWithString:jid];
- // [sender inviteUser:xmppJID withMessage:@"Join Group."];
- // }
- // [sender sendMessageWithBody:@"Hi All"];
- // }
- // }
- }
- - (void)xmppRoom:(XMPPRoom *)sender didFetchConfigurationForm:(NSXMLElement *)configForm
- {
- NSXMLElement *newConfig = [configForm copy];
- NSArray *fields = [newConfig elementsForName:@"field"];
- for (NSXMLElement *field in fields)
- {
- NSString *var = [field attributeStringValueForName:@"var"];
- // Make Room Persistent
- if ([var isEqualToString:@"muc#roomconfig_persistentroom"]) {
- [field removeChildAtIndex:0];
- [field addChild:[NSXMLElement elementWithName:@"value" stringValue:@"1"]];
- }
- }
- [sender configureRoomUsingOptions:newConfig];
- }
- - (void)xmppMUC:(XMPPMUC *)sender roomJID:(XMPPJID *) roomJID didReceiveInvitation:(XMPPMessage *)message;
- {
- NSLog(@"%@",roomJID);
- NSString *messageBody = [message body];
- NSError *jsonError;
- NSData *objectData = [messageBody dataUsingEncoding:NSUTF8StringEncoding];
- NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData
- options:NSJSONReadingMutableContainers
- error:&jsonError];
- Groups *objNewGroup = [[Groups alloc] initWithObject:json];
- [objNewGroup updateInDB];
- NSString *messageFromUser = [[message from] user];
- Conversation *objConversation = [Conversation getConversationWithGroup:objNewGroup];
- Message *objMessage = [[Message alloc] init];
- objMessage.messageId = [XMPPStream generateUUID];
- objMessage.msgType = MsgTypeAlert;
- objMessage.jId = messageFromUser;
- objMessage.msgContent = [NSString stringWithFormat:@"You are added in %@ Group.",objNewGroup.groupName];
- objMessage.conversationId = objConversation.conversationId;
- [objMessage setIsMsgRead:0];
- [objMessage setMsgStatus:Delivered];
- [objMessage updateInDB];
- [objConversation updateKey:kMsgId value:@(objMessage.msgId)];
- [xmppRoster addUser:roomJID withNickname:objNewGroup.groupName];
- [[NSNotificationCenter defaultCenter] postNotificationName:kReceivedMessageInConversationNotification
- object:objConversation];
- [self joinMultiUserChatRoom:roomJID];
- }
- - (void) joinMultiUserChatRoom:(XMPPJID *) roomJID
- {
- XMPPRoomMemoryStorage *roomMemoryStorage = [[XMPPRoomMemoryStorage alloc] init];
- XMPPRoom *xmppRoom = [[XMPPRoom alloc]
- initWithRoomStorage:roomMemoryStorage
- jid:roomJID
- dispatchQueue:dispatch_get_main_queue()];
- [xmppRoom activate:[self xmppStream]];
- [xmppRoom addDelegate:self delegateQueue:dispatch_get_main_queue()];
- [xmppRoom joinRoomUsingNickname:[NSString stringWithFormat:@"%@ %@",ShareObj.loginUser.firstName,ShareObj.loginUser.firstName]
- history:nil];
- }
- - (void)handlePresence:(XMPPPresence *)presence room:(XMPPRoom *)room;
- {
- NSLog(@"%@: %@", THIS_FILE, THIS_METHOD);
- }
- - (void)handleIncomingMessage:(XMPPMessage *)message room:(XMPPRoom *)room
- {
- NSLog(@"%@: %@", THIS_FILE, THIS_METHOD);
- }
- - (void)handleOutgoingMessage:(XMPPMessage *)message room:(XMPPRoom *)room
- {
- NSLog(@"%@: %@", THIS_FILE, THIS_METHOD);
- }
- - (void)handleDidLeaveRoom:(XMPPRoom *)room
- {
- NSLog(@"%@: %@", THIS_FILE, THIS_METHOD);
- }
Add Comment
Please, Sign In to add comment