0

我正在尝试使用 xmppframework 制作一个 jabber 聊天应用程序。我已经在 applicationAppDelegate 中实现了 xmppStream 方法,但是这些方法都没有被调用。

这是 applicationAppDelegate 的代码:

    - (void)setupStream {
    xmppStream = [[XMPPStream alloc] init];
    [xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
    //[self connect];
}
- (void)goOnline {
    XMPPPresence *presence = [XMPPPresence presence];
    [[self xmppStream] sendElement:presence];
}
- (void)goOffline {
    XMPPPresence *presence = [XMPPPresence presenceWithType:@"unavailable"];
    [[self xmppStream] sendElement:presence];
}

- (BOOL)connect {
    [self setupStream];


    NSString *emailUserDefault = [[NSUserDefaults standardUserDefaults] stringForKey:@"email"];

    NSString *jabberID = [emailUserDefault stringByAppendingString:@"@server.local"];
    NSLog(@"%@",jabberID);
    NSString *myPassword = [[NSUserDefaults standardUserDefaults] stringForKey:@"password"];
    NSLog(@"%@",myPassword);

    if (![xmppStream isDisconnected]) {
        NSLog(@"You are connected");

        return YES;
    }
    if (jabberID == nil || myPassword == nil) {
        return NO;
    }
    [xmppStream setMyJID:[XMPPJID jidWithString:jabberID]];
    //xmppStream.myJID = [XMPPJID jidWithString:jabberID];
    password = myPassword;

    NSError *error = nil;
    if (![xmppStream connectWithTimeout:20 error:&error])
    {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                            message:[NSString stringWithFormat:@"Can't connect to server %@", [error localizedDescription]]
                                                           delegate:nil
                                                  cancelButtonTitle:@"Ok"
                                                  otherButtonTitles:nil];
        [alertView show];
        return NO;
    }

    return YES;
}
- (void)disconnect {
    [self goOffline];
    [xmppStream disconnect];
}
- (void)xmppStreamDidConnect:(XMPPStream *)sender {
    isOpen = YES;
    NSError *error = nil;
    [[self xmppStream] authenticateWithPassword:password error:&error];
}
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender {
    [self goOnline];
}
- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence {
    NSString *presenceType = [presence type]; // online/offline
    NSString *myUsername = [[sender myJID] user];
    NSString *presenceFromUser = [[presence from] user];
    if (![presenceFromUser isEqualToString:myUsername]) {
        if ([presenceType isEqualToString:@"available"]) {
            [__chatDelegate newBuddyOnline:[NSString stringWithFormat:@"%@@%@", presenceFromUser, @"server.local"]];
        } else if ([presenceType isEqualToString:@"unavailable"]) {
            [__chatDelegate buddyWentOffline:[NSString stringWithFormat:@"%@@%@", presenceFromUser, @"server.local"]];
        }
    }
}
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message {
    NSString *msg = [[message elementForName:@"body"] stringValue];
    NSString *from = [[message attributeForName:@"from"] stringValue];
    NSMutableDictionary *m = [[NSMutableDictionary alloc] init];
    [m setObject:msg forKey:@"msg"];
    [m setObject:from forKey:@"sender"];
    [__messageDelegate newMessageReceived:m];

}

这是我的 chatViewController 类代码:

- (myApplicationAppDelegate *)appDelegate {
    return (myApplicationAppDelegate *)[[UIApplication sharedApplication] delegate];
}
- (XMPPStream *)xmppStream {
    return [[self appDelegate] xmppStream];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    onlineBuddies = [[NSMutableArray alloc ] init];
    myApplicationAppDelegate *del = [self appDelegate];
    [self xmppStream];
    NSString *login = [[NSUserDefaults standardUserDefaults] objectForKey:@"email"];
    del._chatDelegate = self;
    if (login) {
        if ([[self appDelegate] connect]) {
            NSLog(@"show buddy list");
        }
    } else {
        NSLog(@"Login Error");
    }

}

我无法弄清楚为什么没有调用 xmpp 委托方法。如果有人可以帮我一把,请不要犹豫。

提前致谢。

4

1 回答 1

1

我想你误解了AppDelegate. 首先,对于您正在创建的每个 iOS 应用程序,都会创建Xcode一个包含名称的类,AppDelegate但该类仅用于获取应用程序状态的信息,例如应用程序是否进入后台,是否成功启动或者如果它来自背景。应用程序委托还用于指定应用程序的根(或入口点)视图控制器。

所以我认为你应该首先检查关于如何创建一个非常简单的应用程序(“Hello World 应用程序”)的基本规则(或基本教程),然后你可以继续创建应用程序的基本结构并决定什么视图控制器或哪些模型类将处理您的连接处理和响应/请求解析。

我强烈建议您查看视图控制器,并且我很确定在您完成上述建议的“任务”之后,您将回答自己发布的问题。

PS 最后一点,看看“iOS 命名和其他约定”在此处输入链接描述生命周期方法

于 2013-06-23T20:59:43.873 回答