如何在iOS的XMPPFramework中实现发送好友请求的方式?
我已经在MainAppDelegate.m中找到了在XMPPRoster中接受订阅请求的方法:如何在iOS的XMPPFramework中实现发送好友请求的方式?,ios,xmppframework,Ios,Xmppframework,我已经在MainAppDelegate.m中找到了在XMPPRoster中接受订阅请求的方法: - (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence { NSString *presenceType = [presence type]; if ([presenceType isEqualToString:@"subscribe"]) { [
- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence
{
NSString *presenceType = [presence type];
if ([presenceType isEqualToString:@"subscribe"]) {
[xmppRoster acceptPresenceSubscriptionRequestFrom:[presence from] andAddToRoster:YES];
}
但是,我无法通过单击按钮发送好友请求。我面临的主要问题是,我的新朋友add表单位于一个独立的ViewController.m类中,而不是MainAppDelegate.m。如何从ViewController.m访问XMPPRoster方法?我是否必须为XMPPRoster重新声明对象,或者我是否可以以某种方式重用已在MainAppDelegate.m文件中实例化的对象 重用已经存在的XMPPRoster对象就足够了。为此,您可以为ViewController.m编写一个自定义init方法,例如
- initWithRoster:(XMPPRoster *)roster
{
self = [super initWithNibName:@"nibName" bundle:nil];
...
}
然后将花名册分配给ViewController的实例变量或属性。然后,可以在加载视图时访问XMPPRoster对象
或者,您可以只向ViewController添加一个XMPPRoster属性,并在实例化视图控制器之后但在显示控制器之前从MainAppDelegate分配花名册对象。但我更喜欢第一种解决方案。我尝试在ViewController中实例化MainAppDelegate的对象,如下所示:
MainAppDelegate*myappdelegate=[[MainAppDelegate alloc]init];[myappdelegate.xmppRoster subscribePresenceToUser:(XMPPJID*)friendID]代码>但是,它不起作用。另外,请您解释一下,您在第一个解决方案中创建的花名册对象是如何链接到MainAppDelegate.m中实例化的XMPPRoster对象的?您在ViewController中创建的MainAppDelegate对象与应用程序启动时实例化的运行时AppDelegate对象不同。如果要从ViewController中访问应用程序委派,必须使用以下命令:(MainAppDelegate*)[UIApplication sharedApplication]。委派
。如果已经在MainAppDelegate中创建了第一个XMPPRoster对象,并且ViewController具有在接口文件中声明的属性@property(非原子)XMPPRoser*花名册
,则可以简单地分配在MainAppDelegate中创建的XMPPRoster实例。