2015-10-29 49 views
1

我在做聊天應用程序。 XMPP框架自動從ejabbered服務器獲取聯繫人。我無法通過我的應用程序向ejabbered服務器添加聯繫人。例如:如果我通過iOS客戶端登錄,Jitsi,那麼我可以給添加聯繫人。這也立即反映在我的應用程序和ejabbered服務器自動。我不知道如何通過我的應用程序將我的電話簿聯繫人添加到ejabbered。目標C:通過應用程序向xmpp框架添加聯繫人

請指導如何通過我的應用程序添加聯繫人。

編碼

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

回答

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

} 

這些功能將目標用戶自動添加到您的聯繫人列表

+0

subscribePresenceToUser方法不叫.. – bittu

相關問題