can't receive group message using xmpp - ios

I can't receive group message on this method.
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
{
NSLog(#"Message received description === %#",[message description]);
}
-(void)sendGroupMessage:(NSString *)NameOfGroup :(NSString *)Msg
{
[xmppRoom sendMessageWithBody:Msg];
NSXMLElement *x = [NSXMLElement elementWithName:NameOfGroup xmlns:XMPPMUCNamespace];
XMPPMessage *message = [XMPPMessage message];
[message addAttributeWithName:#"to" stringValue:[NSString stringWithFormat:#"%#",NameOfGroup]];
[message addChild:x];
NSLog(#"x in Invite === %#",x);
[xmppRoom sendMessage:message];
}
-(void)setUpRoom:(NSString *)ChatRoomJID
{
NSString *Strng=[NSString stringWithFormat:#"Abc#conference.localhost"];
ChatRoomJID=Strng;
XMPPRoomMemoryStorage *roomMemoryStorage = [[XMPPRoomMemoryStorage alloc] init];
XMPPJID *roomJID = [XMPPJID jidWithString:ChatRoomJID];
xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:roomMemoryStorage jid:roomJID dispatchQueue:dispatch_get_main_queue()];
[xmppRoom activate:xmppStream];
[xmppRoom addDelegate:self delegateQueue:dispatch_get_main_queue()];
[xmppRoom joinRoomUsingNickname:self.UserID
history:nil
password:nil];
[self performSelector:#selector(ConfigureNewRoom:) withObject:nil afterDelay:4];
}
First of all I create new group, send invitation and successfully accept it also for all invited users.
When I send message on that group, all person got message successfully, But if I will stop App and run again and login with xmpp and send message on group, message is not delivered to joined users.
When i have to log out and login again to xmpp server and send message on previously created group i got this error message
<message xmlns="jabber:client" from="dharmraj123#conference.localhost" to="revoz1wwbzji1e1481005747#localhost/1824017799289652742921131" type="error"><body>1 Message</body><error code="406" type="modify"><not-acceptable xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></not-acceptable><text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas">Only occupants are allowed to send messages to the conference</text></error></message>

Related

How to delete or exit from MUC in XMPP

Currently i am making chat application and i required to delete Particular group chat.
For delete group i tried below solution but not working.please check this code
-(void)ExitGroup:(NSString *)Roomjdi Removeuserid:(NSString *)Userid {
{
XMPPJID *roomJID = [XMPPJID jidWithString:Roomjdi];
XMPPRoomMemoryStorage *roomMemoryStorage = [[XMPPRoomMemoryStorage alloc] init];
XMPPRoom *xmppRooms = [[XMPPRoom alloc]
initWithRoomStorage:roomMemoryStorage
jid:roomJID
dispatchQueue:dispatch_get_main_queue()];
[xmppRooms removeDelegate:self delegateQueue:dispatch_get_main_queue()];
[self deactivate:roomJID];
[self leaveRoom:roomJID];
}
- (void)leaveRoom:(XMPPJID *)myRoomJID
{
// dispatch_block_t block = ^{ #autoreleasepool {
//XMPPLogTrace();
// <presence type='unavailable' to='darkcave#chat.shakespeare.lit/thirdwitch'/>
XMPPPresence *presence = [XMPPPresence presence];
[presence addAttributeWithName:#"to" stringValue:[myRoomJID full]];
[presence addAttributeWithName:#"type" stringValue:#"unavailable"];
[xmppStream sendElement:presence];
[xmppRoom leaveRoom];
[xmppRoom deactivate];
[xmppRoom removeDelegate:self];
}
after i execute this method but in ejabbared display online user in group list.please tell me any where i am wrong.

Objective C: add contact to xmpp framework via app

I am doing Chat App. XMPP Framework, automatically fetch contacts from ejabbered server. I am unable to add contacts to ejabbered server through my app. For ex: If I login through iOS Client, Jitsi, there I can give add contact. That too immediately reflect in my app and ejabbered server automatically. I dont know how to add my phone book contacts to ejabbered via my app.
Kindly guide how to add contact through my app.
Coding
- (void)setupStream
{
XMPPJID jid = [XMPPJID jidWithString:[NSString stringWithFormat:#"%##localhost",
`addBuddyTextField.text]];
[appDelegate.xmppRoster addUser:jid withNickname:addBuddyTextField.text];
xmppStream = [[XMPPStream alloc] init];
xmppReconnect = [[XMPPReconnect alloc] init];
xmppRosterStorage = [[XMPPRosterCoreDataStorage alloc] init];
xmppRoster = [[XMPPRoster alloc] initWithRosterStorage:xmppRosterStorage];
xmppRoster.autoFetchRoster = YES;
xmppRoster.autoAcceptKnownPresenceSubscriptionRequests = YES;
}
For adding contacts first you need to send subscribe message to destination user (if destination user is registered to XMPP)
Send subscribe message
- (void) sendSubscribeMessageToUser:(NSString*)userID
{
XMPPJID* jbid= [XMPPJID jidWithString:userID];
XMPPPresence *presence = [XMPPPresence presenceWithType:#"subscribe" to:jbid];
[xmppStream sendElement:presence];
}
when destination user received subscribe message
/**
This fuction is called when other user state is changed
*/
- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence
{
DDLogVerbose(#"%#: %# - %#", THIS_FILE, THIS_METHOD, [presence fromStr]);
NSString *presenceType = [presence type]; // online/offline
NSString *myUsername = [[sender myJID] user];
NSString *presenceFromUser = [[presence from] user];
NSString* presenceState= [presence status];
NSLog(#"%# is %# state %#",presenceFromUser,presenceType,presenceState);
if ([presenceType isEqualToString:#"subscribe"])
{
[xmppRoster subscribePresenceToUser:[presence from]];
}
else if ([presenceType isEqualToString:#"subscribed"])
{
[xmppRoster subscribePresenceToUser:[presence from]];
}
}
these functions will add automatically destination user to your contact list

Unable to send and receive message in Group Chat using Xmpp in iphone

Using xmpp I can able to create group and send invitation to friends but while I send message on group, members will never received that message.
Is member have to accept the invitation ? If yes, then let me know how?
Please refer below code and if I made any mistake or still I am missing any things then guide me for that so after I can send and receive messages in group and chat with friends.
Below I'm attaching my some code snippet for create group in xmpp and send message.
[self setUpRoom:[NSString stringWithFormat:#"%##conference.myserver",#"GroupName"]];
-(void)setUpRoom:(NSString *)ChatRoomJID {
// Configure xmppRoom
XMPPRoomMemoryStorage *roomMemoryStorage = [[XMPPRoomMemoryStorage alloc] init];
XMPPJID *roomJID = [XMPPJID jidWithString:ChatRoomJID];
xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:roomMemoryStorage jid:roomJID dispatchQueue:dispatch_get_main_queue()];
[xmppRoom activate:xmppStream];
[xmppRoom addDelegate:self delegateQueue:dispatch_get_main_queue()];
[xmppRoom joinRoomUsingNickname:_ro(#"LoginNumber")
history:nil
password:nil];
[self performSelector:#selector(ConfigureNewRoom:) withObject:nil afterDelay:4];
}
Now for room confirmation i used this snippet
- (void)ConfigureNewRoom:(id)sender
{
[xmppRoom configureRoomUsingOptions:nil];
[xmppRoom fetchConfigurationForm];
[xmppRoom fetchBanList];
[xmppRoom fetchMembersList];
[xmppRoom fetchModeratorsList];
}
XMPPRoom Delegate Mothods
- (void)xmppRoomDidCreate:(XMPPRoom *)sender
{
DDLogInfo(#"%#: %#", THIS_FILE, THIS_METHOD);
// I am inviting friends after room is created
for (int i = 0; i<[self.friendListArray count]; i++)
{
NSString * tempStr=[NSString stringWithFormat:#"%##myserver",[[self.friendListArray objectAtIndex:i] valueForKey:#"UserNumber"]];
[sender inviteUser:[XMPPJID jidWithString:tempStr] withMessage:#"Greetings!"];
}
}
- (void)xmppRoomDidJoin:(XMPPRoom *)sender
{
DDLogInfo(#"%#: %#", THIS_FILE, THIS_METHOD);
NSLog(#"........Room Did join.......");
}
- (void)xmppRoom:(XMPPRoom *)sender didFetchConfigurationForm:(NSXMLElement *)configForm
{
DDLogInfo(#"%#: %#", THIS_FILE, THIS_METHOD);
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"]];
}
if ([var isEqualToString:#"roomconfig_enablelogging"])
{
[field removeChildAtIndex:0];
[field addChild:[NSXMLElement elementWithName:#"value" stringValue:#"1"]];
}
if ([var isEqualToString:#"muc#roomconfig_maxusers"])
{
[field removeChildAtIndex:0];
[field addChild:[NSXMLElement elementWithName:#"value" stringValue:#"100"]];
}
}
[sender configureRoomUsingOptions:newConfig];
}
Sending message in group on button click for testing purpose
-(void)sendGroupMessage
{
[xmppRoom sendMessageWithBody:#"Hi All"];
NSXMLElement *x = [NSXMLElement elementWithName:#"groupchat" xmlns:XMPPMUCNamespace];
XMPPMessage *message = [XMPPMessage message];
[message addAttributeWithName:#"to" stringValue:[NSString stringWithFormat:#"%#/%#",[xmppRoom.roomJID full],_ro(#"LoginNumber")]];
[message addChild:x];
NSLog(#"x in Invite === %#",x);
[xmppStream sendElement:message];
}
Instead of:
[xmppStream sendElement:message]
try:
[xmppRoom sendMessage:message]
For MUC chat the member has to accept the invitation.
You can find the answer here:
https://stackoverflow.com/a/26031897/4050160

Adding user to xmpp room

Hello I know there is alot question regarding this but still i could not figure out what can be the problem. I have sucessfully created group using following code.
- (void)createGroup
{
iXmppEngine.userID = #"lahore123#hassan.local";
iXmppEngine.userPass = #"password123";
self.rosterstorage =[[XMPPRoomCoreDataStorage alloc] init];
XMPPRoom *xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:self.rosterstorage jid:[XMPPJID jidWithString:#"test#conference.hassan.local"] dispatchQueue:dispatch_get_main_queue()];
[xmppRoom activate:[[self iXmppEngine]xmppStream]];
[xmppRoom joinRoomUsingNickname:#"Dev iphone" history:nil];
[[[self iXmppEngine] xmppStream] addDelegate:self delegateQueue:dispatch_get_main_queue()];
[xmppRoom addDelegate:self delegateQueue:dispatch_get_main_queue()];
[self performSelector:#selector(joinRoom) withObject:nil afterDelay:4];
}
And For adding user i used following code. I have called this function with delay of 5s so room can be created succesfully before adding users.
- (void)joinRoom
{
self.iXmppEngine.userID = #"lahore#hassan.local";
// self.rosterstorage = [[XMPPRoomCoreDataStorage alloc] init];
XMPPRoom *xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:self.rosterstorage jid:[XMPPJID jidWithString:#"test#conference.hassan.local"] dispatchQueue:dispatch_get_main_queue()];
[xmppRoom activate:[[self iXmppEngine] xmppStream]];
[xmppRoom joinRoomUsingNickname:#"lahore" history:nil];
[xmppRoom fetchConfigurationForm];
[xmppRoom configureRoomUsingOptions:nil];
[xmppRoom addDelegate:[self iXmppEngine] delegateQueue:dispatch_get_main_queue()];
}
What i am doing wrong couldnt find.
Creating and Joining (Already Created Room or Accepting Invitation To Join Room) Rooms are two differnet scenarios.
// CONFERENCE_ROOM_SERVER is the domain name for MUC
For Creating Group Do This Way:
- (void)createChatRoom:(NSString *) newRoomName
{
XMPPJID *roomJID = [XMPPJID jidWithString:[NSString stringWithFormat:#"%##%#", newRoomName, CONFERENCE_ROOM_SERVER]];
XMPPRoomMemoryStorage *roomMemoryStorage = [[XMPPRoomMemoryStorage alloc] init];
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:#"Your Nick Name" history:nil];
}
You will get the response after the group has been created successfully in this delegate:
- (void)xmppRoomDidCreate:(XMPPRoom *)sender
Now Invite Users Here This Way:
[sender inviteUser:[XMPPJID jidWithString:inviteUserJID] withMessage:#"Any Message"];

iPhone Jabber/XMPP client... "TURN Connection failed"

I am trying to build a simple Jabber client.
I have downloaded this sample project, which uses xmpp-framework https://github.com/funkyboy/Building-a-Jabber-client-for-iOS
I am running it in the iOS Simulator. I have installed Openfire locally so that I can interact with a user logged into iChat.
Unfortunately the app only receives messages. It fails in sending messages giving the error "TURN Connection failed!".
This is the code attempting to connect:
- (void)viewDidLoad
{
[super viewDidLoad];
self.tView.delegate = self;
self.tView.dataSource = self;
[self.tView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
messages = [[NSMutableArray alloc ] init];
JabberClientAppDelegate *del = [self appDelegate];
del._messageDelegate = self;
[self.messageField becomeFirstResponder];
XMPPJID *jid = [XMPPJID jidWithString:#"user#server.local"];
NSLog(#"Attempting TURN connection to %#", jid);
TURNSocket *turnSocket = [[TURNSocket alloc] initWithStream:[self xmppStream] toJID:jid];
[turnSockets addObject:turnSocket];
[turnSocket startWithDelegate:self delegateQueue:dispatch_get_main_queue()];
[turnSocket release];
}
And those are the methods called on success/failure:
- (void)turnSocket:(TURNSocket *)sender didSucceed:(GCDAsyncSocket *)socket
{
NSLog(#"TURN Connection succeeded!");
NSLog(#"You now have a socket that you can use to send/receive data to/from the other person.");
[turnSockets removeObject:sender];
}
- (void)turnSocketDidFail:(TURNSocket *)sender
{
NSLog(#"TURN Connection failed!");
[turnSockets removeObject:sender];
}
Can anyone please help?
Thank you.
There is no reason to use TURN for normal messaging. TURN is required for media streaming only. Just use XMPPFramework. There are some good getting-started guides.
Next, use code of this nature to create and send stanzas:
XMPPMessage *msg = [XMPPMessage message];
[msg addAttributeWithName:#"type" stringValue:#"chat"];
[msg addAttributeWithName:#"to" stringValue:#"foo#example.com"];
NSXMLElement *body = [NSXMLElement elementWithName:#"body" stringValue:#"Hello"];
[msg addChild:body];
[[self xmppStream] sendElement:msg];
Note that msg is just a subclass of NSXMLElement, so you can modify the XML at will to craft the protocol you're going to send.

Resources