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];}
注意主機名稱 和 處理順序