【问题标题】:How to implement the way to send a friend request in XMPPFramework in iOS?如何在iOS的XMPPFramework中实现发送好友请求的方式?
【发布时间】:2013-12-29 07:46:24
【问题描述】:

我已经想出了在 XMPPRoster 中使用 MainAppDelegate.m 中的以下内容接受订阅请求的方法:

- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence
    {
     NSString *presenceType = [presence type];
     if  ([presenceType isEqualToString:@"subscribe"]) {
       [xmppRoster acceptPresenceSubscriptionRequestFrom:[presence from] andAddToRoster:YES];
   } 

但是,我无法在单击按钮时发送好友请求。我面临的主要问题是我的新朋友添加表单位于一个单独的 ViewController.m 类中,而不是 MainAppDelegate.m。如何从 ViewController.m 访问 XMPPRoster 方法?我是否必须为 XMPPRoster 重新声明对象,或者我可以以某种方式重用已在 MainAppDelegate.m 文件中实例化的对象?

【问题讨论】:

    标签: ios xmppframework


    【解决方案1】:

    重用已经存在的 XMPPRoster 对象就足够了。为此,您可以为您的 ViewController.m 编写自定义初始化方法,例如

        - initWithRoster:(XMPPRoster *)roster
        {
           self = [super initWithNibName:@"nibName" bundle:nil];
           ...
        } 
    

    然后将名册分配给 ViewController 的实例变量或属性。然后可以在加载和加载视图时访问 XMPPRoster 对象。

    或者,您可以在 ViewController 实例化之后但在控制器显示之前将 XMPPRoster 属性添加到 ViewController 并从 MainAppDelegate 分配 roster 对象。但我更喜欢第一种解决方案。

    【讨论】:

    • 我尝试在 ViewController 中实例化 MainAppDelegate 的对象,如下所示:MainAppDelegate *myappdelegate = [[MainAppDelegate alloc] init]; [myappdelegate.xmppRoster subscribePresenceToUser:(XMPPJID *)friendID]; 但是,它不起作用。另外,您能否解释一下您在第一个解决方案中创建的名册对象如何链接到在 MainAppDelegate.m 中实例化的 XMPPRoster 对象?
    • 您在 ViewController 中创建的 MainAppDelegate 对象与 App 启动时实例化的运行时 AppDelegate 不同。如果您想从 ViewController 中访问应用程序委托,您必须使用以下内容:(MainAppDelegate *)[UIApplication sharedApplication].delegate。当您已经在 MainAppDelegate 中创建了第一个 XMPPRoster 对象并且您的 ViewController 具有在接口文件中声明的属性 @property (nonatomic) XMPPRoser *roster 时,您可以简单地分配在 MainAppDelegate 中创建的 XMPPRoster 实例。
    猜你喜欢
    • 1970-01-01
    • 2014-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-30
    • 2017-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多