gpt4 book ai didi

ios - AppDelegate 方法未被调用

转载 作者:行者123 更新时间:2023-11-29 03:49:17 25 4
gpt4 key购买 nike

我正在尝试使用 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 委托(delegate)方法没有被调用。如果有人可以帮助我,请不要犹豫。

提前致谢。

最佳答案

我认为您误解了AppDelegate的用途。首先,对于您在 Xcode 中创建的每个 iOS 应用程序,都会创建一个包含名称 AppDelegate 的类,但该类只能用于获取应用程序的信息状态,例如应用程序是否进入后台、是否成功启动或是否从后台启动。此外,应用程序委托(delegate)还用于指定应用程序的根(或入口点) View Controller 。

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

我强烈建议您查看一下 View Controller ,并且我很确定在执行上述建议的“任务”后,您将回答自己发布的问题。

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

关于ios - AppDelegate 方法未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17262822/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com