您需要执行以下步骤:
首先,使用 app id 创建 Facebook 流:
- (void)setupStreamFacebook
{
//NSAssert(xmppStream == nil, @"Method setupStream invoked multiple times");
// Setup xmpp stream
//
// The XMPPStream is the base class for all activity.
// Everything else plugs into the xmppStream, such as modules/extensions and delegates.
NSAssert(nil != self.facebookAppId, @"Please setup facebook app id");
NSAssert(nil != self.facebookToken, @"Please pass facebook access token");
xmppStreamFacebook = [[XMPPStream alloc] initWithFacebookAppId:self.facebookAppId];
#if !TARGET_IPHONE_SIMULATOR
{
xmppStream.enableBackgroundingOnSocket = YES;
}
#endif
// Setup reconnect
xmppReconnectFacebook = [[XMPPReconnect alloc] init];
// Setup roster
xmppRosterStorage = [[XMPPRosterCoreDataStorage alloc] initWithInMemoryStore];
if (!xmppRosterFacebook) {
xmppRosterFacebook = [[XMPPRoster alloc] initWithRosterStorage:xmppRosterStorage];
}
xmppRosterFacebook.autoFetchRoster = YES;
xmppRosterFacebook.autoAcceptKnownPresenceSubscriptionRequests = YES;
// Setup vCard support
if (!xmppvCardTempModule) {
xmppvCardTempModule = [[XMPPvCardTempModule alloc] initWithvCardStorage:xmppvCardStorage];
}
if (!xmppvCardAvatarModule) {
xmppvCardAvatarModule = [[XMPPvCardAvatarModule alloc] initWithvCardTempModule:xmppvCardTempModule];
}
if (!xmppCapabilities) {
xmppCapabilities = [[XMPPCapabilities alloc] initWithCapabilitiesStorage:xmppCapabilitiesStorage];
}
xmppCapabilities.autoFetchHashedCapabilities = YES;
xmppCapabilities.autoFetchNonHashedCapabilities = NO;
// Activate xmpp modules
[xmppReconnectFacebook activate:xmppStreamFacebook];
[xmppRosterFacebook activate:xmppStreamFacebook];
[xmppvCardTempModule activate:xmppStreamFacebook];
[xmppvCardAvatarModule activate:xmppStreamFacebook];
[xmppCapabilities activate:xmppStreamFacebook];
// Add ourself as a delegate to anything we may be interested in
[xmppStreamFacebook addDelegate:self delegateQueue:dispatch_get_main_queue()];
[xmppRosterFacebook addDelegate:self delegateQueue:dispatch_get_main_queue()];
// You may need to alter these settings depending on the server you're connecting to
allowSelfSignedCertificates = NO;
allowSSLHostNameMismatch = NO;
NSError *fbError = nil;
}
然后,生成 Facebook 访问令牌并将其传递给以下方法:
[xmppStreamFacebook authenticateWithFacebookAccessToken:self.facebookToken error:&fbError];
// NSLog(@"facebook erro = %@",error);
[xmppStreamFacebook connectWithTimeout:5 error:&fbError];
更新以下代表:
- (void)xmppStreamDidConnect:(XMPPStream *)sender
{
NSLog(@"%@: %@", THIS_FILE, THIS_METHOD);
NSLog(@"Stream Host: %@", sender.hostName);
if ([sender isEqual:xmppStreamFacebook])
{
if (![xmppStreamFacebook isSecure])
{
NSLog(@" if (![xmppStreamFacebook isSecure])");
NSError *error = nil;
BOOL result = [xmppStreamFacebook secureConnection:&error];
if (result == NO)
{
NSLog(@"%@: Error in xmpp STARTTLS: %@", THIS_FILE, error);
}
}
else
{
NSLog(@"authenticateWithFacebookAccessToken");
NSError *error = nil;
BOOL result = [xmppStreamFacebook authenticateWithFacebookAccessToken:self.facebookToken error:&error];
if (result == NO)
{
NSLog(@"%@: Error in xmpp auth: %@", THIS_FILE, error);
}
}
}
}
- (void)xmppStream:(XMPPStream *)sender willSecureWithSettings:(NSMutableDictionary *)settings
{
NSLog(@"%@: %@", THIS_FILE, THIS_METHOD);
if ([sender isEqual:xmppStreamFacebook])
{
NSLog(@"do not secure facebook");
return;
}
}
它会正常工作。