Reputation: 167
I am trying to implement an Instant Messaging App where users can chat as well as add other users to their roster and accept buddy requests. So, far I have been able to implement the chat and I am also able to receive and accept/reject friend requests.
For accepting/rejecting a subscription request, the code is as follows:
- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence
{
NSString *presenceType = [presence type]; // online / offline
NSString *myUsername = [[sender myJID] user];
NSString *presenceFromUser = [[presence from] user];
NSString *presencefromStr=[presence fromStr];
if ([presenceType isEqualToString:@"subscribe"]) {
if(buttonIndex==1) { // For accept button
[xmppRoster acceptPresenceSubscriptionRequestFrom:[tmpPresence from] andAddToRoster:YES];
}
else { // For reject button
[xmppRoster rejectPresenceSubscriptionRequestFrom:[tmpPresence from]];
}
}
However, now I am stuck with the problem of not being able to send a friend request. Can anyone guide me on which function of XMPPRoster to use? I tried using the subscribePresenceToUser function, but, it didn't work. Any help will be highly appreciated.
Also, can someone tell if the way I am going with this XMPPRoster subscription mechanism is right or is there a better way to handle the friend requests in XMPPFramework?
Thanks in advance.
Upvotes: 2
Views: 2678
Reputation: 2010
You can see XMPPRoster.h to see all the functions available inside the roster extension.
For your answer you have three options:
/**
* Adds the given user to the roster with an optional nickname
* and requests permission to receive presence information from them.
**/
- (void)addUser:(XMPPJID *)jid withNickname:(nullable NSString *)optionalName;
/**
* Adds the given user to the roster with an optional nickname,
* adds the given user to groups
* and requests permission to receive presence information from them.
**/
- (void)addUser:(XMPPJID *)jid withNickname:(nullable NSString *)optionalName groups:(nullable NSArray<NSString*> *)groups;
/**
* Adds the given user to the roster with an optional nickname,
* adds the given user to groups
* and optionally requests permission to receive presence information from them.
**/
- (void)addUser:(XMPPJID *)jid withNickname:(nullable NSString *)optionalName groups:(nullable NSArray<NSString*> *)groups subscribeToPresence:(BOOL)subscribe;
And to accept the friend request : (add as friend, as Fan or Decline)
addToRoster flag = true : Friend
addToRoster flag = false : Fan
/**
* Accepts the presence subscription request the given user.
*
* If you also choose, you can add the user to your roster.
* Doing so is similar to the traditional IM model.
**/
- (void)acceptPresenceSubscriptionRequestFrom:(XMPPJID *)jid andAddToRoster:(BOOL)flag;
/**
* Rejects the presence subscription request from the given user.
*
* If you are already subscribed to the given user's presence,
* rejecting they subscription request will not affect your subscription to their presence.
**/
- (void)rejectPresenceSubscriptionRequestFrom:(XMPPJID *)jid;
Upvotes: 0
Reputation: 41618
Answer by OP in comment:
XMPPJID *jid = [XMPPJID jidWithString:self.addFriendField.text];
[xmppRoster addUser:jid withNickname:nil];
This code snippet sends the request to other users and adds them to their Roster.
Upvotes: 2