Demonedge
Demonedge

Reputation: 1423

Can't send message using open fire and iOS

I downloaded this iOS client from here:https://github.com/robbiehanson/XMPPFramework/ and want to add some code so that it can send message. I created a new class called ChatViewController, loaded it from RootViewController. When initializing this view controller, I pass xmppstream as a parameter below:

stream =[[self appDelegate]xmppStream];
ChatViewController *chat = [[ChatViewController alloc]initWithStream:stream jid:user.jid];    
[self.navigationController pushViewController:chat animated:YES];

I checked in initWithStream that I can send message using the stream I get from RootViewController, however I can't send a message through a click on a button on ChatViewController. The m file of ChatViewController is as below:

#import "ChatViewController.h"
#import "XMPPFramework.h"

@interface ChatViewController ()

@end

@implementation ChatViewController

@synthesize MyxmppStream;
@synthesize jid;

-(id)initWithStream:(XMPPStream *)stream jid:(XMPPJID *)ajid{
    self = [super initWithNibName:@"ChatViewController" bundle:nil];
    if (self) {
        jid = ajid;
        MyxmppStream = stream;
    }
    return self;
}
- (IBAction)SendMessage:(id)sender {
    NSXMLElement *body = [NSXMLElement elementWithName:@"body"];
    [body setStringValue:@"aaaa"];

    NSXMLElement *message = [NSXMLElement elementWithName:@"message"];
    [message addAttributeWithName:@"type" stringValue:@"chat"];
    [message addAttributeWithName:@"to" stringValue:[jid full]];
    [message addChild:body];

    [MyxmppStream sendElement:message];
}

@end

If I put the code sending message in initWithStream,it can work fine and send a message successfully, but the same code in SendMessage doesn't work at all.

Upvotes: 0

Views: 473

Answers (1)

Sushrita
Sushrita

Reputation: 725

Try this hope this will work:

- (IBAction)sendMessage {


    sender = [[[[self appDelegate] xmppStream] myJID] bare];



    NSString *messageStr = self.messageField.text;
    NSLog(@"The Mesage%@",messageStr);

    if([messageStr length] > 0) {

    NSXMLElement *body = [NSXMLElement elementWithName:@"body"];
    [body setStringValue:messageStr];

    XMPPMessage *message = [XMPPMessage elementWithName:@"message"];
    [message addAttributeWithName:@"type" stringValue:@"chat"];

    [message addAttributeWithName:@"to" stringValue:chatWithUser];

    [message addChild:body];

    self.messageField.text = messageStr;
    NSMutableDictionary *m = [[NSMutableDictionary alloc] init];

    [m setObject:[messageStr substituteEmoticons] forKey:@"msg"];
    [m setObject:sender forKey:@"sender"];

    [messages addObject:m];

    [[self xmppStream ] sendElement:message];

    NSLog(@"message :",messages);

    }

Upvotes: 1

Related Questions