Sepehrom
Sepehrom

Reputation: 1335

XMPPFramework - Auto Accept of Presence Subscription Requests

I think the title is illustrating enough, but here's the story:

I'm new to XMPPFramework for iOS, and I want to set my client to automatically accept any subscription request it receives. So that other clients can see this client's presence status, when they request it.

According to developer comments In XMPPRoster.h file, there's this method which is called when a subscription request is received:

/**
 * Sent when a presence subscription request is received.
 * That is, another user has added you to their roster,
 * and is requesting permission to receive presence broadcasts that you send.
 * 
 * The entire presence packet is provided for proper extensibility.
 * You can use [presence from] to get the JID of the user who sent the request.
 * 
 * The methods acceptPresenceSubscriptionRequestFrom: and rejectPresenceSubscriptionRequestFrom: can
 * be used to respond to the request.
**/
- (void)xmppRoster:(XMPPRoster *)sender didReceivePresenceSubscriptionRequest:(XMPPPresence *)presence;

But it is not implemented in XMPPRoster.m. So I implemented it as following :

- (void)xmppRoster:(XMPPRoster *)sender didReceivePresenceSubscriptionRequest:(XMPPPresence *)presence
{
    [self acceptPresenceSubscriptionRequestFrom:[presence from] andAddToRoster:YES];
}

Since I'm new to XMPPFramework I dunno if I have done anything wrong, but I still cannot get this client's presence in other clients.

I also have seen similar topics like Accept buddy request in xmpp client iphone or Xmpp Accepting buddy request but the solution does not seem to be even related !

Any suggestions is really appreciated. Thanks.

Upvotes: 1

Views: 3386

Answers (1)

fnc12
fnc12

Reputation: 2237

You did it wrong. You do not have to implement something in XMPPRoster.m or other library files. This function

- (void)xmppRoster:(XMPPRoster *)sender didReceivePresenceSubscriptionRequest:(XMPPPresence *)presence;

is a callback fired when your xmpp client receives presence subscription request. If you want to execute some code when this callback fired you have to implement a protocol called XMPPRosterDelegate. Protocol is a feature like interface in Java and C# or like abstract class in C++. You have to have a class that inherits from this XMPPRosterDelegate and finally implements this function (and other functions if you want so).

If you want to autoaccept all requests you have to implement your protocol function implementation like this:

-(void)xmppRoster:(XMPPRoster *)sender didReceivePresenceSubscriptionRequest:(XMPPPresence *)presence{
    [sender acceptPresenceSubscriptionRequestFrom:[presence from] andAddToRoster:YES];
}

Also roster object got to know who is its delegate (an object who implements XMPPRosterDelegate), cause if you want to send someone a message you have to know two things: target and selector. Selector is specified in protocol. Target is a delegate property. You have to set roster's delegate during its initialization. In my code I added line

[xmppRoster addDelegate:self delegateQueue:dispatch_get_main_queue()];

before line

[xmppRoster            activate:xmppStream];

Of course self implements XMPPRosterDelegate and especially has this piece of code

-(void)xmppRoster:(XMPPRoster *)sender didReceivePresenceSubscriptionRequest:(XMPPPresence *)presence{
    [sender acceptPresenceSubscriptionRequestFrom:[presence from] andAddToRoster:YES];
}

Good luck and sorry for long post.

Upvotes: 4

Related Questions