2013-03-06 28 views
3

我正在按照教程: http://mobile.tutsplus.com/tutorials/iphone/building-a-jabber-client-for-ios-server-setup/設置帶有ejabberd服務器的iOS應用程序。到目前爲止,我幾乎將代碼複製到一個新項目中。在模擬器中調用XMPPFramework委託函數,但不在電話上

我的問題是,在手機上運行時,不會調用XMPP委託函數AppDelegate.m。一切工作正常在模擬器和下面的兩個函數被調用。

- (void)xmppStreamDidConnect:(XMPPStream *)sender { 
    NSLog(@"in WSAppDelegate - xmppStreamDidConnect"); 
    isOpen = YES; 
    NSError *error = nil; 
    [[self xmppStream] authenticateWithPassword:password error:&error]; 

} 

- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender { 
    NSLog(@"in WSAppDelegate - xmppStreamDidAuthenticate"); 

    [self goOnline]; 

} 

我能夠在手機和模擬器上都連接爲這個調用運行沒有錯誤:

[xmppStream connect:&error] 

這裏是我的AppDelegate.h代碼:

#import <UIKit/UIKit.h> 
#import "XMPPRoster.h" 
#import "XMPP.h" 
#import "SMChatDelegate.h" 
#import "SMMessageDelegate.h" 

@class SMBuddyListViewController; 

@interface WSAppDelegate : NSObject <UIApplicationDelegate> { 
    UIWindow *window; 
    SMBuddyListViewController *viewController; 

    XMPPStream *xmppStream; 
    XMPPRoster *xmppRoster; 

    NSString *password; 

    BOOL isOpen; 
} 

@property (nonatomic, retain) IBOutlet UIWindow *window; 
@property (nonatomic, retain) IBOutlet SMBuddyListViewController *viewController; 


@property (nonatomic, readonly) XMPPStream *xmppStream; 
@property (nonatomic, readonly) XMPPRoster *xmppRoster; 

@property (nonatomic, assign) id _chatDelegate; 
@property (nonatomic, assign) id _messageDelegate; 

- (BOOL)connect; 
- (void)disconnect; 

@end 

而且AppDelegate中。 m:

#import "WSBuddyListViewController.h" 


@interface WSAppDelegate() 

- (void)setupStream; 

- (void)goOnline; 
- (void)goOffline; 

@end 


@implementation WSAppDelegate 

@synthesize xmppStream; 
@synthesize xmppRoster; 
@synthesize window; 
@synthesize viewController; 
@synthesize _chatDelegate; 
@synthesize _messageDelegate; 


- (void)applicationWillResignActive:(UIApplication *)application { 

    [self disconnect]; 

} 

- (void)applicationDidBecomeActive:(UIApplication *)application { 

    [self setupStream]; 
    BOOL connected = NO; 
    connected = [self connect]; 
    NSLog(@"*** connected = %i", connected); 


} 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 

    return YES; 
} 


- (void)setupStream { 
    NSLog(@"in WSAppDelegate - setupStream"); 

    xmppStream = [[XMPPStream alloc] init]; 
    [xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()]; 
    [xmppStream setHostName:@"localhost"]; 

} 

- (void)goOnline { 
    NSLog(@"in WSAppDelegate - goOnline"); 

    XMPPPresence *presence = [XMPPPresence presence]; 
    [[self xmppStream] sendElement:presence]; 
} 

- (void)goOffline { 
    XMPPPresence *presence = [XMPPPresence presenceWithType:@"unavailable"]; 
    [[self xmppStream] sendElement:presence]; 
} 

- (BOOL)connect { 
    NSLog(@"in WSAppDelegate - connect"); 

    [self setupStream]; 

    NSString *jabberID = [[NSUserDefaults standardUserDefaults] stringForKey:@"userID"]; 
    NSString *myPassword = [[NSUserDefaults standardUserDefaults] stringForKey:@"userPassword"]; 

    if (![xmppStream isDisconnected]) { 
     NSLog(@"in WSAppDelegate - connect - if (![xmppStream isDisconnected]) "); 

     return YES; 
    } 


    if (jabberID == nil || myPassword == nil) { 
     NSLog(@"in WSAppDelegate - connect - if (jabberID == nil || myPassword == nil)"); 

     return NO; 
    } 

    [xmppStream setMyJID:[XMPPJID jidWithString:jabberID]]; 
    password = myPassword; 

    NSError *error = nil; 
    if (![xmppStream connect:&error]) 
    { 
     NSLog(@"in WSAppDelegate - connect - if (![xmppStream connect:&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]; 
    [_chatDelegate didDisconnect]; 
} 



#pragma mark - 
#pragma mark XMPP delegates 


- (void)xmppStreamDidConnect:(XMPPStream *)sender { 
    NSLog(@"in WSAppDelegate - xmppStreamDidConnect"); 
    isOpen = YES; 
    NSError *error = nil; 
    [[self xmppStream] authenticateWithPassword:password error:&error]; 

} 

- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender { 
    NSLog(@"in WSAppDelegate - xmppStreamDidAuthenticate"); 

    [self goOnline]; 

} 


- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq { 

    return NO; 

} 

- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message { 
    NSLog(@"in WSAppDelegate - xmppStream:(XMPPStream *)sender didReceiveMessage"); 


    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]; 

} 

- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence { 
    NSLog(@"in WSAppDelegate - xmppStream:(XMPPStream *)sender didReceivePresence:"); 

    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, @"localhost"]]; 

     } else if ([presenceType isEqualToString:@"unavailable"]) { 

      [_chatDelegate buddyWentOffline:[NSString stringWithFormat:@"%@@%@", presenceFromUser, @"localhost"]]; 

     } 

    } 

} 


- (void)dealloc { 

    [xmppStream removeDelegate:self]; 
    [xmppRoster removeDelegate:self]; 

    [xmppStream disconnect]; 
} 
@end 

回答

8

如果你看t您的setupStream方法,您正在使用名稱「localhost」。這讓我相信服務器在您的開發機器上,並且設備正嘗試連接到自己(本地主機)。您將不得不用您的服務器名稱替換它。

這可能適用於模擬器,因爲客戶端和服務器是一個一樣的。

UPDATE

我只是閱讀本教程時,它不會在描述它如何在真機上的工作做好的。

+0

非常感謝。我被部分拋出,因爲[xmppStream connect:&error]被調用時沒有返回錯誤,我認爲初始連接已經完成,但錯誤發生在某處之後。 – Ramsel 2013-03-07 03:49:13

0

正如Mike D所說,您正在連接到您的計算機上的服務器([xmppStream setHostName:@「localhost」];) 要連接到「本地主機」,您必須更改hosts文件你的設備(/ etc/hosts),但這是Apple禁止的,因爲你的應用程序不能改變沙箱外的東西(除非設備已越獄)。 我曾經在android手機上做過這個東西,當時遇到類似的問題。 查看此場合(Does hosts file exist on the iPhone? How to change it?

相關問題