AppDelegate方法未被调用

时间:2013-06-23 16:27:06

标签: ios xmppframework

我正在尝试使用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的代码classe:

- (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委托方法。如果有人可以帮助我,请不要犹豫。

提前致谢。

1 个答案:

答案 0 :(得分:1)

我认为你误解了AppDelegate的目的。首先,对于您在Xcode中创建的每个iOS应用程序,都会创建一个包含名称AppDelegate的类,但此类只应用于获取应用程序状态的信息,例如应用程序转到后台,如果它成功启动或者它是从后台发布的。 app delegate还用于指定应用程序的根(或入口点)视图控制器。

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

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

P.S最后一点,看看“iOS命名和其他约定”enter link description here life cycle methods