我正在開發XMPP項目。我創建了連接並能夠成功登錄。但是我已經在appdelegate [connect]方法中設置了所有方法。所以當我登錄成功的應用程序,但是當我必須獲取朋友列表,然後我不得不再次調用appdelegate [connect]方法,所以我想設置所有條件和所有viewcontroller Loginbutton。所以當第二次調用appdelegate [connect]方法時,它不會影響到其他視圖控制器,也會導致結果。我已經嘗試了聲明BOOL方法,但我不成功。 這裏是我的嘗試。ios xmpp通過appdelegate從一個視圖控制器調用另一個視圖控制器
//appdelegate.m file//
-(BOOL) isauthenticate; // Mthod declaration
- (BOOL)connect
{
// HUD = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES] ;
//HUD.delegate = self;
HUD = [[MBProgressHUD alloc ] initWithWindow:[UIApplication sharedApplication ].keyWindow]; [self.window.rootViewController.view addSubview:HUD];
[HUD setDetailsLabelText:@"Please wait..."];
[HUD setDimBackground:YES];
[HUD setOpacity:0.5f];
[HUD show:YES];
// HUD.color =[UIColor colorWithPatternImage:[UIImage imageNamed:@"logo"]];
// [HUD hide:YES afterDelay:10.0];
if (![xmppStream isDisconnected]) {
return YES;
// isauthenticate=YES;
}
NSString *myJID = [[NSUserDefaults standardUserDefaults] stringForKey:kXMPPmyJID];
NSString *myPassword = [[NSUserDefaults standardUserDefaults] stringForKey:kXMPPmyPassword];
//
// If you don't want to use the Settings view to set the JID,
// uncomment the section below to hard code a JID and password.
//
// myJID = @"[email protected]/xmppframework";
// myPassword = @"";
if (myJID == nil || myPassword == nil) {
return NO;
}
[xmppStream setMyJID:[XMPPJID jidWithString:myJID]];
password = myPassword;
NSError *error = nil;
if (![xmppStream connectWithTimeout:XMPPStreamTimeoutNone error:&error])
{
HUD.hidden=YES;
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error connecting"
message:@"See console for error details."
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertView show];
// DDLogError(@"Error connecting: %@", error);
// return NO;
}
return YES;
}
這裏是我的Viewcontroller.m文件
//viewcontroller.m file//
- (IBAction)checkLogin:(id)sender {
[self dismissKeyboard];
// HUD = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES] ;
// HUD.delegate = self;
NSLog(@"Email: %@ Password: %@",mViewEmail.text,mViewPassword.text);
[self setField:mViewEmail forKey:kXMPPmyJID];
[self setField:mViewPassword forKey:kXMPPmyPassword];
if ([[self appDelegate ]connect])
{
if (appdelegate.isauthenticate==YES) {
//appdelegate.isauthenticate=YES;
[self showHome];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sorry"
message:@"Please Check Username or Password"
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
}
}
- (void)showHome{
//[[self appDelegate]isauthenticate];
[self performSegueWithIdentifier:@"signIn" sender:self];
}
究竟是什麼解決辦法嗎?
那麼我該如何在buddlist控制器中設置?目前我正在使用此條件來查看朋友列表。如果([[self appDelegate] connect]){ titleLabel.text = [[[[self appDelegate] xmppStream] myJID] bare]; } else { titleLabel.text = @「No JID」; } [titleLabel sizeToFit]; self.navigationItem.titleView = titleLabel; ' –
其實你正在檢查你的身份驗證與jid和pwd是否經過身份驗證。你不是在管理好友列表。 –
是的,你是對的,但是當我使用這個條件時,它會再次調用connect方法,我的頁面會再次被重定向到home.because connectiondidauthenticate再次執行。所以,我只想在登錄按鈕中實現segue。 –