XMPPStream *xmppStream; XMPPReconnect *xmppReconnect; XMPPRoster *xmppRoster;//用戶對象 //須要添加的對象
//添加好友 #pragma mark 加好友 - (void)XMPPAddFriendSubscribe:(NSString *)name { //XMPPHOST 就是服務器名, 主機名 XMPPJID *jid = [XMPPJID jidWithString:[NSString stringWithFormat:@"%@@%@",name,XMPPHOST]]; //[presence addAttributeWithName:@"subscription" stringValue:@"好友"]; [xmppRoster subscribePresenceToUser:jid]; }
//處理加好友 #pragma mark 處理加好友回調,加好友 - (void)xmppRoster:(XMPPRoster *)sender didReceivePresenceSubscriptionRequest:(XMPPPresence *)presence { //取得好友狀態 NSString *presenceType = [NSString stringWithFormat:@"%@", [presence type]]; //online/offline //請求的用戶 NSString *presenceFromUser =[NSString stringWithFormat:@"%@", [[presence from] user]]; NSLog(@"presenceType:%@",presenceType); NSLog(@"presence2:%@ sender2:%@",presence,sender); XMPPJID *jid = [XMPPJID jidWithString:presenceFromUser]; [xmppRoster acceptPresenceSubscriptionRequestFrom:jid andAddToRoster:YES]; }
//在次處理加好友 #pragma mark 收到好友上下線狀態 - (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence { // DDLogVerbose(@"%@: %@ ^^^ %@", THIS_FILE, THIS_METHOD, [presence fromStr]); //取得好友狀態 NSString *presenceType = [NSString stringWithFormat:@"%@", [presence type]]; //online/offline //當前用戶 // NSString *userId = [NSString stringWithFormat:@"%@", [[sender myJID] user]]; //在線用戶 NSString *presenceFromUser =[NSString stringWithFormat:@"%@", [[presence from] user]]; NSLog(@"presenceType:%@",presenceType); NSLog(@"用戶:%@",presenceFromUser); //這裏再次加好友 if ([presenceType isEqualToString:@"subscribed"]) { XMPPJID *jid = [XMPPJID jidWithString:[NSString stringWithFormat:@"%@",[presence from]]]; [xmppRoster acceptPresenceSubscriptionRequestFrom:jid andAddToRoster:YES]; } }
#pragma mark 刪除好友,取消加好友,或者加好友後須要刪除 - (void)removeBuddy:(NSString *)name { XMPPJID *jid = [XMPPJID jidWithString:[NSString stringWithFormat:@"%@@%@",name,XMPPHOST]]; [self xmppRoster] removeUser:jid]; }
注意主機名 和 處理順序