从上面的讨论中,听起来您的服务器正在处理您的名册请求。使用您的设置代码我无法重现您遇到的问题。
您可以尝试打开日志记录,看看阅读协议日志是否有助于您了解正在发生的事情:
#import "DDTTYLogger.h"
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[DDLog addLogger:[DDTTYLogger sharedInstance]];
...
或者在你的服务器上尝试我的测试代码,如果问题消失,从那里开始工作:
#import "AppDelegate.h"
#import "XMPPFramework.h"
#import "DDTTYLogger.h"
#import "DDLog.h"
static const int ddLogLevel = LOG_LEVEL_VERBOSE;
NSString * const XMPPAuthenticationMethodPlain = @"Plain";
NSString * const XMPPAuthenticationMethodDigestMD5 = @"Digest-MD5";
NSString * const OptionHostName = @"...";
NSUInteger const OptionPort = 5222;
BOOL const OptionOldSchoolSSL = NO;
NSString * const OptionJID = @"...";
NSString * const OptionAuthenticationMethod = @"Digest-MD5";
NSString * const OptionPassword = @"...";
@interface AppDelegate () <XMPPStreamDelegate, XMPPRosterMemoryStorageDelegate>
@property (retain) XMPPStream *xmppStream;
@property (retain) XMPPRosterMemoryStorage *xmppRosterMemStorage;
@property (retain) XMPPRoster *xmppRoster;
@end
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[DDLog addLogger:[DDTTYLogger sharedInstance]];
self.xmppStream = [[XMPPStream alloc] init];
[self.xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
self.xmppStream.hostName = OptionHostName;
self.xmppStream.hostPort = OptionPort;
self.xmppStream.myJID = [XMPPJID jidWithString:OptionJID];
self.xmppRosterMemStorage = [[XMPPRosterMemoryStorage alloc] init];
self.xmppRoster = [[XMPPRoster alloc] initWithRosterStorage:self.xmppRosterMemStorage
dispatchQueue:dispatch_get_main_queue()];
[self.xmppRoster addDelegate:self delegateQueue:dispatch_get_main_queue()];
[self.xmppRoster activate:self.xmppStream];
NSError *error = nil;
if (OptionOldSchoolSSL)
[self.xmppStream oldSchoolSecureConnect:&error];
else
[self.xmppStream connect:&error];
}
-(void)applicationWillTerminate:(NSNotification *)notification {
[self.xmppStream removeDelegate:self];
[self.xmppStream disconnect];
}
-(void)xmppStreamDidConnect:(XMPPStream *)sender {
Class authClass = nil;
if ([OptionAuthenticationMethod isEqual:XMPPAuthenticationMethodPlain])
authClass = [XMPPPlainAuthentication class];
else if ([OptionAuthenticationMethod isEqual:XMPPAuthenticationMethodDigestMD5])
authClass = [XMPPDigestMD5Authentication class];
else {
DDLogWarn(@"Unrecognized auhthentication method '%@', falling back on Plain",
OptionAuthenticationMethod);
authClass = [XMPPPlainAuthentication class];
}
id<XMPPSASLAuthentication> auth = [[authClass alloc] initWithStream:sender
password:OptionPassword];
NSError *error = nil;
if (![sender authenticate:auth error:&error])
NSLog(@"Error authenticating: %@", error);
}
-(void)xmppRosterDidPopulate:(XMPPRosterMemoryStorage *)sender {
NSLog(@"users: %@", [sender unsortedUsers]);
// My subscribed users do print out
}
@end
如果我将名册设置代码移至-xmppStreamDidAuthenticate
,它也可以工作,但是在这种情况下我确实需要手动调用-fetchRoster
。