Iphone 如何使用xmpp框架仅列出facebook上的在线用户
我已经在我的应用程序中集成了xmpp,能够在表视图中列出所有用户,但我只想显示在线用户,然后想实现向我的在线朋友发送和接收消息的功能 请给我推荐一些有用的代码 这是我的代码,在登录facebook后执行Iphone 如何使用xmpp框架仅列出facebook上的在线用户,iphone,facebook-graph-api,xmpp,xmppframework,Iphone,Facebook Graph Api,Xmpp,Xmppframework,我已经在我的应用程序中集成了xmpp,能够在表视图中列出所有用户,但我只想显示在线用户,然后想实现向我的在线朋友发送和接收消息的功能 请给我推荐一些有用的代码 这是我的代码,在登录facebook后执行 - (void)fbDidLogin { NSLog(@"logged in....................."); [appDelegate.facebook requestWithGraphPath:@"me" andDelegate:self];
- (void)fbDidLogin
{
NSLog(@"logged in.....................");
[appDelegate.facebook requestWithGraphPath:@"me" andDelegate:self];
DDLogVerbose(@"%s accessToken: %@ expirationDate: %@",__PRETTY_FUNCTION__,appDelegate.facebook.accessToken,appDelegate.facebook.expirationDate);
self.accessToken = appDelegate.facebook.accessToken;
if (xmppStreamFB) {
[xmppStreamFB release];
xmppStreamFB = nil;
}
xmppStreamFB = [[XMPPStreamFacebook alloc] init];
xmpReconnect = [[XMPPReconnect alloc] initWithStream:xmppStreamFB];
if (xmppRosterStorage) {
[xmppRosterStorage release];
xmppRosterStorage = nil;
}
xmppRosterStorage = [[XMPPRosterCoreDataStorage alloc] init];
if (xmppRoster) {
[xmppRoster release];
xmppRoster = nil;
}
xmppRoster = [[XMPPRoster alloc] initWithStream:xmppStreamFB rosterStorage:xmppRosterStorage];
[xmppStreamFB addDelegate:self];
[xmppRoster addDelegate:self];
[xmppRoster setAutoRoster:YES];
xmppStreamFB.myJID = [XMPPJID jidWithString:[NSString stringWithFormat:@"%@@chat.facebook.com", uid]];
// You may need to alter these settings depending on the server you're connecting to
allowSelfSignedCertificates = NO;
allowSSLHostNameMismatch = YES;
// Uncomment me when the proper information has been entered above.
NSError *error = nil;
if (![xmppStreamFB connect:&error])
NSLog(@"Error connecting: %@", error);
if(!tableView)
{
tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0, 480, 320) style:UITableViewStylePlain];
}
[tableView setFrame:CGRectMake(0,0, 480, 320)];
[tableView setTag:2];
[tableView setDelegate:self];
[tableView setDataSource:self];
[tableView setHidden:NO];
[tableView setBackgroundColor:[UIColor clearColor]];
[tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
[tableView setAlpha:1.0];
[self.view addSubview:tableView];
[self.tableView reloadData];
[self showTopBar];
}
我不知道xmpp框架用于在线显示用户和实现聊天功能的实际流程
i have the following delegate methods as well..
- (void)xmppStreamDidSecure:(XMPPStreamFacebook *)sender
{
NSLog(@"---------- xmppStreamDidSecure: ----------");
}
- (void)xmppStreamDidConnect:(XMPPStreamFacebook *)sender
{
NSLog(@"---------- xmppStreamDidConnect: ----------");
isOpen = YES;
NSError *error = nil;
if (![self.xmppStreamFB authenticateWithAppId:_APP_ID accessToken:self.accessToken error:&error])
{
NSLog(@"Error authenticating: %@", error);
}
else {
NSLog(@"NO Error authenticating:");
/*
ChatViewController *cvc = [[ChatViewController alloc] init];
[self.view addSubview:cvc.view];*/
}
}
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender
{
NSLog(@"---------- xmppStreamDidAuthenticate: ----------");
[self goOnline];
}
- (void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(NSXMLElement *)error
{
NSLog(@"---------- xmppStream:didNotAuthenticate: ----------");
}
- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq
{
NSLog(@"---------- xmppStream:didReceiveIQ: ----------");
/*
ChatViewController *cvc = [[ChatViewController alloc] init];
[self.view addSubview:cvc.view];*/
return NO;
}
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
{
NSLog(@"---------- xmppStream:didReceiveMessage: ----------");
}
- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence
{
NSLog(@"---------- xmppStream:didReceivePresence: ----------");
}
- (void)xmppStream:(XMPPStream *)sender didReceiveError:(id)error
{
NSLog(@"---------- xmppStream:didReceiveError: ----------");
}
- (void)xmppStreamDidDisconnect:(XMPPStream *)sender
{
NSLog(@"---------- xmppStreamDidDisconnect: ----------");
if (!isOpen)
{
NSLog(@"Unable to connect to server. Check xmppStream.hostName");
}
}
以及在线和离线用户状态的两种方法,但不知道如何为我的任务修改它们:
- (void)goOnline
{
NSXMLElement *presence = [NSXMLElement elementWithName:@"presence"];
[[self xmppStream] sendElement:presence];
}
- (void)goOffline
{
NSXMLElement *presence = [NSXMLElement elementWithName:@"presence"];
[presence addAttributeWithName:@"type" stringValue:@"unavailable"];
[[self xmppStream] sendElement:presence];
}
经过大量的努力,我终于找到了如何显示在线/离线/异地用户的方法 我将一步一步地告诉你我是如何做到的,这样我也可以对经验不足的用户有用 第1步-点击聊天按钮,我调用以下方法-
-(void) chatFacebook
{
if (appDelegate.facebook == nil)
{
appDelegate.facebook = [[[Facebook alloc] initWithAppId:_APP_ID] autorelease];
}
if (!accessToken)
{
[appDelegate.facebook authorize:[XMPPStreamFacebook permissions] delegate:self appAuth:NO safariAuth:NO];
}
else
{
[self fbDidLogin];
}
}
第2步-现在是登录对话框委托方法进入的时候了,如果登录成功,将调用fbDidLogin,下面是您应该包括的委托方法-
#pragma mark FBLoginDialogDelegate
/**
*当用户成功登录时调用。
*/
}
/**
*当用户未登录就取消对话框时调用。
*/
/**
*当用户注销时调用。
*/
步骤3-fbDidLogin方法的第二行调用FBRequestDelegate方法,因此您应该在.h类中包含此协议,以获取用户的uid(已登录,表示当前用户),您需要实现以下方法-
布拉格标记
pragma标记FBRequestDelegate
/**
*当请求返回且其响应已解析为对象时调用。
*结果对象可能是字典、数组、字符串或数字,具体取决于
*关于API响应的格式。
*/
步骤4-现在是表视图数据源和委托方法,您需要实现这些方法,下面是这些方法-
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView1
{
return [[[self fetchedResultsController] sections] count];
- (NSFetchedResultsController *)fetchedResultsController
{
if (fetchedResultsController == nil)
{
NSEntityDescription *entity = [NSEntityDescription entityForName:@"XMPPUserCoreDataStorage"
inManagedObjectContext:[self managedObjectContext]];
NSSortDescriptor *sd1 = [[NSSortDescriptor alloc] initWithKey:@"sectionNum" ascending:YES];
NSSortDescriptor *sd2 = [[NSSortDescriptor alloc] initWithKey:@"displayName" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sd1, sd2, nil];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:entity];
[fetchRequest setSortDescriptors:sortDescriptors];
[fetchRequest setFetchBatchSize:10];
fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:[self managedObjectContext]
sectionNameKeyPath:@"sectionNum"
cacheName:nil];
[fetchedResultsController setDelegate:self];
[sd1 release];
[sd2 release];
[fetchRequest release];
NSError *error = nil;
if (![fetchedResultsController performFetch:&error])
{
NSLog(@"Error performing fetch: %@", error);
}
}
return fetchedResultsController;
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[[self tableView] reloadData];
}
//需要实现NSFetchedResultsControllerDelegate
}
{
NSArray*节=[[self-fetchedResultsController]节]
if (sectionIndex < [sections count])
{
id <NSFetchedResultsSectionInfo> sectionInfo = [sections objectAtIndex:sectionIndex];
int section = [sectionInfo.name intValue];
switch (section)
{
case 0 : return @"Available";
case 1 : return @"Away";
default : return @"Offline";
}
}
}
return @"";
}
- (NSInteger)tableView:(UITableView *)tableView1 numberOfRowsInSection:(NSInteger)sectionIndex
{
NSArray *sections = [[self fetchedResultsController] sections];
if (sectionIndex < [sections count])
{
id <NSFetchedResultsSectionInfo> sectionInfo = [sections objectAtIndex:sectionIndex];
return sectionInfo.numberOfObjects;
NSLog(@"section ifnfo ===========%@", sectionInfo);
}
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[cell setSelectionStyle:UITableViewCellSelectionStyleGray];
user = [[self fetchedResultsController] objectAtIndexPath:indexPath];
cell.textLabel.text = user.displayName;
cell.textLabel.textColor = [UIColor whiteColor];
cell1 = cell;
第6步-编译并运行代码,用户列表应该出现在表视图中
如果出现任何问题,请分享,我总是在这里帮助你。请不要介意在发布这个答案时是否有一些错误,cz我只是第三次发布
谢谢。非常有用:你好,拉凯什,这是一个很好的建议,但是什么是
managedObjectContext
。你能给我们一个示例代码让我们理解它吗。提前谢谢。你好,拉凯什,你能给我推荐任何关于Xmppframework的教程链接或演示或任何帮助吗。在authenticateWithPassword方法中,我遇到了这个错误或者messase—“摘要字符串解析错误”?ThanksHi Rakesh,我正在对mac执行相同的操作,authenticateWithPassword方法返回YES,但始终未调用Authenticate委托方法。您能为我提供任何链接吗?Thanksuid=[dict objectForKey:@“id”];应替换为uid=[dict objectForKey:@“uid”];@Rakesh我熟悉Facebook API,但对标准XMPP非常陌生,最近发布了这个问题()。上面的方法是否能为彼此不是朋友的Facebook应用程序用户提供一种高效返回在线状态的方法?无论哪种方法,我都希望得到您的指导,谢谢。
- (void)request:(FBRequest*)request didFailWithError:(NSError*)error{
DDLogError(@"%s %@",__PRETTY_FUNCTION__,error);
//[appDelegate.facebook logout:self];
}
- (void)request:(FBRequest*)request didLoad:(id)result {
DDLogVerbose(@"%s............DDLOG................... %@",__PRETTY_FUNCTION__,result);
NSLog(@" Result>>>>-------%@", result);
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:(NSMutableDictionary *)result];
uid = [dict objectForKey:@"id"];
NSLog(@"iddddddddddddd---%@", uid);
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView1
{
return [[[self fetchedResultsController] sections] count];
- (NSString *)tableView:(UITableView *)sender titleForHeaderInSection:(NSInteger)sectionIndex
if (sectionIndex < [sections count])
{
id <NSFetchedResultsSectionInfo> sectionInfo = [sections objectAtIndex:sectionIndex];
int section = [sectionInfo.name intValue];
switch (section)
{
case 0 : return @"Available";
case 1 : return @"Away";
default : return @"Offline";
}
}
}
return @"";
}
- (NSInteger)tableView:(UITableView *)tableView1 numberOfRowsInSection:(NSInteger)sectionIndex
{
NSArray *sections = [[self fetchedResultsController] sections];
if (sectionIndex < [sections count])
{
id <NSFetchedResultsSectionInfo> sectionInfo = [sections objectAtIndex:sectionIndex];
return sectionInfo.numberOfObjects;
NSLog(@"section ifnfo ===========%@", sectionInfo);
}
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[cell setSelectionStyle:UITableViewCellSelectionStyleGray];
user = [[self fetchedResultsController] objectAtIndexPath:indexPath];
cell.textLabel.text = user.displayName;
cell.textLabel.textColor = [UIColor whiteColor];
cell1 = cell;
- (NSFetchedResultsController *)fetchedResultsController
{
if (fetchedResultsController == nil)
{
NSEntityDescription *entity = [NSEntityDescription entityForName:@"XMPPUserCoreDataStorage"
inManagedObjectContext:[self managedObjectContext]];
NSSortDescriptor *sd1 = [[NSSortDescriptor alloc] initWithKey:@"sectionNum" ascending:YES];
NSSortDescriptor *sd2 = [[NSSortDescriptor alloc] initWithKey:@"displayName" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sd1, sd2, nil];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:entity];
[fetchRequest setSortDescriptors:sortDescriptors];
[fetchRequest setFetchBatchSize:10];
fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:[self managedObjectContext]
sectionNameKeyPath:@"sectionNum"
cacheName:nil];
[fetchedResultsController setDelegate:self];
[sd1 release];
[sd2 release];
[fetchRequest release];
NSError *error = nil;
if (![fetchedResultsController performFetch:&error])
{
NSLog(@"Error performing fetch: %@", error);
}
}
return fetchedResultsController;
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[[self tableView] reloadData];
}