目标C:通过app添加联系人到xmpp框架

时间:2015-10-29 07:46:34

标签: ios objective-c xmppframework

我在做聊天应用程序。 XMPP Framework,自动从ejabbered服务器获取联系人。我无法通过我的应用向 ejabbered server 添加联系人。例如:如果我通过iOS客户端 Jitsi 登录,我可以添加联系人。这也会立即反映在我的app和ejabbered服务器中。我不知道如何通过我的应用程序将我的电话簿联系人添加到ejabbered。

请指导如何通过我的应用添加联系人。

编码

- (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;
}

1 个答案:

答案 0 :(得分:0)

首先添加联系人需要向目标用户发送订阅消息(如果目标用户注册到XMPP)

发送订阅消息

 - (void) sendSubscribeMessageToUser:(NSString*)userID
{
    XMPPJID* jbid=  [XMPPJID jidWithString:userID];
    XMPPPresence *presence = [XMPPPresence presenceWithType:@"subscribe" to:jbid];
    [xmppStream sendElement:presence];
}

目标用户收到订阅消息时

/**
 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]];
    }

}

这些功能会自动将目标用户添加到您的联系人列表